HDU 6664 Roundgod and Milk Tea(easy)

本文探讨了一种在特定条件下最大化学生品尝奶茶数量的算法。通过分析班级学生数和奶茶数,利用Hall's Marriage定理,设计了二分图匹配策略,以确保尽可能多的学生能够品尝到奶茶。

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));
	}
}

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

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值