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 (1T25), denoting the number of test cases.

Each test case starts with a line of a single integer n (1n106), the number of classes. For the next n lines, each containing two integers a,b (0a,b109), 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
 
Report problem solving: This question is a match up you want to use to solve, and later found to be unsuitable, then start thinking about thinking, these tea in a sense he is considered equivalent, then we as long as their own class tea to throw to the judging process can be a time to compare the current surplus of tea (throw your own class production) and tea i-th classes need, it seems to be no problem, but it indeed is a wa and later his teammates gave me answers to your questions, that is, we need to determine the consumption of tea before it, because although they are equivalent, but it in class for the i-th time, he has its own constraints, that is, before the tea assumptions is consumed by the other classes, then left for the rest of the class would be less than the actual situation, because of his class can not consume their own production of tea, so it needs to be processed, then it is assumed that all tea consumed before this class is currently being produced, it can leave the class to use the tea will become more.
 
ac Code:
 1 #include<cstdio>
 2 #include<iostream>
 3 using namespace std;
 4 typedef long long ll;
 5 const int N=1e6+10;
 6 ll a[N],b[N];
 7 int n;
 8 int main()
 9 {
10     int T;
11     scanf("%d",&T);
12     while(T--)
13     {
14         scanf("%d",&n);
15         ll sb=0,ans=0;
16         for(int i=1;i<=n;i++)
17         {
18             scanf("%lld%lld",&a[i],&b[i]);
19             sb+=b[i];
20         }
21         for(int i=1;i<=n;i++)
22         {
23             ll t=max(b[i]-ans,ll(0));
24             ll z=min(sb-t,a[i]);
25             ans+=z;
26             sb-=z;
27         }
28         printf("%lld\n",ans);
29     }
30     return 0;
31 }

 

 

Guess you like

Origin www.cnblogs.com/Spring-Onion/p/11354728.html