UVa 11729 Commando War 【贪心】

题意:有n个部下,交待每个部下完成他相应的任务需要bi的时间,然后完成这项任务需要ji的时间,

选择交待任务的顺序,使得总的花费的时间最少

因为不管怎么样,交待所需要的n*bi的时间都是要花费的,

然后就让完成任务需要的时间长的人先做,就将j按降序排,更新每完成一个人的任务所花费的时间

 1 #include<iostream>  
 2 #include<cstdio>  
 3 #include<cstring> 
 4 #include <cmath> 
 5 #include<stack>
 6 #include<vector>
 7 #include<map> 
 8 #include<set>
 9 #include<queue> 
10 #include<algorithm>  
11 using namespace std;
12 
13 typedef long long LL;
14 const int INF = (1<<30)-1;
15 const int mod=1000000007;
16 const int maxn=10005;
17 
18 int n;
19 struct node{
20     int b,q;
21 } a[maxn];
22 
23 int cmp(node n1,node n2){
24     return n1.q > n2.q;
25 }
26 
27 int main(){
28     int kase = 0;
29     while(scanf("%d",&n) != EOF && n){
30         for(int i=1;i<=n;i++) scanf("%d %d",&a[i].b,&a[i].q);
31         sort(a+1,a+n+1,cmp);
32         
33         int beg = 0,end = 0,ans = 0;
34 
35         for(int i = 1;i <= n;i++){
36             beg += a[i].b; 
37             if(beg + a[i].q >= end)  end = beg + a[i].q;
38             
39         //    printf("i = %d  beg = %d  end = %d
",i,beg,end);
40         }
41         printf("Case %d: %d
",++kase,max(beg,end));
42     }
43     return 0;
44 }
View Code
原文地址:https://www.cnblogs.com/wuyuewoniu/p/4618440.html