您的位置:首页 > 其它

51nod 1351 吃点心(贪心)

2017-12-14 21:07 127 查看
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1351
题意:



思路:

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

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

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
const int maxn = 50+5;

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 = 0;
scanf("%d%d%d",&n,&c,&x);
for(int i=1;i<=n;i++)
{
scanf("%d%d",&a[i].low,&a[i].high);
lowtot += a[i].low;
hightot += a[i].high;
}
int sum = 0;
int ans = 0;
sort(a+1,a+n+1);
int tmp = hightot;
for(int i=1;i<=n;i++)
{
sum += a[i].low;
ans++;
hightot -= a[i].high;
if(sum>=x || c-hightot>=x)  break;
}

sum = 0;
int ans2 = 0;
sort(a+1,a+n+1,cmp);
hightot = tmp;
for(int i=1;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 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: