ACM: 贪心题 uva 11729

来源:互联网 发布:电脑网络连接显示未知 编辑:程序博客网 时间:2024/05/23 10:36
   CommandoWar

“Waiting for orders we held in the wood, word from the front nevercame
By evening the sound of the gunfire was miles away
Ah softly we moved through the shadows, slip away through thetrees
Crossing their lines in the mists in the fields on our hands andour knees
And all that I ever, was able to see
The fire in the air, glowing red, silhouetting the smoke on thebreeze”

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 disposaland planning an ambush on an important enemy camp located nearby.You have N soldiers in your squad. In your master-plan, everysingle soldier has a unique responsibility and you don't want anyof your soldier to know the plan for other soldiers so thateveryone can focus on his task only. In order to enforce this, youbrief every individual soldier about his tasks separately and justbefore sending him to the battlefield. You know that every singlesoldier needs a certain amount of time to execute his job. You alsoknow very clearly how much time you need to brief every singlesoldier. Being anxious to finish the total operation as soon aspossible, you need to find an order of briefing your soldiers thatwill minimize the time necessary for all the soldiers to completetheir tasks. You may assume that, no soldier has a plan thatdepends on the tasks of his fellows. In other words, once asoldier  begins a task, he can finish it withoutthe necessity of pausing in between.

Input
There will be multiple test cases in the input file. Every testcase starts with an integer N(1<=N<=1000), denoting the number ofsoldiers. Each of the following N lines describe a soldier with twointegers B (1<=B<=10000)& J (1<=J<=10000). Bseconds are needed to brief the soldier while completing his jobneeds 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”, whereX is the case number & Y is the total number ofseconds counted from the start of your first briefing till thecompletion of all jobs.
 
Sample Input
3
2 5
3 2
2 1
3
3 3
4 4
5 5
  
Output for Sample Input
Case 1: 8
Case 2: 15

题意: 现在你有N个任务需要完成, 有足够的士兵, 你分析你的问题给他需要Bi时间, 他执行的时间Ji时间;
     现在要你安排先后顺序使得全部任务完成时间最短是多少?

解题思路:
     1. 贪心题, 将设现在又2个任务a,任务b; 顺序是ab. 显然要确定它们是否需要交换, 使时间更少.
        假设a是任务时间执行比b任务完成要长. ab: B[a]+J[a];  ba:B[b]+B[a]+J[a]; ab优于ba;
        假设a是任务时间执行比b任务完成要短. ab: B[a]+B[b]+J[b];  ba:B[b]+B[a]+J[a];
        要使得ab优于ba:  J[b] < J[a]; 即:先执行的任务要执行时间长.
     2. 排序之后计算即可.

代码:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstring>
using namespace std;
#define MAX 1005

struct node
{
    int B,J;
    node(int b,int j){ B = b; J = j; }
    booloperator <(const node &a)const
    {
       return J> a.J;
    }
};

int n;

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

int main()
{
    int i;
    int num =1;
//   freopen("input.txt", "r", stdin);
   while(scanf("%d", &n)  !=EOF)
    {
       if(n == 0)break;

       vectorp;
       int B,J;
       for(i = 0; i< n; ++i)
       {
          scanf("%d%d", &B, &J);
          p.push_back(node(B,J) );
       }

      sort(p.begin(), p.end());
       int sum =0;
       int temp =0;
       for(i = 0; i< n; ++i)
       {
          temp +=p[i].B;
          sum =max(sum, temp+p[i].J);
       }

       printf("Case%d: %d\n", num++, sum);
    }
    return0;
}

0 0