-
Notifications
You must be signed in to change notification settings - Fork 8
/
1821.cpp
92 lines (79 loc) · 2.24 KB
/
1821.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <deque>
#include <list>
using namespace std;
#define MAXWORKER 110
#define MAXBENCH 16005
#define dprintf(x,args...)
struct _worker
{
int L,P,S;
};
bool operator < (const _worker& inca,const _worker& incb)
{
return inca.S<incb.S;
}
_worker worker[MAXWORKER];
int dp[MAXWORKER][MAXBENCH];
int main()
{
int N,K;
while(scanf("%d %d",&N,&K)==2)
{
for(int i=1;i<=K;i++)
{
scanf("%d %d %d",&worker[i].L,&worker[i].P,&worker[i].S);
}
sort(worker+1,worker+K+1);
/// DP Init
memset(dp,0,sizeof(dp));
/*
/// Enumerate By Worker (K)
for(int i=0;i<K;i++)
{
}
*/
/// dp[i][j] means the MAX income while
/// i worker have operated j benches.
for(int i=1;i<=K;i++)
{
dprintf(">>>>>>> i=%d\n",i);
deque<int> pbus;
pbus.push_back(max(0,worker[i].S-worker[i].L));
for(int j=1;j<=N;j++)
{
dprintf(">>> j=%d (i=%d)\n",j,i);
dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
/// i-th Worker cannot reach j-th bench
/// !!! Must include 'equal' (=) !!!
if(worker[i].L+worker[i].S<=j) continue;
while(!pbus.empty()&&pbus.front()+worker[i].L<j) pbus.pop_front();
if(j<worker[i].S)
{
while(!pbus.empty()&&
dp[i-1][pbus.back()]-pbus.back()*worker[i].P
<
dp[i-1][j]-j*worker[i].P
)
pbus.pop_back();
pbus.push_back(j);
continue;
}
dp[i][j]=max(dp[i][j],dp[i-1][pbus.front()]+worker[i].P*(j-pbus.front()));
/*
/// Output DP
for(int pi=1;pi<=K;pi++)
{
for(int pj=1;pj<=N;pj++) printf("%d ",dp[pi][pj]);
printf("\n");
}
*/
}
}
printf("%d\n",dp[K][N]);
}/// End of Main Loop
return 0;
}