HDU 6667 Roundgod and Milk Tea (思维)
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 \(n\) classes participating in this festival, where the ith class has \(a_i\) students and will make \(b_i\) 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\le T\le 25)\), denoting the number of test cases.
Each test case starts with a line of a single integer \(n (1\le n\le 10^6)\), the number of classes. For the next \(n\) lines, each containing two integers \(a,b (0\le a,b\le 10^9)\), 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\times 10^6\).
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
题意:
有 \(n\) 个班级,每个班有 \(a_i\) 个人,做了 \(b_i\) 杯奶茶,每个班的每个人最多喝一杯奶茶且不能和自己班做的奶茶,问最多共有多少人喝到奶茶。
思路
最初的想法是用一个 \(sum\) 记录所有剩余的奶茶数,然后每个组能喝的奶茶数为 \(sum\ -\) 该组的奶茶(自己不能喝自己的) \(+\) 上一组做的奶茶 (上一组减掉的加回来)。后来发现有点问题,就是中间一步减掉自己的奶茶可能是减多的,也就是上一组喝掉的可能就是当前组的奶茶,那么当前组剩余的奶茶是比原来少的,于是就用 \(tmp2\) 保存上一组喝掉的奶茶数,每次让上一组喝掉当前组的奶茶,如果不够喝再用 \(tmp\) 保存还要喝掉的奶茶数,往下迭代。
#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;
}
HDU 6667 Roundgod and Milk Tea (思维)的更多相关文章
- HDU 6667 Roundgod and Milk Tea
hdu题面 Time limit 6000 ms Memory limit 131072 kB OS Windows Source 2019 Multi-University Training Con ...
- hdu多校第八场 1011 (hdu6667) Roundgod and Milk Tea 二分图匹配
题意: 有若干个班,每个班有些人要喝奶茶,也提供一些奶茶,一人喝一杯,但是自己班的人不能喝自己班的奶茶,求最多能有多少人喝上奶茶. 题解: 典型的二分图匹配问题,学生在左,奶茶在右,学生和非自己班的奶 ...
- 喝奶茶最大值(不能喝自己班级的)2019 Multi-University Training Contest 8--hdu杭电第8场(Roundgod and Milk Tea)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6667 题意: 有 n个班级,每个班级有a个人.b个奶茶,每个班的人不能喝自己的奶茶,只能喝别人班的奶茶 ...
- [2019杭电多校第八场][hdu6667]Roundgod and Milk Tea
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6667 题目大意是说n个班级,每个班级有ai人和bi杯茶,每个人只能喝其他班的茶并且只能喝一杯.问最多有 ...
- 【HDU6667】Roundgod and Milk Tea【贪心】
题目大意:给你ai,bi,限制ai不能流向bi,求最大流 题解:贪心,对于第i个班级,考虑前i-1个班级匹配完剩余多少a,b,将这些ab对第i个班级进行贪心匹配 匹配完若第i个班级还有剩余的ab,考虑 ...
- 【HDOJ6667】Roundgod and Milk Tea(模拟)
题意:有n个班级,每个班级有a[i]个人,b[i]杯奶茶 每个人至多喝一杯奶茶,且不能喝自己班的 问能喝到奶茶的最多总人数 n<=1e6,a[i],b[i]<=1e9 思路: 做法一: # ...
- hdu 3336 Count the string(思维可水过,KMP)
题目 以下不是KMP算法—— 以下是kiki告诉我的方法,好厉害的思维—— 就是巧用标记,先标记第一个出现的所有位置,然后一遍遍从标记的位置往下找. #include<stdio.h> # ...
- HDU 4611 Balls Rearrangement (数学-思维逻辑题)
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4611 题意:给你一个N.A.B,要你求 AC代码: #include <iostream> ...
- HDU 5122 K.Bro Sorting(模拟——思维题详解)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5122 Problem Description Matt's friend K.Bro is an A ...
随机推荐
- php+js实现极验滑块拖动验证码-tncode
先上图: 演示地址:http://aso.39gs.com/tncode/index.html 相信在淘宝,斗鱼这些大网站都见到过这样的验证码了,拖动验证码比传统在移动端有更好的化验,减少用户的输入. ...
- MySQL strcmp 函数
STRCMP(str1, str2) 比较两个字符串,如果这两个字符串相等返回0,如果第一个参数是根据当前的排序小于第二个参数顺序返回-1,否则返回1. mysql> SELECT STRCMP ...
- 【SQL】链接服务器
最近做项目,需要对两个数据库进行同步操作,所以采用在Server SQL中建立链接服务器方式实现. 链接服务器,可以直接访问/操作其他服务器上的数据库表. 1.连接SQL Server链接服务器 EX ...
- Forgery CodeForces - 1059B
一道印章刻印的题目: 具体要求:有一个固定的3*3的印章,给你一个墨迹问能用这个印章印出墨迹吗?(一个像素可以多次被上色) 输入:第一行是墨迹的行列规模,接下来是墨迹 输出:If Andrey can ...
- Prometheus 详解
Prometheus 章节 1.Prometheus 简介 2.Prometheus 安装与配置 3.Exporter 4.Pushgateway 5.本地存储和远程存储 6.高可用方案 7.报警插件 ...
- activiti7从act_ge_bytearray表中查询资源文件并保存到桌面文件夹中
package com.zcc.activiti02; import org.activiti.engine.ProcessEngine;import org.activiti.engine.Proc ...
- Windows7下移植Qt4.8.4项目到QT5.2上时遇到的一些问题
最近在Windows7下将Qt4.8.4+MSVC2008的项目移植到QT5.2下时,遇到了一些小问题: 问题一:错误:C1083: 无法打开包括文件:"QApplication&q ...
- CVE-2010-4258漏洞分析
Nelson Elhage最近发现了一个内核设计上的漏洞, 通过利用这个漏洞可以将一些以前只能dos的漏洞变成可以权限提升的漏洞. 当fork一个进程在的时候, copy_process执行如下操作: ...
- [轉]C/C++中的volatile使用時機?
不知各位對volatile(揮發性的)這個字陌不陌生? 我相信大家在一些程式或多或少都看 過這個字眼, 但是究竟要在何種場合用它呢?.當然一定是有需要, C/C++才會有這個保留字, 否則只是增加pr ...
- CTU OPEN 2017 Shooting Gallery /// 区间DP
题目大意: 给定n 给定n个数 选定一个区间留下其他消去 要求区间两端的两个数一样 若成功留下一个区间 则在选定区间的基础上 继续进行上述操作 直到无法再选出这样的区间 求最多操作数 按区间长度由短到 ...