Roundgod and Milk Tea (HTU-6667)

Roundgod and Milk Tea (HTU-6667)


HTU-6667

题目

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?

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≤10^6), the number of classes. For the next n lines, each containing two integers a,b (0≤a,b≤10^9), 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×10^6.

For each test case, print the answer as a single integer in one line.

样例输入

1
2
3 4
2 1

样例输出

3

题目大意

奶茶节有n个班参加(1≤n≤10^6),第i个班有 a[i] 学生,他们将制作 b[i] 杯奶茶。(0≤a[i],b[i]≤10^9)每个学生最多只能品尝一杯奶茶。此外,学生不能喝班上做的奶茶。最多有多少学生可以喝奶茶?(有T组测试样例!(1≤T≤25))

题目解析

之前消费的奶茶需要判断一下,因为他们虽然是等价的,但是呢对于第i的班级的时候,他有自身的制约因素,就是假设之前的奶茶都是消耗别的班级的,那么剩下给这个班级的就会比实际情况剩余的少,因为自己班级的不可以消耗自己生产的奶茶,所以需要进行一下处理,我们可以假设之前消耗的奶茶都是当前这个班级所生产的,那么可以给这个班级使用的奶茶就会变得更多。解题更加方便。

代码

#include<iostream>
#include<cstdio>
#include<fstream>
#include<algorithm>
#include<cmath>
#include<deque>
#include<vector>
#include<queue>
#include<string>
#include<cstring>
#include<map>
#include<stack>
#include<set>
#include<cstdlib>
#include<bitset>
#include<climits>
#include<list>
#include<iomanip>
#include<ctime>
#include<functional>
#include<sstream>
#include<deque>
#define maxn 100010
#define INF 0xffffff
using namespace std;
typedef long long ll;
const int N=1e6+10;
ll a[N],b[N];
int n;
int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d",&n);
		ll sb=0,ans=0;
		for(int i=1;i<=n;i++)
		{
			scanf("%lld%lld",&a[i],&b[i]);
			sb+=b[i];
		}
		for(int i=1;i<=n;i++)
		{
			ll t=max(b[i]-ans,ll(0));
			ll z=min(sb-t,a[i]);
			ans+=z;
			sb-=z;
		}
		printf("%lld\n",ans);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/cxkdad/article/details/107289501