我是靠谱客的博主 干净高跟鞋,最近开发中收集的这篇文章主要介绍Paired Up - UPCOJ 3450,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

题目:

题目描述
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行,每行给你一对x y,表示有xy,现在让你把所有的y都两两配对起来,问你怎样搭配可以让最大值最小。


思路:

  直接按照权值sort一下,然后最左最右两两配对记录一下最大值就可以了。不能太暴力,会超时,需要在配对的时候稍微处理一下。


实现:

#include <bits/stdc++.h>
using namespace std;
#define maxn 100007
int n,result;
struct C {
    int w,num;
    bool operator < (const C& tmp) const {
        return w < tmp.w;
    }
}cow[maxn];
int main() {
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
#endif
    ios_base::sync_with_stdio(false);cin.tie(nullptr);
    cin >> n;
    for(int i=0 ; i<n ; i++) cin >> cow[i].num >> cow[i].w;
    sort(cow,cow+n);
    int l=0,r=n-1,tmp;
    while(l<=r) {
        while(cow[l].num>0 && cow[r].num>0) {
            result = max(result, cow[l].w + cow[r].w);
            tmp = min(cow[l].num, cow[r].num);
            cow[l].num -= tmp;
            cow[r].num -= tmp;
        }
        if(cow[l].num<=0) l++;
        if(cow[r].num<=0) r--;
    }
    cout << result << 'n';
    return 0;
}

最后

以上就是干净高跟鞋为你收集整理的Paired Up - UPCOJ 3450的全部内容,希望文章能够帮你解决Paired Up - UPCOJ 3450所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(45)

评论列表共有 0 条评论

立即
投稿
返回
顶部