题解 | #「土」巨石滚滚#
「土」巨石滚滚
https://ac.nowcoder.com/acm/problem/53681
#include <iostream>
#include <algorithm>
using namespace std;
struct q{
int a;
int b;
int d;
} p[500010];
bool cmp1(q a, q b)
{
return a.d > b.d;
}
bool cmp2(q a, q b)
{
return a.a < b.a;
}
bool cmp3(q a, q b)
{
return a.b > b.b;
}
int n;
int main()
{
int t;
cin >> t;
while(t --)
{
long long x;
int flag = 0, cnt = 0;
cin >> n >> x;
for(int i = 1; i <= n; i++)
{
cin >> p[i].a >> p[i].b;
p[i].d = p[i].b - p[i].a;
if(p[i].d > 0) cnt ++;
}
sort(p + 1, p + n + 1, cmp1);
sort(p + 1, p + cnt + 1, cmp2);
sort(p + cnt + 1, p + n + 1, cmp3);
for(int i = 1; i <= n; i++)
{
x -= p[i].a;
if(x < 0) {cout << "No" << endl; flag = 1; break;}
x += p[i].b;
}
if(flag == 0)
cout << "Yes" << endl;
}
return 0;
}
当delta > 0 时排序方法显然 当delta < 0 时需要令b大的在前
可从AB可以但BA不行下手