11729 - Commando War

11729 - Commando War

链接:https://uva.onlinejudge.org/external/117/11729.pdf

题意:有若干个战士需要分配任务,分配任务必须独立,完成任务也需要时间,问至少需要多少时间来完成所有任务。

题解:简单的贪心,因为分配任务的时间是固定的,所以只需要按照完成任务需要的时间排序即可。

//但是被自己的疏忽强行喂屎,一开始双重for循环居然用了同一个i,这是怎么过样例的。。。

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<cstdlib>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn=1000+100;
vector<int>t;
struct node
{
    int x,y;
}p[maxn];
int cmp(struct node a,struct node b)
{
    return a.y>b.y;
}
int main()
{
    int n,kase=0;
    while(cin>>n&&n)
    {
        t.clear();
    for(int i=0;i<n;i++)
        cin>>p[i].x>>p[i].y;
    int ans=0;
    sort(p,p+n,cmp);
    cout<<"Case "<<++kase<<": ";
    ans+=p[0].x;
    t.push_back(p[0].y);
    for(int i=1;i<n;i++)
    {
        ans+=p[i].x;
        for(int j=0;j<t.size();j++)
        {
            if(t[j]<=p[i].x)
                t[j]=0;
            else t[j]-=p[i].x;
        }
        t.push_back(p[i].y);
    }
    int maxx=-1;
    for(int i=0;i<t.size();i++)
        maxx=max(t[i],maxx);
    ans+=maxx;
    cout<<ans<<endl;
    }
    return 0;
}

时间: 2024-12-22 19:20:31

11729 - Commando War的相关文章

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

题目传送门 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

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

“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) 计算方法: 先按照交代任务的时间依次累加,就是本次任务开始执行时的时间,这个时间加上任务完成的时间就是这次任务执行完毕后需要的总时间 这个总时间如果没有之前的总时间长的话表示这次任务的执行时间是包括在上次任务的执行时间之间的 超出的话就要更新总的任务执行时间 最后输出总的

UVa 11729 Commando War 【贪心】

题意:有n个部下,交待每个部下完成他相应的任务需要bi的时间,然后完成这项任务需要ji的时间, 选择交待任务的顺序,使得总的花费的时间最少 因为不管怎么样,交待所需要的n*bi的时间都是要花费的, 然后就让完成任务需要的时间长的人先做,就将j按降序排,更新每完成一个人的任务所花费的时间 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include <cmath> 5 #inclu

UVA 11729 Commando War (贪心)

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

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

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