【USACO-2017-OPEN-银组】 Paired Up



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 (M1,000,000,000M 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  ( 1N100,000 ). Each of the next  N  lines contains two integers  x  and  y , indicating that FJ has  x  cows each with milk output  y  ( 1y1,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.

AC代码:


#include <cstdio>   //AC
#include <iostream>
#include <algorithm>
#define M 100010
using namespace std;

struct node{
    int num;    //相应的奶牛数量
    int milk;   //产奶量
}cow[M];

bool cmp(node x,node y)
{
    if(x.milk != y.milk)
        return x.milk < y.milk;
    return x.num < y.num;
}
int main()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
        scanf("%d%d",&cow[i].num,&cow[i].milk);
    sort(cow+1,cow+n+1,cmp);

    int L = 1;
    int R = n;
    int ans=0;
    while(L <= R)
    {
        ans = max(ans,cow[L].milk + cow[R].milk);   //每次 都是 选择 产奶量最小的+最大的
                                                // 这样能做到 和的最大值 尽量小(最小)
        int temp = min(cow[L].num,cow[R].num);

        cow[L].num -= temp;
        if(!cow[L].num) // 如果产奶量 少的奶牛,不剩下(刚好减完),否则,L就不变
            ++L;   

        cow[R].num -= temp; //同理
        if(!cow[R].num)
            --R;
    }
    cout<<ans<<endl;
    return 0;
}

希望对你有所帮助



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值