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  

3

2 5

3 2

2 1

3

3 3

4 4

5 5

0

Output for Sample Input

Case 1: 8

Case 2: 15

 题目大意:你有n个部下,每个部下需要完成一项任务。第i个部下需要你花Bi分钟交代任务,然后他会立刻独立地、无间断地执行Ji分钟后完成任务。你需要选择交代任务的顺序,使得所有任务尽早执行完毕(即最后一个执行完的任务应尽早结束)。注意,不能同时给两个部下交代任务,但部下们可以同时执行他们各自的任务。

分析:直觉告诉我们,执行时间较长的任务应该先交代。于是我们想到这样一个贪心算法:按照J从大到小的顺序给各个任务排序,然后依次交代。

 # include<cstdio>
# include<vector>
# include<algorithm>
using namespace std; struct Job{
int j,b;
bool operator < (const Job& x) const{ // 运算符重载。不要忘记const修饰符
return j>x.j; //从大到小排序
}
}; int main(){
int n,b,j,cas=;
int i;
while(scanf("%d",&n) && n){
vector<Job>v;
for(i=;i<n;i++){
scanf("%d%d",&b,&j);
v.push_back((Job){j,b});
}
sort(v.begin(),v.end()); //使用Job类自己的 < 运算符排序
int s=;
int ans=;
for(i=;i<n;i++){
s += v[i].b; //当前任务的开始执行时间
ans = max(ans,s+v[i].j); //更新任务执行完毕时的最晚时间
}
printf("Case %d: %d\n", cas++, ans);
}
return ;
}

上述代码直接提交可以通过测试。

可是为什么这样会对呢?假设我们交换两个相邻的任务X和Y(交换前X在Y之前,交换后Y在X之前),那么这两个任务的完成时间有没有影响?(相邻交换法)

情况一:交换之前,任务Y比X先结束,交换之后X的结束时间延后,Y的结束时间提前,最终答案不会变好。

情况二:交换之前X比Y先结束,因此交换后答案变好的充要条件是:交换后X的结束时间比交换前Y的结束时间要早(交换后Y的结束时间肯定变早了)。这个条件可以写成

B[Y]+B[X]+J[X]<B[X]+B[Y]+J[Y],化简得J[X]<J[Y]。这就是我们贪心的依据。

UVA 11729 - Commando War(贪心 相邻交换法)的更多相关文章

  1. UVa 11729 - Commando War(贪心)

    "Waiting for orders we held in the wood, word from the front never came By evening the sound of ...

  2. Uva 11729 Commando War (简单贪心)

    Uva 11729  Commando War (简单贪心) There is a war and it doesn't look very promising for your country. N ...

  3. 贪心 UVA 11729 Commando War

    题目传送门 /* 贪心:按照执行时间长的优先来排序 */ #include <cstdio> #include <algorithm> #include <iostrea ...

  4. [ACM_水题] 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 ...

  5. UVa 11729 Commando War 【贪心】

    题意:有n个部下,交待每个部下完成他相应的任务需要bi的时间,然后完成这项任务需要ji的时间, 选择交待任务的顺序,使得总的花费的时间最少 因为不管怎么样,交待所需要的n*bi的时间都是要花费的, 然 ...

  6. UVa 11729 - Commando War

    [题目翻译]: 题目分析:因为任务是可以并行的执行,所以直觉上是花费时间长的任务优先去部署.但是这到题目还给你交待任务的时间,所以容易让人想多了. 不管有没有交待任务的时间,对于任务x和y,只可能有两 ...

  7. UVa 11729 Commando War 突击战

    你有 n 个部下,每个部下需要完成一个任务.第 i 个部下需要你花 Bi 分钟交待任务,然后他会立刻独立地.无间断地执行 Ji 分钟后完成任务.你需要选择交待任务的顺序,使得所有任务尽早执行完毕(即最 ...

  8. cogs 1446. [Commando War,Uva 11729]突击战

    1446. [Commando War,Uva 11729]突击战 ★   输入文件:commando.in   输出文件:commando.out   简单对比时间限制:1 s   内存限制:64 ...

  9. uva11729 - Commando War(贪心)

    贪心法,执行任务的时间J越长的应该越先交待.可以用相邻交换法证明正确性.其实对于两个人,要让总时间最短,就要让同一时间干两件事的时间最长. #include<iostream> #incl ...

随机推荐

  1. IntelliJ 直接编辑国际化文件(properties)方法

    IntelliJ 直接编辑国际化文件(properties)方法 settings-File Encodings 右下角 Transparent native-to-ascii conversion的 ...

  2. Hadoop工程师面试题(1)--MapReduce实现单表汇总统计

    数据源格式描述: 输入t1.txt源数据,数据文件分隔符"*&*",字段说明如下: 字段序号 字段英文名称 字段中文名称 字段类型 字段长度 1 TIME_ID 时间(到时 ...

  3. linux和MAC下静态库.a文件合并

    这两天开发群了讨论了一个文体,如何合并众多的.a文件. 有人给了建议,使用ar 解.a,生成.o.然后合并所有的.o文件. 我在MAC OS下尝试了一下. 正好赶上编译了ffmpeg的IOS armv ...

  4. javascript的类和构造函数

    在javascript中,类的实现是基于其原型继承机制的.如果两个实例都从同一个原型对象上继承了属性,我们就说它们是同一个类的实例.那么,如果两个对象继承自同一个原型,那基本上可以认为它们是由同一个构 ...

  5. nyoj 27 水池数目

    水池数目 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 南阳理工学院校园里有一些小河和一些湖泊,现在,我们把它们通一看成水池,假设有一张我们学校的某处的地图,这个地 ...

  6. Robot Framework自动化测试(二)第一个用例

    RIDE启动界面: 首先创建一个Test project File-New Project ,选择Directory类型 在创建的文件夹上右键,创建一个Test Suite Openbaidu, NE ...

  7. [USACO10MAR]伟大的奶牛聚集

    [USACO10MAR]伟大的奶牛聚集 Bessie正在计划一年一度的奶牛大集会,来自全国各地的奶牛将来参加这一次集会.当然,她会选择最方便的地点来举办这次集会. 每个奶牛居住在 N(1<=N& ...

  8. Sqlite在Windows、Linux 和 Mac OS X 上的安装过程

    一:在 Windows 上安装 SQLite 1,下载 请访问SQLite下载页面http://www.sqlite.org/download.html,从Windows 区下载预编译的二进制文件.需 ...

  9. SQL Server数据库PIVOT函数的使用详解(一)

    http://database.51cto.com/art/201108/285250.htm SQL Server数据库中,PIVOT在帮助中这样描述滴:可以使用 PIVOT 和UNPIVOT 关系 ...

  10. dmesg 程序崩溃调试2

    dmesg命令基于缓冲区打印信息dmesg -c可以清除该内存信息清除后demsg 命令不显示任何信息,但可以到/var/log/dmesg查看信息 dmesg |tail 20dmesg |head ...