Roundgod and Milk Tea
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
题目大意:
n个班级,每个班级有a个人,然后可以制作b杯奶茶,然后问在本班人不能喝本班制作奶茶的前提下,最多可有多少人可以喝到奶茶
分析:
这算是一个极端的方法吧,就是我们每次去假设其他的班级喝不到奶茶,从第一个班开始(按照输入的顺序,不用排序),先求出所有的奶茶数目,然后去比较min(当前班级人数,总的奶茶数目-当前班级制作的奶茶数目),这样就保证了本班人是不会喝到本班制作的奶茶的,这样一来的话,就可以去将这个答案加到ans里;
但是这样存在一个问题,最后的答案啊可能是多于总的制作奶茶数量,因此,你需要去输出min(ans,总的奶茶数目);
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<string>
#include<cmath>
#include<cstring>
#include<set>
#include<queue>
#include<stack>
#include<map>
#define rep(i,a,b) for(int i=a;i<=b;i++)
typedef long long ll;
using namespace std;
const int N=1e6+10;
const int INF=0x3f3f3f3f;
struct node{
ll a,b;
}num[N];
//ll sum[N];
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif // ONLINE_JUDGE
int T;
scanf("%d",&T);
while(T--){
int n;
scanf("%d",&n);
ll sum=0;
rep(i,1,n) scanf("%lld%lld",&num[i].a,&num[i].b),sum+=num[i].b;
ll pre=0,ans=0;
for(int i=1;i<=n;i++){
pre= min(sum-num[i].b,num[i].a);
ans+=pre;
}
printf("%lld\n",min(ans,sum));
}
return 0;
}