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
 
解题报告: 这道题目一上来想使用匹配来求解,后来发现不适合,然后就开始考虑思维,考虑的是这些奶茶他在一定意义上是等价的,然后咱们只要是把自己班级的奶茶给抛出去进行判断处理就可以,每次比较当前剩余的奶茶(抛出自己班级生产的)和第i个班级所需要的奶茶,看起来是没有什么问题的,但是呢,确是wa了,后来队友给我解答疑惑,就是咱们之前消费的奶茶需要判断一下,因为他们虽然是等价的,但是呢在对于第i的班级的时候,他有自身的制约因素,就是假设之前的奶茶都是消耗的别的班级的,那么剩下给这个班级的就会比实际情况剩余的少,因为自己班级的不可以消耗自己生产的奶茶,所以需要进行一下处理,那么就假设之前消耗的奶茶都是当前这个班级所生产的,那么留下了可以给这个班级使用的奶茶就会变得更多。
 
ac代码:
 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 }
 

猜你喜欢

转载自www.cnblogs.com/Spring-Onion/p/11354728.html