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\n",i,beg,end);
40         }
41         printf("Case %d: %d\n",++kase,max(beg,end));
42     }
43     return 0;
44 }

时间: 2024-08-05 01:16:56

UVa 11729 Commando War 【贪心】的相关文章

UVA 11729 Commando War (贪心)

题目链接:https://vjudge.net/problem/UVA-11729 一道比较显然的贪心. 我们可以发现如果我们让$a_j$最大的尽可能地往前来交待,那么时间重合地会更多. 一个很明显的贪心策略:按照$j$从大到小排序,记录每一次的$s$(交代的时间)和$s+a_j$(结束的时间),用结束的时间来更新$ans$. 证明其正确性:(蓝书 P4) 可以使用最常见的交换论证法: 假设我们交换相邻的两个任务$X$和$Y$,不难发现交换前后只会对$X$和$Y$有关. 情况一:交换之前,$X$

贪心 UVA 11729 Commando War

题目传送门 1 /* 2 贪心:按照执行时间长的优先来排序 3 */ 4 #include <cstdio> 5 #include <algorithm> 6 #include <iostream> 7 #include <cstring> 8 #include <string> 9 #include <cmath> 10 using namespace std; 11 12 const int MAXN = 1e3 + 10; 13

UVA 11729 Commando War

Commando WarInput: Standard InputOutput: Standard Output “Waiting for orders we held in the wood, word from the front never cameBy evening the sound of the gunfire was miles awayAh softly we moved through the shadows, slip away through the treesCross

UVA 11729 Commando War 突击战 【贪心】

题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=28436 贪心 按照工作执行时间的从长到短进行排序,之后计算总的执行时间(反证见刘汝佳P4) 计算方法: 先按照交代任务的时间依次累加,就是本次任务开始执行时的时间,这个时间加上任务完成的时间就是这次任务执行完毕后需要的总时间 这个总时间如果没有之前的总时间长的话表示这次任务的执行时间是包括在上次任务的执行时间之间的 超出的话就要更新总的任务执行时间 最后输出总的

UVA 11729 Commando War 题解

“Waiting for orders we held in the wood, word from the front never came By evening the sound of the gunfire was miles away Ah softly we moved through the shadows, slip away through the trees Crossing their lines in the mists in the fields on our hand

UVA 之11729 - Commando War

There is a war and it doesn't look very promising for your country. Now it's time to act. You have a commando squad at your disposal and planning an ambush on an important enemy camp located nearby. You have N soldiers in your squad. In your master-p

11729 - Commando War

11729 - Commando War 链接:https://uva.onlinejudge.org/external/117/11729.pdf 题意:有若干个战士需要分配任务,分配任务必须独立,完成任务也需要时间,问至少需要多少时间来完成所有任务. 题解:简单的贪心,因为分配任务的时间是固定的,所以只需要按照完成任务需要的时间排序即可. //但是被自己的疏忽强行喂屎,一开始双重for循环居然用了同一个i,这是怎么过样例的... 代码: #include<cstdio> #include&

[Water]UVA 11792 Commando War

n个部下,每个部下都要完成一个任务.每个任务需B时间交代,J时间执行. 不能同时给两个部下同时交代任务. 输出完成所有任务的最短时间. //Uva 11792 #include <algorithm> #include <iostream> #include <cstring> #include <cstdio> #include <string> #include <stack> #include <queue> #in

uva 11729

A B C D B - Commando War Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVA 11729 Appoint description:  System Crawler  (2014-11-25) Description G Commando War Input: Standard Input Output: Standard Outp