Roundgod and Milk Tea(贪心)

Problem Description
Roundgod is a famous milk tea lover at Nanjing University second to none. This year, he plans to conduct a milk tea festival. There will be n classes participating in this festival, where the ith class has ai students and will make bi cups of milk tea.
Roundgod wants more students to savor milk tea, so he stipulates that every student can taste at most one cup of milk tea. Moreover, a student can’t drink a cup of milk tea made by his class. The problem is, what is the maximum number of students who can drink milk tea?
Input
The first line of input consists of a single integer T (1≤T≤25), denoting the number of test cases.
Each test case starts with a line of a single integer n (1≤n≤106), the number of classes. For the next n lines, each containing two integers a,b (0≤a,b≤109), denoting the number of students of the class and the number of cups of milk tea made by this class, respectively.
It is guaranteed that the sum of n over all test cases does not exceed 6×106.
Output
For each test case, print the answer as a single integer in one line.
Sample Input
1
2
3 4
2 1
Sample Output
3
一开始以为是网络流,问了问搞网络流的队友(今天他跑路了),说是数据量太大。。
只能硬着头皮去贪心着找。按着奶茶数由大到小排序(瞎猜。。)先把奶茶的总数记录下来,遍历到了一个班就把它的奶茶数减掉,然后还有记录有多少个人喝了奶茶,在遍历到一个班的时候就减去它。
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;

const int maxx=1e6+100;
struct node{
	ll num;
	ll cnt;
	bool operator <(const node &a)const
	{
		if(a.cnt!=cnt) return a.cnt<cnt;
		else return a.num<num;
	}
}p[maxx];
int n;

int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		ll sum1=0,sum2=0;
		scanf("%d",&n);
		for(int i=1;i<=n;i++) scanf("%lld%lld",&p[i].num,&p[i].cnt),sum1+=p[i].cnt;
		sort(p+1,p+1+n);
		ll ans=0;
		for(int i=1;i<=n;i++)
		{
			if(sum2)
			{
				if(sum2>p[i].cnt)
				{
					p[i].cnt=0;
					sum2-=p[i].cnt;
				}
				else
				{
					p[i].cnt-=sum2;
					sum2=0;
				}
			}
			sum1-=p[i].cnt;
			if(sum1>=p[i].num)
			{
				sum1-=p[i].num;
				ans+=p[i].num;
				sum2+=p[i].num;
			}
			else
			{
				p[i].num-=sum1;
				ans+=sum1;
				sum2+=sum1;
				sum1=0;
			}
			sum1+=p[i].cnt;
		}
		printf("%lld\n",ans);
	}
	return 0;
}

努力加油a啊,(o)/~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值