洛谷 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&& ...
随机推荐
- BZOJ 1598 第k短路
思路: 先反向建图 Dijkstra一遍 求出h数组 再正向建图 A_star一遍 搞定 //By SiriusRen #include <queue> #include <cstd ...
- OpenCV中InputArray和OutputArray使用方法
原文链接:http://blog.csdn.net/yang_xian521/article/details/7755101 看过OpenCV源代码的朋友,肯定都知道很多函数的接口都是InputArr ...
- QNX与Linux两家未来有望独霸车载电子操作系统
车载电子操作系统是汽车智能化的核心,能够有效分配车机的硬件资源,对车内各种任务功能进行协同管理,并控制各项任务优先级别.常见的车载电子操作系统有:QNX.Linux(Android,AaliOS).W ...
- eclipse部署maven web项目到tomcat服务器时,没有将lib、web.xml复制过去的解决办法
我这几天在写项目的时候发现自己以前的项目能够访问,隔一段时间写的这个项目却不能够访问,没有发现代码的逻辑错,但是就是访问不了jsp页面,项目一发布就是出现404错误,后来发现原来是发布到tomcat上 ...
- C语言利用 void 类型指针实现面向对象类概念与抽象
不使用C++时,很多C语言新手可能认为C语言缺乏了面向对象和抽象性,事实上,C语言通过某种组合方式,可以间接性的实现面对对象和抽象. 不过多态和继承这种实现,就有点小麻烦,但是依然可以实现. 核心: ...
- .Net 扩展的使用
Product.cs using System; using System.Collections.Generic; using System.Linq; using System.Web; name ...
- 怎样及时检测出非正常断开的TCP连接(zz)
此处的”非正常断开”指TCP连接不是以优雅的方式断开,如网线故障等物理链路的原因,还有突然主机断电等原因有两种方法可以检测:1.TCP连接双方定时发握手消息 2.利用TCP协议栈中的KeepAlive ...
- AC自动机 hdu2222
#include <iostream> using namespace std; struct Node{ Node *next[]; Node* fail; int count; Nod ...
- 量化派基于Hadoop、Spark、Storm的大数据风控架构--转
原文地址:http://www.csdn.net/article/2015-10-06/2825849 量化派是一家金融大数据公司,为金融机构提供数据服务和技术支持,也通过旗下产品“信用钱包”帮助个人 ...
- javafx drag
public class EffectTest extends Application { @Override public void start(Stage stage) { stage.setTi ...