【题解】任务分配

【问题描述】

现有n个任务,要交给A和B完成。每个任务给A或给B完成,所需的时间分别为ai和bi。问他们完成所有的任务至少要多少时间。

【输入】

第一行一个正整数n,表示有n个任务。

接下来有n行,每行两个正整数ai,bi。

【输出】

    一个数,他们完成所有的任务至少要的时间。

【输入样例】divide.in

3

5 10

6 11

7 12

【输出样例】divide.out

12

【输入输出样例解释】

A完成任务1和任务2,时间为11。B完成任务3,时间为12。

或者 A完成任务1和任务3,时间为12。B完成任务2,时间为11。

【限制】

30%的数据满足:1 <= n <= 20

100%的数据满足:1 <= n <= 200 , 1 <= ai,bi <=200

这道题乍一看感觉很简单,只要判断每一项任务是a做还是b做,要定义三维,可实际上我们开不了那么大的数组,

所以我想到的是缩维,于是定义f(i,j)表示第i项任务,a的时间是j,b的时间是f(i,j),就顺利缩维。

 1 #include <iostream>
 2 #include <fstream>
 3 #include <cstdlib>
 4 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
 5 using namespace std;
 6 
 7 int cnt_gs;
 8 int shijab[205][2];
 9 int f[205][40005];
10 
11 
12 ifstream fin("divide.in");
13 ofstream fout("divide.out");
14 
15 using namespace std;
16 
17 int main(int argc, char** argv) {
18 fin>>cnt_gs;
19 for(int x=1;x<=cnt_gs;x++)fin>>shijab[x][0]>>shijab[x][1];
20 
21 int sum=0;
22 for(int x=1;x<=cnt_gs;x++){
23  sum+=shijab[x][0];
24  for(int y=0;y<=sum;y++){
25   f[x][y]=max(f[x][y],f[x-1][y]+shijab[x][1]);    
26   if(y>=shijab[x][0])f[x][y]=min(f[x][y],f[x-1][y-shijab[x][0]]);    
27  }    
28 } 
29  
30 int ans=40005;
31 for(int x=1;x<=sum;x++){
32 ans=min(ans,max(x,f[cnt_gs][x]));        
33 } 
34 cout<<ans;
35 fout<<ans;
36 return 0;
37 }
原文地址:https://www.cnblogs.com/Ateisti/p/4923424.html