Roundgod and Milk Tea(HDU-6667)

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 (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

题意:t 组数据,每组数据给出一个整数 n 代表有 n 个班级,每个班级有 a[i] 个学生,制作了 b[i] 杯奶茶,现在要求每个学生最多喝一杯奶茶,而且每个学生不能喝自己班的奶茶,问最多有多少个学生能喝到奶茶

思路:

第一反应最大流,但数据太大了,不能用网络流的方法来写

仔细想想,用贪心就可以做

首先记录所有的奶茶和已经喝掉的奶茶,然后直接从前往后处理,每次以班为单位去考虑,计算每一个班最多可以喝多少杯奶茶,然后从剩余的奶茶里减去本班的奶茶数,再进行统计即可

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<unordered_map>
#include<bitset>
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
#define Pair pair<int,int>
LL quickPow(LL a,LL b){ LL res=1; while(b){if(b&1)res*=a; a*=a; b>>=1;} return res; }
LL multMod(LL a,LL b,LL mod){ a%=mod; b%=mod; LL res=0; while(b){if(b&1)res=(res+a)%mod; a=(a<<=1)%mod; b>>=1; } return res%mod;}
LL quickMultPowMod(LL a, LL b,LL mod){ LL res=1,k=a; while(b){if((b&1))res=multMod(res,k,mod)%mod; k=multMod(k,k,mod)%mod; b>>=1;} return res%mod;}
LL quickPowMod(LL a,LL b,LL mod){ LL res=1; while(b){if(b&1)res=(a*res)%mod; a=(a*a)%mod; b>>=1; } return res; }
LL getInv(LL a,LL mod){ return quickPowMod(a,mod-2,mod); }
LL GCD(LL x,LL y){ return !y?x:GCD(y,x%y); }
LL LCM(LL x,LL y){ return x/GCD(x,y)*y; }
const double EPS = 1E-10;
const int MOD = 1000000000+7;
const int N = 1000000+5;
const int dx[] = {0,0,-1,1,1,-1,1,1};
const int dy[] = {1,-1,0,0,-1,1,-1,1};
using namespace std;

LL num[N],tea[N];
int main() {
    int t;
    scanf("%d",&t);
    while (t--) {
        int n;
        scanf("%d",&n);
        LL sum = 0;
        for (int i = 1; i <= n; i++) {
            scanf("%lld%lld", &num[i], &tea[i]);
            sum += tea[i];
        }

        LL res = 0;
        for (int i = 1; i <= n; i++) {
            LL maxx = sum - (max(0LL, tea[i] - res)); //可以喝的数量
            LL cnt = min(maxx, num[i]); //当前班最多喝多少
            sum -= cnt;
            res += cnt;
        }
        printf("%lld\n", res);
    }
    return 0;
}
发布了1871 篇原创文章 · 获赞 702 · 访问量 194万+

猜你喜欢

转载自blog.csdn.net/u011815404/article/details/102535823