ZOJ3331-Process the Tasks(双塔DP)
来源:互联网 发布:杀敌算法 下载 编辑:程序博客网 时间:2024/06/10 13:10
There are two machines A and B. There are n tasks, namely task 1, task 2, ..., task n. You must assign each task to one machine to process it. There are some facts you must know and comply with:
- You must assign each task to only one machine to process.
- At any time, a machine can process at most one task.
- Task i (0 < i < n) can be processed if and only if each task j (0 < j < i) has been processed or processing.
- If a task is processing on one machine, it cannot be interrupted.
You want to do finish all the tasks as soon as possible.
Input
There are multiple test cases. The first line of the input is an integer T (0 < T < 1000) indicating the number of test cases. Then T test cases follow. Each test case starts with an integer n (0 < n < 100). The ith line of the next n lines contains two integers tA, tB (0 < tA, tB < 100), giving the time to process the ith task by machine A and machine B.
Output
For each test case, output the earliest time when all the tasks have been processed.
Sample Input
411 221 22 121 290 9531 31 31 3
Sample Output
11903
Hints
- Test case 1: Let machine A process task 1.
- Test case 2: Let machine A process task 1 and at the same time let machine B process task 2.
- Test case 3: Let machine B process task 1 and at the same time let machine A process task 2.
- Test case 4: Let machine A process all the tasks.
Author: CAO, Peng
Source: The 7th Zhejiang Provincial Collegiate Programming Contest
题意:有两个机器,A和B,现在有N个任务,每个任务用A机器做需要ai时间,用B机器做需要bi时间。 a,b,n<100,第i个任务只有在第i-1个任务在做或者已经做完才可以开始做,每个机器只能同时做一个任务,问最少需要多少时间能最晚所有任务
解题思路:双塔dp。
首先可以将问题转化为两个塔,塔的高度为在该机器上完成的项目的总时间。
存在两种情况:
1.工作加在较高的塔上,此时高度差增加,需注意低塔也要增加空闲值(因为那段时间还在处理i-1任务,i任务还没有开始,i+1不可能进行)
2.工作加在较低的塔上,①低塔变为高塔 ② 低塔仍为低塔
以A塔为高塔为例:
ps:高度差 j 为A塔高度减去B塔
#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <algorithm>#include <cmath>#include <queue>#include <vector>#include <set>#include <stack>#include <map>#include <climits>using namespace std;#define LL long longconst int INF=0x3f3f3f3f;const int MAX=100009;int dp[105][200];//前i项任务,两个机器时间差为j,两塔中较高塔的高度。int ta[105],tb[105];int main(){ int t; scanf("%d",&t); while(t--) { int k= 100;//时间差可为正可为负,故增加k保证值为正 int n; scanf("%d",&n); for (int i = 1; i <= n; i++) scanf("%d %d",&ta[i],&tb[i]); for (int i=0;i<=n;i++) for (int j=0;j<=200;j++) dp[i][j]=INF; dp[0][0+k]=0;//没有任务时候,为0 for(int i=1;i<=n;i++) { for(int j=-99;j<=99;j++)// 时间差 { if(dp[i-1][j+k]==INF) continue; if(j<0)//B塔较高 { dp[i][-tb[i]+k]=min(dp[i][-tb[i]+k],dp[i-1][j+k]+tb[i]);//第i项任务放在B塔上 dp[i][j+ta[i]+k]=min(dp[i][j+ta[i]+k],dp[i-1][j+k]+max(0,j+ta[i]));//第i项任务放在A塔上,①低塔变成高塔 + j + ta[i];②低塔仍为低塔 + 0(利用了空闲时间,高塔高度不变即dp值不变); } else { dp[i][ta[i]+k]=min(dp[i][ta[i]+k],dp[i-1][j+k]+ta[i]); dp[i][j-tb[i]+k]=min(dp[i][j-tb[i]+k],dp[i-1][j+k]+max(0,tb[i]-j)); } } } int ans=INF; for (int i=-99;i<=99;i++) ans=min(dp[n][i+k],ans); printf("%d\n",ans); } return 0;}
- ZOJ3331-Process the Tasks(双塔DP)
- ZOJ3331---Process the Tasks(dp)
- Process the Tasks(ZOJ3331)
- zoj 3331 Process the Tasks(双塔DP)
- ZOJ 3331 Process the Tasks(双塔DP)
- ZOJ 3331-Process the Tasks (DP)
- ZOJ-3331-Process the Tasks【7th浙江省赛】【双塔dp】【好题】
- ZOJ 3331 Process the Tasks
- ZOJ 3331 Process the Tasks
- Event Handler to trigger User process tasks when the associated Organization is updated
- A Task Process DP
- tasks
- iOS-Performing Common Peripheral Role Tasks(API Reference)the fourth part
- HDU3433 A Task Process(dp+二分查找)
- Launching Tasks in the Foreground and Background
- Circular dependency between the following tasks:
- The C# Design Process
- The Linux Process Manager
- 数据结构之简单算法学习
- 算法-数组-二维数字中的查找
- 集合接口collection与Map接口
- JSP基本语法
- Codeforces Round #395 (Div. 2) D Timofey and rectangles(思维题)
- ZOJ3331-Process the Tasks(双塔DP)
- 协同过滤推荐算法总结
- Ubuntu16安装搜狗拼音输入法
- http://blog.csdn.net/bat67/article/details/52145331
- jsp与servlet中文乱码问题
- POJ 3190 Stall Reservations
- 算法训练 最大的算式
- 第四次到上海,这里正下着小雨
- http://blog.csdn.net/bat67/article/details/52054562