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 }
相關文章
相關標籤/搜索