Description
A team of k (1 <= K <= 100) workers should paint a fence which contains N (1 <= N <= 16 000) planks numbered from 1 to N from left to right. Each worker i (1 <= i <= K) should sit in front of the plank Si and he may paint only a compact interval (this means that the planks from the interval should be consecutive). This interval should contain the Si plank. Also a worker should not paint more than Li planks and for each painted plank he should receive Pi $ (1 <= Pi <= 10 000). A plank should be painted by no more than one worker. All the numbers Si should be distinct.
Being the team’s leader you want to determine for each worker the interval that he should paint, knowing that the total income should be maximal. The total income represents the sum of the workers personal income.
Write a program that determines the total maximal income obtained by the K workers.
Input
The input contains:
Input
N K
L1 P1 S1
L2 P2 S2
…
LK PK SK
Semnification
N -the number of the planks; K ? the number of the workers
Li -the maximal number of planks that can be painted by worker i
Pi -the sum received by worker i for a painted plank
Si -the plank in front of which sits the worker i
Output
The output contains a single integer, the total maximal income.
Sample Input
8 4
3 2 2
3 2 3
3 3 5
1 1 7
Sample Output
17
I think
单调队列优化DP
Code
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int sm = 16000 + 5;
int N,K;
int q[sm],qv[sm],dp[110][sm];
struct fence {
int s,l,p;
}f[sm];
bool cmp(fence x,fence y) {
return x.s<y.s;
}
int main() {
scanf("%d%d",&N,&K);
for(int i=1;i<=K;++i)
scanf("%d%d%d",&f[i].l,&f[i].p,&f[i].s);
sort(f+1,f+K+1,cmp);
for(int i=1;i<=K;++i) {
int now,s=1,t=1;
for(int j=max(0,f[i].s-f[i].l);j<f[i].s;++j) {
now=dp[i-1][j]-j*f[i].p;
while(s<t&&now>q[t-1])--t;
q[t]=now;qv[t++]=j;
}
for(int j=1;j<=N;++j) {
dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
if(j<f[i].s||f[i].s+f[i].l-1<j)continue;
while(s<t&&j-qv[s]>f[i].l)++s;
dp[i][j]=max(dp[i][j],q[s]+j*f[i].p);
}
}
printf("%d\n",dp[K][N]);
return 0;
}