题目
分析
很神奇的贪心
(Code)
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long LL;
const int N = 100005;
int n , T , cnt1 , cnt2;
LL v;
struct node{
int a , b;
}p[N] , q[N];
bool cmp1(node x , node y){return x.a < y.a;}
bool cmp2(node x , node y){return x.b < y.b;}
int main()
{
freopen("backpack.in" , "r" , stdin);
freopen("backpack.out" , "w" , stdout);
scanf("%d" , &T);
while (T--)
{
scanf("%d%lld" , &n , &v);
cnt1 = cnt2 = 0;
int fl = 1 , a , b;
for(register int i = 1; i <= n; i++)
{
scanf("%d%d" , &a , &b);
if (a <= b) p[++cnt1] = node{a , b};
else q[++cnt2] = node{a , b};
}
sort(p + 1 , p + cnt1 + 1 , cmp1);
for(register int i = 1; i <= cnt1; i++)
{
a = p[i].a , b = p[i].b;
if (v < a){fl = 0; break;}
v = v - a + b;
}
if (fl == 0){printf("No
"); continue;}
sort(q + 1 , q + cnt2 + 1 , cmp2);
for(register int i = 1; i <= cnt2; i++) v = v - q[i].a + q[i].b;
for(register int i = 1; i <= cnt2; i++)
{
a = q[i].a , b = q[i].b;
if (v < b){fl = 0; break;}
v = v - b + a;
}
if (fl == 0){printf("No
"); continue;}
printf("Yes
");
}
}