Command War(Uva 11729)贪心算法应用

来源:互联网 发布:怎么缴费过期域名 编辑:程序博客网 时间:2024/06/10 05:46

“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”

Problem Description

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

题目大体意思是:
有N个士兵,每一个士兵都有一个特殊的不同于其他人的任务,你不希望士兵了解别人的任务以免他在执行自己的任务时分心。所以每一个士兵的任务都要单独交代,每一个士兵执行完自己的任务需要固定的时间 假设为J[i],你也知道交代给每一个士兵任务所需要的时间 假设为B[i],假设士兵在被交代完任务后就开始无间断地执行知道任务完成,现在需要你找出一种发式交代任务使所有的任务都尽早执行完毕;
输入格式:
输入包含多组数据,每组数据的第一行为士兵的数目N(1<=N<=1000);以下的N行每行两个正数分别是B(1<=B<=10 000) 和 J(1<=B<=10 000),结束标志0;
输出格式:
对于每组数据输出所有任务完成的最短时间;

解答:

本题利用谈心算法解决:

#include <cstdio>#include <vector>#include <algorithm>using namespace std;struct Job{    int j,b;    bool operator < (const Job& x)const{ //oprator override        return j > x.j;    }};int main(){    int n;    int b;    int j;    int kase = 1;    while(scanf("%d", &n) == 1 && n){   //not the end of the input        vector<Job> v;        for(int i = 0; i<n; i++){            scanf("%d%d", &b, &j);            v.push_back((Job){j,b});        }        sort(v.begin(), v.end());        int s = 0;        int ans = 0; //answer        for(int i = 0; i<n; i++){            s += v[i].b;            ans = max(ans, s+v[i].j);        }        printf("Case %d: %d\n", kase++, ans);    }    return 0;}

注释:
以上代码中,自定义了一个结构体并且重载了<运算符,使得JobVector中,经过sort函数排序以后,按照执行时间J的长短降序排列,也就是执行时间长的任务先交代;
计算最终答案的地方,s记录的是到当前任务所有任务总共交代的时间长度,也就是当前任务开始执行的时刻数值,用它加上当前任务的执行时间和ans比较,取比较大的为ans的新值;

关于为什么这里选择使用贪心以及贪心算法在该问题上的正确性证明以后再更新;

0 0