洛谷 P3669 [USACO17OPEN]Paired Up 牛牛配对
题目描述
Farmer John finds that his cows are each easier to milk when they have another cow nearby for moral support. He therefore wants to take his MM cows (M \leq 1,000,000,000M≤1,000,000,000, MM even) and partition them into M/2M/2 pairs. Each pair of cows will then be ushered off to a separate stall in the barn for milking. The milking in each of these M/2M/2 stalls will take place simultaneously.
To make matters a bit complicated, each of Farmer John's cows has a different milk output. If cows of milk outputs AA and BBare paired up, then it takes a total of A+BA+B units of time to milk them both.
Please help Farmer John determine the minimum possible amount of time the entire milking process will take to complete, assuming he pairs the cows up in the best possible way.
有M(M为奇数)头奶牛,每头奶牛有一个产奶量,将这些奶牛两两配对,每对奶牛的产奶的时间为两头奶牛产奶量的总和。现在这M/2对奶牛同时产奶,问所需的最短时间是多少
输入输出格式
输入格式:
The first line of input contains NN (1 \leq N \leq 100,0001≤N≤100,000).
Each of the next NN lines contains two integers xx and yy, indicating that FJ has xx cows each with milk output yy (1 \leq y \leq 1,000,000,0001≤y≤1,000,000,000). The sum of the xx's is MM, the total number of cows.
第一行为一个正整数N
接下来有M行,每行两个正整数x和y,表示有x头奶牛的产奶量为y。保证所有x的总和等于M
输出格式:
Print out the minimum amount of time it takes FJ's cows to be milked, assuming they are optimally paired up.
输出产奶时间的最小值
输入输出样例
说明
Here, if the cows with outputs 8+2 are paired up, and those with outputs 5+5 are
paired up, the both stalls take 10 units of time for milking. Since milking
takes place simultaneously, the entire process would therefore complete after 10
units of time. Any other pairing would be sub-optimal, resulting in a stall taking more than 10
units of time to milk.
奶牛的产奶量分别为8,5,5,2。
让8和2配对,5和5配对,则产奶时间分别为10,10,所以这两对奶牛同时产奶的时间为10.
感谢 @ x咫尺天涯x 的翻译
思路:模拟+贪心。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int n,ans;
struct nond{
int x,y;
}v[];
int cmp(nond a,nond b){
return a.y<b.y;
}
int main(){
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d%d",&v[i].x,&v[i].y);
sort(v+,v++n,cmp);
int i=,j=n;
while(i<=j){
if(v[i].y+v[j].y>ans) ans=v[i].y+v[j].y;
if(v[i].x>v[j].x){ v[i].x-=v[j].x;j--; }
else if(v[i].x<v[j].x){ v[j].x-=v[i].x;i++; }
else{ i++;j--; }
}
printf("%d",ans);
}
洛谷 P3669 [USACO17OPEN]Paired Up 牛牛配对的更多相关文章
- 洛谷P3668 [USACO17OPEN]Modern Art 2 现代艺术2
P3668 [USACO17OPEN]Modern Art 2 现代艺术2 题目背景 小TY的同学HF也想创作艺术 HF只有一块长条状的画布(画条),所以每一次涂色只能涂上连续几个单位的颜料,同样新的 ...
- 洛谷 P3671 [USACO17OPEN]Where's Bessie? 贝西在哪呢
P3671 [USACO17OPEN]Where's Bessie? 贝西在哪呢 题目背景 农夫John正在测试一个他新发明的全自动寻找奶牛无人机,它能够照一张农场的图片然后自动找出奶牛的位置. 不幸 ...
- 洛谷 P3670 [USACO17OPEN]Bovine Genomics S奶牛基因组(银)
P3670 [USACO17OPEN]Bovine Genomics S奶牛基因组(银) 题目描述 Farmer John owns NN cows with spots and NN cows wi ...
- 洛谷P2756飞行员配对方案问题 P2055假期的宿舍【二分图匹配】题解+代码
洛谷 P2756飞行员配对方案问题 P2055假期的宿舍[二分图匹配] 飞行员配对方案问题 题目背景 第二次世界大战时期.. 题目描述 英国皇家空军从沦陷国征募了大量外籍飞行员.由皇家空军派出的每一架 ...
- 【CJOJ1494】【洛谷2756】飞行员配对方案问题
题面 题目背景 第二次世界大战时期.. 题目描述 英国皇家空军从沦陷国征募了大量外籍飞行员.由皇家空军派出的每一架飞机都需要配备在航行技能和语言上能互相配合的2 名飞行员,其中1 名是英国飞行员,另1 ...
- 洛谷P2507 [SCOI2008]配对 题解(dp+贪心)
洛谷P2507 [SCOI2008]配对 题解(dp+贪心) 标签:题解 阅读体验:https://zybuluo.com/Junlier/note/1299251 链接题目地址:洛谷P2507 [S ...
- 洛谷 [USACO17OPEN]Bovine Genomics G奶牛基因组(金) ———— 1道骗人的二分+trie树(其实是差分算法)
题目 :Bovine Genomics G奶牛基因组 传送门: 洛谷P3667 题目描述 Farmer John owns NN cows with spots and NN cows without ...
- [洛谷OJ] P1114 “非常男女”计划
洛谷1114 “非常男女”计划 本题地址:http://www.luogu.org/problem/show?pid=1114 题目描述 近来,初一年的XXX小朋友致力于研究班上同学的配对问题(别想太 ...
- USACO Section 1.3 题解 (洛谷OJ P1209 P1444 P3650 P2693)
usaco ch1.4 sort(d , d + c, [](int a, int b) -> bool { return a > b; }); 生成与过滤 generator&& ...
随机推荐
- Uncaught DOMException: Failed to execute 'toDataURL' on 'HTMLCanvasElement': Tainted canvases may not be exported
Uncaught DOMException: Failed to execute 'toDataURL' on 'HTMLCanvasElement': Tainted canvases may no ...
- 13. Intellij IDEA调试功能使用总结
转自:https://www.cnblogs.com/Bowu/p/4026117.html 这段时间一直在使用Intellij IDEA, 今天把调试区工具的使用方法记录于此. 先编译好要调试的程序 ...
- Weka中数据挖掘与机器学习系列之Weka简介(二)
不多说,直接上干货! Weka简介 Weka是怀卡托智能分析环境(Waikato Environment for Knowledge Analysis)的英文字首缩写,官方网址为:http://www ...
- PullToRefreshListView中嵌套ViewPager滑动冲突的解决
PullToRefreshListView中嵌套ViewPager滑动冲突的解决 最近恰好遇到PullToRefreshListView中需要嵌套ViewPager的情况,ViewPager 作为头部 ...
- K-序列(埃森哲杯第十六届上海大学程序设计联赛春季赛暨上海高校金马五校赛)
题目描述 给一个数组 a,长度为 n,若某个子序列中的和为 K 的倍数,那么这个序列被称为“K 序列”.现在要你 对数组 a 求出最长的子序列的长度,满足这个序列是 K 序列. 输入描述: 第一行为 ...
- ThinkPad X260 UEFI安装 win7 64位 方法
ThinkPad X260 UEFI安装 win7 64位 方法 1.使用DG重新格式化硬盘,格式为GPT 2.使用CGI 安装 WIM文件 (image不知是否可以,下次测试) 3.改BIOS ...
- Tensorflow 函数学习笔记
A: A:## tf.argmax(A, axis).eval() 输出axis维度上最大的数的索引 axis=0:列,axis=1:行 A:## tf.add(a,b) 创建a+b的计算图 A:# ...
- 【Linux下用户和组管理】
创建用户--useradd . 命令格式:useradd [参数] 用户名 useradd也可写成adduser . 参数如下 -u 指定UID号 -d 指定宿主目录 -e 指定生效时间 -g 指定基 ...
- Uva 10081 Tight words (概率DP)
Time limit: 3.000 seconds Given is an alphabet {0, 1, ... , k}, 0 <= k <= 9 . We say that a wo ...
- vim 基础学习之插入模式
插入模式1.字符编码,插入特殊字符 <C-v>{3位} 如,你想输入A,你可以在输入模式下<C-v>065(必须是3位) <C-v>u{4位} 如,你想输入¿,你可 ...