ACDream 1734 Can you make a water problem?(贪心)

Problem Description
Losanto want to make a water problem. But he have no idea….
Then he thought a problem: A bus arrived at a station, and there were x persons got off the bus, then y persons got on the bus. And there were r persons(include the driver) on the bus before the bus arrived at next station. How many person on the bus initially? The driver can’t get on or off the bus. There is only 1 driver in the bus.
But if the x=8 , y=5 r=4, there are something wrong in the problem. After got on 5 persons and there are 4 persons rest…What a pity! Losanto give you x, y, r want know whether the problem is OK.
Of course few buses has only one station, so there are n stations.
Now give you x and y for every station, and there is only one person (the driver) rest after the last station. Can you find a suitable order for the station to make the problem OK?
Input

There are several cases.

For each case, First line there is a n, indicate n stations. Then n lines followed, each line has two numbers xi and yi. To make the problem easier, we guarantee that xi>=yi.(0<n<100000  0<=xi,yi<1000000)
Output
For each cases, output one line with “Yes” (if you can make an OK order) or “No” (if you can’t).
Sample Input
2
5 0
7 4
2
5 2
3 3
Sample Output
Yes
No


题意:n个车站,每个车站信息:下车xi人,上车yi人  【隐含条件是在下个车站之前车上有r个】

思路:反推在第i个站之前考虑车上的人数,只要考虑每次的上下车前的车上人数是否满足之前的下车的人数。题目只给你的信息是每个车站并没有说是固定的第几个车站,可以按照那个上车的人数从大到小进行排序,然后从最后一个车站反推到第一个车站就行。。注意这题不要把司机考虑进去。


下面是AC代码:

#include<iostream>
#include<algorithm>
using namespace std;
struct node {//存储车站的信息
    int x;
    int y;
}a[100005];
int cmp(node aa,node bb)//从大到小进行排序
{
    return aa.y > bb.y;
}
int main()
{
    int n;
    while(cin>>n)
    {
        for(int i=1;i<=n;i++)
        {
            cin>>a[i].x>>a[i].y;
        }
        sort(a+1,a+n+1,cmp);
        long long sum=0;
        bool flag=false;
        for(int i=n;i>=1;i--)
        {
            sum=sum+a[i].x-a[i].y;
            if(sum<a[i].x)
            {
                flag=true;
                break;
            }
        }
        if(!flag)
        {
            cout<<"Yes"<<endl;
        }
        else
        {
            cout<<"No"<<endl;
        }
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值