USACO——Paired Up
来源:互联网 发布:小学一年级数学软件 编辑:程序博客网 时间:2024/06/11 09:06
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 M cows (M≤1,000,000,000) andpartition them into M/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/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 A and B are paired up, then it takes a total of A+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.
INPUT FORMAT (file pairup.in):
The first line of input contains N (1≤N≤100,000). Each of the next N lines contains two
integers x and y, indicating that FJ has x cows each with milk output y (1≤y≤1,000,000,000).
The sum of the x's is M, the total number of cows.
OUTPUT FORMAT (file pairup.out):
Print out the minimum amount of time it takes FJ's cows to be milked, assuming they are
optimally paired up.
SAMPLE INPUT:
3
1 8
2 5
1 2
SAMPLE OUTPUT:
10
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.
#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>using namespace std;struct node{int x,y;}cow[100001];inline int readint(){ int i=0,f=1; char ch; for(ch=getchar();ch<'0'||ch>'9';ch=getchar()); for(;ch>='0' && ch<='9';ch=getchar()) i=(i<<3)+(i<<1)+ch-'0'; return i*f;}inline bool cmp(const node&a,const node&b){return a.y<b.y;}int main(){auto int n,time=0,l=1,r;freopen("pairup.in","r",stdin);freopen("pairup.out","w",stdout);r=n=readint();for(int i=1;i<=n;++i) cow[i].x=readint(),cow[i].y=readint();sort(cow+1,cow+1+n,cmp);while(l<r){time=max(time,cow[l].y+cow[r].y);if(cow[l].x<cow[r].x){cow[r].x-=cow[l].x;while(!cow[++l].x);}else if(cow[l].x==cow[r].x){while(!cow[++l].x);while(!cow[--r].x);}else{cow[l].x-=cow[r].x;while(!cow[--r].x);}}if(l==r) time=max(time,cow[l].y+cow[r].y);printf("%d\n",time);return 0;}
阅读全文
0 0
- USACO——Paired Up
- 【USACO-2017-OPEN-银组】Paired Up
- Paired Up
- Paired Up
- bzoj4781[Usaco2017 Open]Paired Up
- BZOJ 4781 Usaco2017 Open Paired Up
- BZOJ 4781: [Usaco2017 Open]Paired Up 贪心
- bzoj 4781: [Usaco2017 Open]Paired Up
- luogu3669[USACO17OPEN]Paired Up 牛牛配对
- C++11——【USACO 4.4.3】——Frame Up
- 真机测试遇到 ——is not paired with your computer
- usaco 4.4.3 Frame Up
- usaco 4.4.3 Frame Up
- USACO Frame Up 解题报告
- USACO Section 4.4 Frame Up - DFS即可~
- usaco 4.4 Frame Up(拓扑排序)
- USACO 2009 Mar Look Up 向右看齐
- usaco training 4.4.3 Frame Up 题解
- 搭建Struts2 环境
- 朴素贝叶斯分类原理及Python实现简单文本分类
- 1642 Set
- 二叉树的层次遍历
- POJ-3126 Prime Path
- USACO——Paired Up
- LINUX字符设备驱动纠正
- MySQL数据库操作
- C. Vladik and Memorable Trip
- 图像处理基础知识系列之二:核概率密度估计简介
- 网站怎么赚钱? 靠流量赚钱吗? 广告吗? 建什么网站赚钱
- scala隐式转换
- 在Nanopi-NEO上直接编译带Cedrus-avcodec的ffmpeg
- Mybatis之Mapper动态代理方式