51nod 1351 吃点心(贪心)

时间:2023-12-19 16:39:20

http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1351

题意:
51nod 1351 吃点心(贪心)

思路:

要么先选low值大的,要么先选high值大的,分两种情况讨论。

每次只要选了的low值和>=x或者c-未选的high值和>=x就肯定满足了。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
const int maxn = +; int n,c,x,lowtot,hightot; struct node
{
int low,high;
bool operator< (const node& rhs) const
{
return low > rhs.low;
}
}a[maxn]; bool cmp(node a, node b)
{
return a.high>b.high;
} int main()
{
//freopen("in.txt","r",stdin);
int T;
scanf("%d",&T);
while(T--)
{
lowtot = hightot = ;
scanf("%d%d%d",&n,&c,&x);
for(int i=;i<=n;i++)
{
scanf("%d%d",&a[i].low,&a[i].high);
lowtot += a[i].low;
hightot += a[i].high;
}
int sum = ;
int ans = ;
sort(a+,a+n+);
int tmp = hightot;
for(int i=;i<=n;i++)
{
sum += a[i].low;
ans++;
hightot -= a[i].high;
if(sum>=x || c-hightot>=x) break;
} sum = ;
int ans2 = ;
sort(a+,a+n+,cmp);
hightot = tmp;
for(int i=;i<=n;i++)
{
sum += a[i].low;
ans2++;
hightot -= a[i].high;
if(sum>=x || c-hightot>=x) break;
}
printf("%d\n",min(ans,ans2));
}
return ;
}