BZOJ 4781: [Usaco2017 Open]Paired Up 贪心

4781: [Usaco2017 Open]Paired Up

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 87  Solved: 63
[Submit][Status][Discuss]

Description

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 Mcows (M≤1,000,000,000, M even) and partition 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 Aand B are paired  up, then it takes a total of A+Bunits of time to milk them both.Please help Farmer John determine th e minimum possible amount of time the entire milking process will take to complete, assuming he pair s the cows up in the best possible way.
John有n种牛(n<=100000)每种牛有数量x与时间y牛的总数为m要将m头牛分为m/2对,每对的时间花费为两牛时间之 和问最大的那对的最小值m<=1e9 y<=1e9

Input

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

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 proc
ess would therefore complete after 10 units of time. Any other pairing would be sub-optimal, resulti
ng in a stall taking more than 10 units of time to milk.

随便YY一下就知道最大匹配最小

贪心搞就可以了

不知为何WA两发 T_T


#include<cmath>
#include<ctime>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<complex>
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<string>
#include<bitset>
#include<queue>
#include<map>
#include<set>
using namespace std;
inline int read()
{
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch<='9'&&ch>='0'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
	return x*f;
}
const int N=100100;
struct cow{int num,val;}c[N];
inline bool cmp(cow a,cow b)
{return a.val<b.val;}
int main()
{
	int n=read();
	for(int i=1;i<=n;i++)c[i].num=read(),c[i].val=read();
	sort(c+1,c+1+n,cmp);int l=1,r=n;
	int ans=0;
	while(l<r)
	{
		ans=max(ans,c[l].val+c[r].val);
		int tmp=min(c[l].num,c[r].num);
		c[l].num-=tmp;c[r].num-=tmp;
		if(!c[l].num)l++;
		if(!c[r].num)r--;
	}
	if(l==r&&c[l].num)ans=max(c[l].num<<1,ans);
	printf("%d\n",ans);
}
/*
3
1 8
2 5
1 2

10
*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值