HDU 6664 Roundgod and Milk Tea(easy)

Describe

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(学生做的奶茶数),然后一个学生不能喝自己班的奶茶尽管有点作,求最多有多少学生能喝到奶茶。其中一个同学只能喝一杯奶茶。

非常草率的分析

首先肯定是想到贪心,但是贪心的策略呢?(蒟蒻和某大佬一起想了半个下午也没想出贪心策略)于是在神犇的讲解下,决定水一篇题解

比较有用的分析

首先有一个hall’s margia 定理,大意就是如果对于一个二分图,A集合内任意一个子集,在B中的对应元素个数大于等于这个子集的大小,则这个二分图必有匹配。

现在,对于任意一个班级i,有A集合{i班级的学生}和B集合{其他班级的奶茶},然后将这个二分图匹配,而这个二分图是完全的,即A中所有点都与B中各点相连。这样一来,只要A的大小小于等于B,就可以实现完全匹配(可以自己画图理解)。

解题

如此一来,就可以算出肯定不能喝到奶茶的人数(max(A.size()-B.size(),0)),由此可以推得有希望喝到奶茶的人数,然后与总人数取min即可。

代码

#include<algorithm>
#include<iostream>
#include<cstring>
#include<iomanip>
#include<cstdio>
#include<string>
#include<vector>
#include<cmath>
#include<queue>
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define inf 1<<30
using namespace std;
const int MAXN=1e6+10;
struct node{
	ll a,b;
}c[MAXN];
int main()
{
	ll T,n,sum,ans;
	scanf("%lld",&T);
	while(T--){
		scanf("%lld",&n);sum=ans=0;
		for(int i=1;i<=n;i++){
			scanf("%lld%lld",&c[i].a,&c[i].b);
			sum+=c[i].a;//总人数
		}
		for(int i=1;i<=n;i++)
			ans+=min(c[i].b,sum-c[i].a);
		//这里用被喝的奶茶数代替喝的学生
		//可以通过原代码推
		printf("%lld\n",min(ans,sum));
	}
}

害到这里就结束了,欢迎指正!

猜你喜欢

转载自blog.csdn.net/zhangchizc/article/details/107288858