Commando War

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 hands and our knees

And all that I ever, was able to see

The fire in the air, glowing red, silhouetting the smoke on the breeze”

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-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). B seconds are needed to brief the soldier while completing his job needs J 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

Sample Output

Case 1: 8

Case 2: 15

此题仍然是采用贪心法,将每个偶序按照 Job Seconds 为关键字从大到小排序。

下证贪心法正确性。

证:

首先,任两偶序之间交换不影响两偶序的前面或后面的偶序的Brief开始时间。

其次,任两偶序之间交换对两偶序的completion时间的影响如下

分两种情形讨论:

设X、Y为两偶序,B[i]、J[i]分别为Brief Seconds、Job Seconds 默认一开始X先于Y。

1.若J[X]>(B[Y]+J[Y]),则交换只能无谓的增加两偶序的completion时间,交换无意义。

2.若J[X]<(B[Y]+J[Y]),则交换前两偶序的completion时间为B[X]+B[Y]+J[Y]

交换后两偶序的completion时间为B[Y]+B[X]+J[X]

不难发现,此情形下两偶序的completion时间大小仅与J[X]、J[Y]的大小有关。

综上所述,贪心策略为:若J[X]>=J[Y],X偶序排在Y偶序之前最优。

#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;

int max(int a,int b)
{
    return a>b?a:b;
}

struct Job
{
    int j, b;
    bool operator < (const Job& x) const
    {
        return j > x.j;
    }
};
int main()
{
    int n, b, j, kase = 1;
    while(cin>>n && n)
    {
        vector<Job>v;
        for(int i = 0; i < n; i++)
        {
            cin>>b>>j;
            Job job;
            job.j = j , job.b = b;
            v.push_back( job );
        }
        sort(v.begin(), v.end());
        int s = 0;
        int ans = 0;
        for(i = 0; i < n; i++)
        {
            s += v[i].b;
            ans = max(ans,s+v[i].j);
        }
        cout<<ans<<endl;
    }
    return 0;
}
时间: 2024-10-09 20:14:27

Commando War的相关文章

11729 - Commando War

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

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

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 (突击战争)

G Commando War Input: Standard Input Output: Standard Output "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 tree

B - Commando War

Description G Commando War Input: Standard Input Output: Standard Output “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 thro

贪心 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 题解

“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 突击战 【贪心】

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

UVa11729 - Commando War

解题思路:很明显,执行时间较长的任务应该先交代.按照j从大到小的顺序,   如果j相同,b的大小是不影响的. 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 const int maxn = 1005; 6 #define inf 0x3f3f3f3f 7 8 struct node{ 9 int b, j; 10 }p[maxn]; 11 12