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 soldiers in your squad. In your
master-plan, every single soldier has a unique responsibility and you don't want any of your soldier to know the plan for other soldiers so that everyone can focus on his task only. In order to enforce this, you brief every individual soldier about his tasks
separately and just before sending him to the battlefield. You know that every single soldier needs a certain amount of time to execute his job. You also know very clearly how much time you need to brief every single soldier. Being anxious to finish the total
operation as soon as possible, you need to find an order of briefing your soldiers that will minimize the time necessary for all the soldiers to complete their tasks. You may assume that, no soldier has a plan that depends on the tasks of his fellows. In other
words, once a soldier  begins a task, he can finish it without the necessity of pausing in between.

 

Input

 

There will be multiple test cases in the input file. Every test case starts with an integer N (1<=N<=1000), denoting the number of soldiers. Each of the following N lines describe a soldier with two integers B (1<=B<=10000) J
(1<=J<=10000)
seconds are needed to brief the soldier while completing his job needs seconds. The end of input will be denoted by a case with N =0 . This case should not be processed.

 

Output

 

For each test case, print a line in the format, “Case X: Y”, where X is the case number & Y is the total number of seconds counted from the start of your first briefing till the completion of all jobs.

 

Sample Input                                               Output for Sample Input


3

2 5

3 2

2 1

3

3 3

4 4

5 5

0


Case 1: 8

Case 2: 15

 


【题目翻译】:

【思路】:

贪心算法:处理时间长的先交代。按照J从大到小的顺序给任务排序,依次交代。

【代码】:

[cpp] view
plain
copy

  1. /********************************* 
  2. *   日期:2013-4-20 
  3. *   作者:SJF0115 
  4. *   题号: 题目11729 - Commando War 
  5. *   来源:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=117&page=show_problem&problem=2829 
  6. *   结果:AC 
  7. *   来源:UVA 
  8. *   总结: 
  9. **********************************/  
  10. #include<stdio.h>  
  11. #include<stdlib.h>  
  12.   
  13. typedef struct Time{  
  14.     //交代时间  
  15.     int B;  
  16.     //处理时间  
  17.     int J;  
  18. }Time;  
  19.   
  20. //排序函数  
  21. int cmp(const void *a,const void *b){  
  22.     struct Time * c = (Time *)a;  
  23.     struct Time * d = (Time *)b;  
  24.     return d->J - c->J;  
  25. }  
  26.   
  27. Time T[1001];  
  28.   
  29. int main ()  
  30. {  
  31.     int i,N,Case = 1;  
  32.     //freopen("C:\\Users\\XIAOSI\\Desktop\\acm.txt","r",stdin);    
  33.     while(scanf("%d",&N) != EOF && N != 0){  
  34.         //N个士兵  
  35.         for(i = 0;i < N;i++){  
  36.             scanf("%d %d",&T[i].B,&T[i].J);  
  37.         }  
  38.         //按处理时间排序  
  39.         qsort(T,N,sizeof(T[0]),cmp);  
  40.         int startTime = 0;  
  41.         int endTime = 0;  
  42.         for(i = 0;i < N;i++){  
  43.             //开始执行时间  
  44.             startTime += T[i].B;  
  45.             //最晚结束时间  
  46.             if(endTime < T[i].J + startTime){  
  47.                 endTime = T[i].J + startTime;  
  48.             }  
  49.         }  
  50.         printf("Case %d: %d\n",Case,endTime);  
  51.         Case++;  
  52.     }  
  53.     return 0;  
  54. }  
  55.   
  56.       

时间: 2024-11-27 23:26:21

UVA 之11729 - Commando War的相关文章

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

点击打开链接uva 11729 思路:贪心 分析: 1 给定n个人的交待任务的时间和完成任务的时间,要求不能同时给两个人交代任务,但是可以多人同时去做任务,求最短的完成任务的时间 2 根据贪心的原则,我们知道执行时间比较长的任务必须先交待,于是我们只要对这n个任务按照完成任务的时间进行排序,然后枚举n个人进去求解即可. 代码: #include<cstring> #include<cstdio> #include<iostream> #include<algori

UVA之409 - Excuses, Excuses!

 Excuses, Excuses!  Judge Ito is having a problem with people subpoenaed for jury duty giving rather lame excuses in order to avoid serving. In order to reduce the amount of time required listening to goofy excuses, Judge Ito has asked that you write

UVa 402 M*A*S*H (STL&amp;amp;list)

402 - M*A*S*H Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=343 Corporal Klinger is a member of the 4077th Mobile Army Surgical Hospital in the Korean W

UVa 409 Excuses, Excuses! (字符串匹配)

409 - Excuses, Excuses! Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=96&page=show_problem&problem=350 Judge Ito is having a problem with people subpoenaed for jury duty giving rath

刘汝佳uva 字符串专题

第一题   palindrome 点击打开链接uva 401 题目意思:给定一个字符串判断是什么类型 分析: 1 根据输出我们知道这个字符串总共有4种类型 2 首先应该是否是"palindrome ",判断的理由很简单直接对这个字符串进行判断,但是这里有个地方会出错就是'0'和'O',题目明确说明了'0'和'O'看成相同,所以我们应该在输入的时候就把所有的'0'处理成'O',注意这里不能把'O'改成'0'(想想为什么?) 3 接下来判断是否是"mirrored string&

UVa 10602

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1543 类型:贪心 原题: Company Macrohard has released it's new version of editor Nottoobad, which can understand a few voice commands.

UVa 10392 Factoring Large Numbers:素因子分解

10392 - Factoring Large Numbers Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=100&page=show_problem&problem=1333 One of the central ideas behind much cryptography is that factoring

UVa 10182 Bee Maja:规律&amp;amp;O(1)算法

10182 - Bee Maja Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1123 Maja is a bee. She lives in a bee hive with thousands of other bees. This bee hive c