洛谷 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&& ...
随机推荐
- vue20 父子组件数据交互
子组件使用父组件数据 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...
- BZOJ 1577 贪心
思路:同POJ3038 http://blog.csdn.net/qq_31785871/article/details/52953214 //By SiriusRen #include <se ...
- 把华为交换机设置成时钟源服务器(NTP)
把华为交换机设置成时钟源服务器(NTP),提供给下面客户端Linux服务器使用, 1,先设置交换机的时区,和正确时间 # 假设地理位置在中国北京,设置本地时区名称为BJ. 如果系统默认的UTC是伦敦时 ...
- react入门安装
react的入门安装 1.react的适用方法有两种,其一是依赖在线的cdn地址: https://reactjs.org/docs/cdn-links.html 官方给的cdn地址如下 <sc ...
- HDU 4971 A simple brute force problem.
A simple brute force problem. Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged o ...
- 洛谷 P1581 A+B Problem(升级版)
P1581 A+B Problem(升级版) 题目背景 小明这在写作业,其中有一道A+B Problem ,他想啊想啊想,就是想不出来,于是就找到了会编程的你...... 题目描述 这里的A+B是很奇 ...
- [Javascript] this in Function Calls
In most cases, the value of a function's this argument is determined by how the function is called. ...
- 【Struts2】Struts2纯手工安装、配置以及Helloworld,以最新版struts 2.3.20 GA做样例
很多网上的教程对Struts2的配置.安装弄得不明不白,非常多高手以为小白是什么都懂.很多细节上面的地方没有说明清楚,甚至还有在Maven上面解说的,要知道Struts2跟Maven没有半点的关系.全 ...
- TXT小说朗读正式版
作者:www.gudianxiaoshuo.com 干净清洁的小说朗读软件,支持 TXT阅读.语音朗诵.点评标注.数据挖掘
- Spring MVC原理及实例基础扫盲篇
近期 项目中刚接触了SpringMVC,就把这几天看的跟实践的东西写出来吧. 一.首先,先来了解一下SpringMVC究竟是个什么样的框架? Spring Web MVC是一种基于Java的实现了We ...