Fence

有一个长度为n的\([1,n]\)墙,有k位工人,第i位工人有参数\(s_i,p_i,l_i\),意思该位工人可以刷包含\(s_i\)的长度小于等于\(l_i\)的区间,报酬为区间长度乘以\(p_i\),墙的一个位置不能被重复刷,问最大的报酬之和,\(1 <= n <= 16 000,1 <= k <= 100\)

注意到k * n才十万,不难想到设\(f[i][j]\)表示前i位工人刷前j个位置的最大报酬之和,注意到我们要保证递推的无后效性,于是我们得把工人的\(s_i\)排序,因此有

\[f[i][j]=\max(f[i-1][j],f[i][j-1],\max_{j-l_i\leq k\leq s_i}f[i-1][k]+(j-k)\times p_i)
\]

注意到在i一定时,决策范围都是呈单调性,且j与k无关,于是可以使用单调队列优化,注意判边界即可,时间复杂度\(O(nk)\)。

参考代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define il inline
#define ri register
using namespace std;
struct inter{
int l,p,s;
il bool operator<(const inter&x)const{
return s<x.s;
}
}I[150];
int dp[150][16050],T[20050],L,R;
il void read(int&);
template<class free>
il free Max(free,free);
int main(){
int N,K;
read(N),read(K);
for(int i(1);i<=K;++i)
read(I[i].l),read(I[i].p),read(I[i].s);
sort(I+1,I+K+1);
for(int i(1),j;i<=K;++i){
L=1,R=0;
for(j=0;j<=N;++j){
while(L<=R&&T[L]<j-I[i].l)++L;
dp[i][j]=Max(dp[i][j-1],dp[i-1][j]);//麻烦解释一下,这里明显越界了,但是改成不越界反而a不掉了
if(L<=R&&j>=I[i].s)dp[i][j]=Max(dp[i][j],dp[i-1][T[L]]+(j-T[L])*I[i].p);
if(j<I[i].s){
while(L<=R&&dp[i-1][j]-j*I[i].p>=dp[i-1][T[R]]-T[R]*I[i].p)--R;
T[++R]=j;
}
}
}printf("%d",dp[K][N]);
return 0;
}
template<class free>
il free Max(free a,free b){
return a>b?a:b;
}
il void read(int &x){
x&=0;ri char c;while(c=getchar(),c<'0'||c>'9');
while(c>='0'&c<='9')x=(x<<1)+(x<<3)+(c^48),c=getchar();
}

Fence的更多相关文章

  1. [LeetCode] Paint Fence 粉刷篱笆

    There is a fence with n posts, each post can be painted with one of the k colors. You have to paint ...

  2. poj 3253 Fence Repair

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 42979   Accepted: 13999 De ...

  3. CF 484E - Sign on Fence

    E. Sign on Fence time limit per test 4 seconds memory limit per test 256 megabytes input standard in ...

  4. poj3253 Fence Repair

    http://poj.org/problem?id=3253 Farmer John wants to repair a small length of the fence around the pa ...

  5. CF448C Painting Fence (分治递归)

    Codeforces Round #256 (Div. 2) C C. Painting Fence time limit per test 1 second memory limit per tes ...

  6. Codeforces Round #276 (Div. 1) E. Sign on Fence 二分+主席树

    E. Sign on Fence   Bizon the Champion has recently finished painting his wood fence. The fence consi ...

  7. ACM Color the fence

    Color the fence 时间限制:1000 ms  |  内存限制:65535 KB 难度:2   描述 Tom has fallen in love with Mary. Now Tom w ...

  8. codeforces 349B Color the Fence 贪心,思维

    1.codeforces 349B    Color the Fence 2.链接:http://codeforces.com/problemset/problem/349/B 3.总结: 刷栅栏.1 ...

  9. [LintCode] Paint Fence 粉刷篱笆

    There is a fence with n posts, each post can be painted with one of the k colors.You have to paint a ...

  10. LeetCode Paint Fence

    原题链接在这里:https://leetcode.com/problems/paint-fence/ 题目: There is a fence with n posts, each post can ...

随机推荐

  1. 操作bin目录下的文件

    string dir = AppDomain.CurrentDomain.BaseDirectory + "Video"; if (!System.IO.Directory.Exi ...

  2. touchWX使用 echarts

    <button bindtap="init" wx:if="{{!isLoaded}}">加载图表</button> <butto ...

  3. Raft——可理解的分布式一致性算法

    Raft  Understandable Distributed Consensus http://thesecretlivesofdata.com/raft/ 一个直观的动画,便于理解raft算法. ...

  4. HTML常见问题

    一.外边距,margin: 垂直外边距的重叠:在网页中相邻的垂直方向的外边距会发生外边距的重叠 所谓的外边距重叠指兄弟元素之间的相邻外边距会取最大值而不是取和. .box1{margin-bottom ...

  5. C#异步编程----async和await组合的写法

    微软示例: private async void StartButton_Click(object sender, RoutedEventArgs e) { // ExampleMethodAsync ...

  6. Pregel Combiner

  7. 从零开始搭建系统1.5——Redis安装及配置

    1.在/usr/目录下创建redis目录 [root@localhost usr]# mkdir redis 2.下载安装包 wget http://download.redis.io/release ...

  8. final、finally和finalized的区别?

    (1)final:被final修饰的类,不被能继承:被final修饰的方法,不能被重写:被fianl修饰的量,为常量,只能被赋值一次: (2)finally:异常处理,和try.catch结合使用,可 ...

  9. h5 app 设置全屏

    h5 app的全屏和沉浸式状态栏是不一样的 全屏模式 常见使用场景:如果页面是全屏游戏,一般会直接让状态栏消失,也就是页面全屏.webview高度全屏了,状态栏没有了.写法: 终端支持:没有终端类型限 ...

  10. CTF学习路线指南(附刷题练习网址)

    PWN,Reverse:偏重对汇编,逆向的理解: Gypto:偏重对数学,算法的深入学习: Web:偏重对技巧沉淀,快速搜索能力的挑战: Mic:则更为复杂,所有与计算机安全挑战有关的都算在其中 常规 ...