码迷,mamicode.com
首页 > 其他好文 > 详细

贪心 No Time for Dragons Gym - 101149B

时间:2017-07-29 11:41:52      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:for   需要   poi   drag   main   pre   name   int   sort   

国王要杀死n条龙,杀死每条龙需要a人,会战死b人,问杀死n条龙至少需要多少人。

因为杀死每条龙需要的人是一定的且战死的人是一定的,所以先杀战死的人最少的龙。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 200005
using namespace std;
typedef struct
{
    int x,y;
}Point;
Point a[maxn];
int cmp(Point b,Point c)
{
    return b.y> c.y;
}
int main()
{
    long long ans=0;
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        int x1,y1;
        cin>>x1>>y1;
        a[i].x=x1,a[i].y=x1-y1;
    }
    sort(a+1,a+n+1,cmp);
    ans+=a[1].x;
    for(int i=2;i<=n;i++)
    {
        if(a[i].x>a[i-1].y)
         ans+=(a[i].x-a[i-1].y);
         else
             a[i].y+=(a[i-1].y-a[i].x);

    }
 cout<<ans<<endl;
}

 

贪心 No Time for Dragons Gym - 101149B

标签:for   需要   poi   drag   main   pre   name   int   sort   

原文地址:http://www.cnblogs.com/Twsc/p/7253851.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!