uva11729 - Commando War(贪心)

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

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<stack>
#include<queue>
#include<cctype>
#include<sstream>
using namespace std;
#define pii pair<int,int>
#define LL long long int
const int eps=1e-8;
const int INF=1000000000;
const int maxn=1000+10;
struct node
{
    int B,J;
}a[maxn];
int n,ans,cas=0;
bool cmp(node x,node y)
{
    return x.J>y.J;
}
int main()
{
    //freopen("in2.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    while(scanf("%d",&n)==1&&n)
    {
        for(int i=0;i<n;i++)
        {
            scanf("%d%d",&a[i].B,&a[i].J);
        }
        sort(a,a+n,cmp);
        ans=a[0].B+a[0].J;
        int t=a[0].B+a[0].J;
        for(int i=1;i<n;i++)
        {
            t+=a[i].B+a[i].J-a[i-1].J;//t表示每一“横条”的用时
            ans=max(ans,t);

        }
        printf("Case %d: %d\n",++cas,ans);
    }
    //fclose(stdin);
    //fclose(stdout);
    return 0;
}
时间: 2024-08-22 21:47:17

uva11729 - Commando War(贪心)的相关文章

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

UVA 11729 Commando War (贪心)

题目链接:https://vjudge.net/problem/UVA-11729 一道比较显然的贪心. 我们可以发现如果我们让$a_j$最大的尽可能地往前来交待,那么时间重合地会更多. 一个很明显的贪心策略:按照$j$从大到小排序,记录每一次的$s$(交代的时间)和$s+a_j$(结束的时间),用结束的时间来更新$ans$. 证明其正确性:(蓝书 P4) 可以使用最常见的交换论证法: 假设我们交换相邻的两个任务$X$和$Y$,不难发现交换前后只会对$X$和$Y$有关. 情况一:交换之前,$X$

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

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

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 field

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

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

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