51 nod 1099 任务执行顺序(贪心)

来源:互联网 发布:iprint打印监控软件 编辑:程序博客网 时间:2024/05/20 15:40

http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1099

思路:贪心法,剩余空间最大的活动安排在前面。。即 r-o最大 的排前面。。

#include<iostream>#include<cstring>#include<stdio.h>#include<algorithm>#define maxn 100005using namespace std;struct node{    int r,o;};node n[maxn];bool cmp(node a,node b){    return a.r-a.o>b.r-b.o;}int main(){    long long n1,sum=0,sum1=0;    scanf("%lld",&n1);    for(int x=0;x<n1;x++)        scanf("%lld%lld",&n[x].r,&n[x].o);    sort(n,n+n1,cmp);    for(int x=0;x<n1;x++)    {        if(sum<=sum1+n[x].r) sum=sum1+n[x].r;        sum1+=n[x].o;    }    printf("%lld\n",sum);}
原创粉丝点击