HDU 6667 (思维)

2019 杭电多校 8 1011

题目链接:HDU 6667

比赛链接:2019 Multi-University Training Contest 8

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 classes participating in this festival, where the ith class has students and will make 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 , denoting the number of test cases.

Each test case starts with a line of a single integer , the number of classes. For the next lines, each containing two integers , 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 over all test cases does not exceed .

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

Solution

题意:

个班级,每个班有 个人,做了 杯奶茶,每个班的每个人最多喝一杯奶茶且不能和自己班做的奶茶,问最多共有多少人喝到奶茶。

思路

最初的想法是用一个 记录所有剩余的奶茶数,然后每个组能喝的奶茶数为 该组的奶茶(自己不能喝自己的) 上一组做的奶茶 (上一组减掉的加回来)。后来发现有点问题,就是中间一步减掉自己的奶茶可能是减多的,也就是上一组喝掉的可能就是当前组的奶茶,那么当前组剩余的奶茶是比原来少的,于是就用 保存上一组喝掉的奶茶数,每次让上一组喝掉当前组的奶茶,如果不够喝再用 保存还要喝掉的奶茶数,往下迭代。

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const int maxn = 1e6 + 10;

struct Team
{
    ll m, n;  // 人数 奶茶数
} t[maxn];

int cmp(Team t1, Team t2) {
    return t1.m > t2.m;
}

int main() {
    int T;
    cin >> T;
    while(T--) {
        int n;
        scanf("%d", &n);
        ll sum = 0;
        for(int i = 0; i < n; ++i) {
            scanf("%lld%lld", &t[i].m, &t[i].n);
            sum += t[i].n;
        }
        sort(t, t + n, cmp);
        ll ans = 0; 
        ll tmp = t[0].n; // tmp 保存喝掉的奶茶数 第一组一定要被喝
        ll tmp2 = 0; // tmp2 保存的是上一组喝掉的奶茶
        for(int i = 0; i < n; ++i) {
            if(i) {
                // 上一组喝掉的奶茶数+之前喝掉的奶茶数
                if(t[i].n < tmp2 + tmp) {
                    t[i].n = 0;
                    tmp = tmp2 + tmp - t[i].n;
                } else {
                    t[i].n = t[i].n - (tmp2 + tmp);
                    tmp = 0;
                }
            }
            sum -= t[i].n; // 自己不能喝自己的奶茶
            if(i) sum += t[i - 1].n; // 可以喝上一组的奶茶
            // 剩余的奶茶数与第 i 组人数比较
            if(sum >= t[i].m) {
                ans += t[i].m;
                sum -= t[i].m;
                tmp2 = t[i].m;
            } else {
                ans += sum;
                sum -= sum;
                tmp2 = sum;
            }
            // cout << ans << endl;
        }
        printf("%lld\n", ans);
    }
    return 0;
}

比赛中完全想复杂了,其实完全可以很快处理。把每个人能喝的奶茶加起来和所有的奶茶比较即可。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e6 + 10;

ll a[maxn], b[maxn];

int main() {
    int T;
    scanf("%d", &T);
    while(T--) {
        int n;
        scanf("%d", &n);
        ll sum = 0;
        for(int i = 0; i < n; ++i) {
            scanf("%lld%lld", &a[i], &b[i]);
            sum += b[i];
        }
        ll ans = 0;
        for(int i = 0; i < n; ++i) {
            ans += min(a[i], sum - b[i]);
        }
        printf("%lld\n", min(ans, sum));
    }
    return 0;
}
全部评论

相关推荐

02-22 18:38
门头沟学院 Java
程序员牛肉:标准的NPC简历,一个短链接+12306。你可以在牛客上面搜一搜有多少人的简历和你一样。你自己能不能给出你一个理由让面试官在大家简历高度相同的情况下,选择约面你而不是对应的211,985学生? 是因为你即将拥有的那段小厂实习吗?这种小厂实习真的很有含金量吗?因此你可以找实习,但是你如果只能找到小厂实习的话,其实意义不太大。 但你的时间是充足的,相信我:从现在到今年的九月份大三上你就干两个事情:"写博客"+“参加开源之夏”。这两个搞好了不亚于一段大厂实习的含金量。 想要让自己变得更强,首先就是不要把自己当打工人看待,让自己简历上面的活人气息更多一点,不要让自己成为流水线的产物。你不是在出售你的技能,你是在利用你的技能和公司达成一种合作关系。
点赞 评论 收藏
分享
群星之怒:1.照片可以换更好一点的,可以适量P图,带一些发型,遮住额头,最好穿的正式一点,可以适当P图。2.内容太少。建议添加的:求职意向(随着投递岗位动态更改);项目经历(内容太少了建议添加一些说明,技术栈:用到了什么技术,还有你是怎么实现的,比如如何确保数据传输稳定的,角色注册用到了什么技术等等。)项目经历是大头,没有实习是硬伤,如果项目经理不突出的话基本很难过简历筛。3.有些内容不必要,比如自我评价,校内实践。如果实践和工作无关千万别写,不如多丰富丰富项目。4.排版建议:建议排版是先基础信息,然后教育背景(要突出和工作相关的课程),然后专业技能(一定要简短,不要长篇大论,写你会什么,会的程度就可以),然后是项目经历(一定要详细,占整个简历一定要超过一半,甚至超过百分之70都可以)。最后如果有一部分空白的话可以填补上校内获得的专业相关的奖项,没有就写点校园经历和自我评价。5.技术一定要够硬,禁得住拷打。还有作息尽量保证正常,不要太焦虑。我24双非本科还是非科班,秋招春招各找了一段实习结果都没有转正,当时都想噶了,最后6月份在校的尾巴也找到一份工作干到现在,找工作有时很看运气的不要急着自我否定。 加油
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务