【ACM】HDU 6667 Roundgod and Milk Tea 2019杭电多校第八场1011 贪心

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6667

Roundgod and Milk Tea

Time Limit: 6000/6000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 12    Accepted Submission(s): 6


 

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

 

Source

2019 Multi-University Training Contest 8

 

题目大意:

有n个班级,每个班级有ai个人,制作了bi杯奶茶。每个人都不能喝自己班制作的奶茶,且只能喝一杯奶茶,问最多有多少人喝到奶茶。

思路:

最开始是想到匹配,数据范围太大,爆炸了。接着想贪心,按奶茶数最多的同时人数最少的进行排序,开两个指针统计,代码写炸了,爆炸了。

实际总的思路还是贪心,让第一个班级优先喝第二个班级做的奶茶。先把班级按人数从大到小排序,统计所有的奶茶数sum。

先上一个式子:(原谅笔者表达能力不太好)

now= min(a,sum-max(b-leave,0))

now表示当前班级可以喝到多少杯奶茶,a为班级人数,b为该班级做的奶茶数量,leave表示之前的班级喝掉的奶茶数,答案就是所有now的总和。

举个栗子:

4

1 3

2 5

5 2

3 1

排序之后变成了

5 2

3 1

2 5

1 3

最开始的sum=11,第一个班级最多喝5杯,上面遗留了0杯,now=min(5,11-max(2-0))=5,sum=11-5=6,leave=0+5=5

第二个班级最多喝3杯,上面遗留5杯,now=min(3,6-max(1-5,0))=3,sum=6-3=3,leave=5+3-1=7(-1是因为第二个班级提供了1杯奶茶)

第三个班级最多喝2杯,上面遗留7杯,now=min(2,3-max(5-7,0))=2,sum=3-2=1,leave=7+2-5=4

第四个班级最多喝1杯,上面遗留4杯,now=min(1,1-max(3-4,0))=1,sum=1-1=0,leave=4+1-3=2

答案就是5+3+2+1=11

上代码:

#include <bits/stdc++.h>
using namespace std;
const int maxn=1e6+5;
struct node
{
    long long a,b;
}pe[maxn];
bool cmp(node x,node y)
{
    return x.a>y.a;
}
int main()
{
    //freopen("in.txt","r",stdin);
    int t,n;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        long long ans=0,sum=0;
        for(int i=0;i<n;i++)
        {
            scanf("%lld%lld",&pe[i].a,&pe[i].b);
            sum+=pe[i].b;
        }
        sort(pe,pe+n,cmp);
        long long rela=0;
        for(int i=0;i<n;i++)
        {
            long long now=min(pe[i].a,sum-max(pe[i].b-rela,(long long)0));
            if(pe[i].b<=rela)
                rela-=pe[i].b;
            else
                rela=0;
            sum-=now;
            rela+=now;
            ans+=now;
        }
        printf("%lld\n",ans);
    }
    return 0;
}

菜哭~~

队里的大佬想出了一种很巧妙的思路,大体是这样的:

先遍历一遍数组,找到人数最多的班级和奶茶数最多的班级,统计总人数和总奶茶数。

如果人数最多和奶茶最多的班级是同一个,且这个班的人数大于剩余的奶茶,这个班做的奶茶多于剩下的人,结果为总人数-该班人数+总奶茶数-该班奶茶数,否则,结果为min(总人数,总奶茶数)。

(更新)

上面这种思路还有一些不完善的地方,放一组测试数据:

1

4

1 1

6 3

11 2

10 20

答案为24

这题数据有些水···

%%%

猜你喜欢

转载自blog.csdn.net/qq_41279172/article/details/99599211