H - easy - Roundgod and Milk Tea

题目描述

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 nn classes participating in this festival, where the iith class has aiai students and will make bibi 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?

输入

The first line of input consists of a single integer TT (1≤T≤25)(1≤T≤25), denoting the number of test cases.

Each test case starts with a line of a single integer nn (1≤n≤106)(1≤n≤106), the number of classes. For the next nn lines, each containing two integers a,ba,b (0≤a,b≤109)(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 nn over all test cases does not exceed 6×1066×106.

输出

For each test case, print the answer as a single integer in one line.

样例输入

1
2
3 4
2 1

样例输出

3

思路

签到题,先是果断贪心。
在寻找最佳策略时,发现连贪心都不用了,先把每个班的奶茶能被多少人喝用s数组存下,用sum来累加每个班级奶茶数量和能被多少人喝的较小值(人家奶茶数量有限你总不能强行白嫖吧),最后输出学生总人数和sum的较小值即为答案
代码:

#include

using namespace std;

const int MAXN=1e6+10;
struct node
{
    long long st,na;
}a[MAXN];

bool cmp(node a,node b){return a.na<b.na;}

int T,n;
long long s[MAXN];

int main()
{
    scanf("%d",&T);
    while(T--)
	{
        scanf("%d",&n);
        long long ST=0,sum=0;
        for(int i=1;i<=n;i++)
		{
            scanf("%d%d",&a[i].st,&a[i].na);
            ST+=(long long)a[i].st;
        }
        for(int i=1;i<=n;i++) s[i]=ST-a[i].st,sum+=min(s[i],a[i].na);
        printf("%lld\n",min(sum,ST));
    }
}

------原创文章,仅供参考

你可能感兴趣的:(H - easy - Roundgod and Milk Tea)