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?
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个站 每个站x人下,y人上车,问你情况是否可行。
保证车上人数大于等于下车人数就好
AC代码:
#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
using namespace std;
const int maxn = 100005;
typedef long long ll;
int n;
ll tot;
struct node
{
/* data */
int x, y;
}a[maxn];
bool cmp(node a, node b)
{
return a.x - a.y > b.x - b.y;
}
bool judge()
{
for(int i = 0; i < n; ++i) {
if(tot < a[i].x) return false;
tot -= a[i].y;
}
return true;
}
int main(int argc, char const *argv[])
{
while(scanf("%d", &n) != EOF) {
tot = 0;
for(int i = 0; i < n; ++i) {
scanf("%d%d", &a[i].x, &a[i].y);
tot += (a[i].x - a[i].y);
a[i].y = a[i].x - a[i].y;
}
sort(a, a + n, cmp);
if(judge()) printf("Yes\n");
else printf("No\n");
}
return 0;
}
探讨一个公交车乘客上下车问题,通过合理的站点调度确保车上人数始终合理。利用贪心策略解决复杂问题,实现有效的乘客管理。

被折叠的 条评论
为什么被折叠?



