pku1337 A Lazy Worker 很诡异的DP

题意:
很多人这题题目读不懂,我详细解释下:一个懒工人,他想工作的时间尽量少。有N个任务,每个任务有开始时间和deadline,工人完成这个工作需要ti时间。如果某个时刻有工作可以做(这里是说,如果从这个时刻开始某个工作,在deadline之前能够完成),那么这个工人必须做,但是如果这个时刻存在着多余1件工作可以做,工人可以选择;假设这个时刻没有工作可以做了,工人就可以偷懒直到有新的任务到来。

解法:
这题初看觉得很麻烦,但是题目中有句话太给力了! You should note that for each job i, the length of Ii, di - ai, is greater than or equal to ti, but less than 2*ti
这句话把后效性全部消除了,即不可能出现这种情况,在t1这个决策点出现了第k个工作,在t1后的某个决策点t2又出现了第k个工作,明白了?
DP方程:
dp[t]=min(work[i]+dp[t+work[i]]) work指在第t个时间点能够可以开始做的工作
如果在第t个时刻没有任务可做,那么dp[t]=dp[t+1]

代码:
 1 //============================================================================
 2 // Name        : pku1337.cpp
 3 // Author      : yzhw
 4 // Version     :
 5 // Copyright   : yzhw
 6 // Description : Hello World in C++, Ansi-style
 7 //============================================================================
 8 
 9 #include <iostream>
10 #include <algorithm>
11 #include <vector>
12 #include <cstring>
13 using namespace std;
14 vector<int> work[251];
15 int data[101][3],n,minnum,maxnum,dp[300];
16 int getdp(int t)
17 {
18     if(t>maxnum) return 0;
19     else return dp[t];
20 }
21 int main() {
22     int test;
23     cin>>test;
24     while(test--)
25     {
26         cin>>n;
27         minnum=300;
28         maxnum=-1;
29         for(int i=0;i<=250;i++)
30             work[i].clear();
31         for(int i=0;i<n;i++)
32         {
33             cin>>data[i][0]>>data[i][1]>>data[i][2];
34             for(int j=data[i][1];j+data[i][0]<=data[i][2];j++)
35                 work[j].push_back(i);
36             minnum=min(minnum,data[i][1]);
37             maxnum=max(maxnum,data[i][2]-data[i][0]);
38         }
39         memset(dp,0,sizeof(dp));
40         for(int t=maxnum;t>=minnum;t--)
41           if(work[t].empty())
42               dp[t]=dp[t+1];
43           else
44           {
45               dp[t]=data[work[t][0]][0]+getdp(data[work[t][0]][0]+t);
46               for(int i=1;i<work[t].size();i++)
47                 dp[t]=min(dp[t],data[work[t][i]][0]+getdp(data[work[t][i]][0]+t));
48           }
49         cout<<dp[minnum]<<endl;
50     }
51     return 0;
52 }
53 

posted on 2011-01-23 02:44 yzhw 阅读(226) 评论(0)  编辑 收藏 引用 所属分类: DP


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2011年1月>
2627282930311
2345678
9101112131415
16171819202122
23242526272829
303112345

导航

统计

公告

统计系统

留言簿(1)

随笔分类(227)

文章分类(2)

OJ

最新随笔

搜索

积分与排名

最新评论

阅读排行榜