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, 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 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.

输入

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.

输出

Print out the minimum amount of time it takes FJ’s cows to be milked, assuming they are optimally paired up.

样例输入

3
1 8
2 5
1 2

样例输出

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.


题意

给定N有N组数据,每组数据有两个数,x,y。是指有x头挤奶需要y时间的牛。两两组对,挤奶同时进行,问时间上的最少花费是多少。

思路

给一个结构体然后谈后用sort排序, 就按照y从小到大排序,从两端模拟,时间最大和时间最小来匹配,水题。直接上代码。

代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define  mem(a)  memset(a,0,sizeof(a))
using namespace std;

typedef struct
{
    int v,x;
} node;
node a[100005];
bool cmp (node x,node y)
{
    if(x.v==y.v)
        return x.x<y.x;
    else
        return x.v<y.v;
}
int main ()
{

#ifndef ONLINE_JUDGE
    freopen("1","r",stdin);
#endif // ONLINE_JUDGE
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int n;
    cin>>n;
    for(int i=0; i<n; ++i)
    {
        cin>>a[i].x>>a[i].v;
    }
    sort(a,a+n,cmp);

    int x=0,y=n-1,z,ans=-1;
    while(x<y)
    {
      z=min(a[x].x,a[y].x);
      a[x].x-=z;
      a[y].x-=z;
      ans=max(ans,a[x].v+a[y].v);
      if(a[x].x==0)
         x++;
      if(a[y].x==0)
         y--;
    }
    if(a[x].x!=0)
    {
         ans=max(a[x].x*2,ans);
    }
    cout<<ans<<endl;
    return 0;
}



/**************************************************************
    Language: C++
    Result: 正确
    Time:80 ms
    Memory:2472 kb
****************************************************************/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

渴鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值