uva 11729 突击队(训练思维+贪心)

本文介绍了一个简单的任务调度问题,通过贪心算法实现任务的最优执行顺序。具体做法是将执行时间长的任务安排在前面,执行时间短的任务放在后面。文章提供了完整的C++代码实现,并通过案例展示了算法的有效性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这个题也是水的不行,贪心算法,执行任务最长的在前面执行时间短的在后面,书上有详细的解释 我就不解释原因了

代码如下:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
struct node
{
    int u,v;
};
int n;
node no[1000+10];
bool cmp(node x, node y)
{
    return x.v > y.v;
}
int main()
{
    //freopen("s.txt","w",stdout);
    int ans =0;
    while(scanf("%d",&n)&&n)
    {
        //printf("%d\n",n);
        ans++;
        for(int i = 0; i < n ; i++)
        {
            scanf("%d%d",&no[i].u,&no[i].v);
        }
        sort(no,no+n,cmp);
        int maxx = 0,sum=0;
        for(int i =0 ;i < n ; i++)
        {
            sum += no[i].u;
            maxx = max(maxx,sum+no[i].v);
        }
        printf("Case %d: %d\n",ans,maxx);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值