【枚举】哈理工OJ-1079 - I can do it【思路】

Given n elements, which have two properties, say Property A and Property B. For convenience, we use two integers Ai and Bi to measure the two properties.
Your task is, to partition the element into two sets, say Set A and Set B (can’t be empty) , which minimizes the value of max(x∈Set A) {Ax}+max(y∈Set B) {By}.
See sample test cases for further details.

Input
There are multiple test cases, the first line of input contains an integer denoting the number of test cases.
For each test case, the first line contains an integer N, indicates the number of elements. (2 <= N <= 100000)
For the next N lines, every line contains two integers Ai and Bi indicate the Property A and Property B of the ith element. (0 <= Ai, Bi <= 1000000000)

Output
For each test cases, output the minimum value.

Sample Input
1
3
1 100
2 100
3 1

Sample Output
Case 1: 3

#include<bits/stdc++.h>
using namespace std;
const int inf=0x3f3f3f3f;

struct node
{
    int x,y;
} a[111111];

int cmp(node a,node b)
{
    return a.x>b.x;
}

int main()
{
    int t,T=1,n;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i=0; i<n; i++)
        {
            scanf("%d%d",&a[i].x,&a[i].y);
        }
        sort(a,a+n,cmp);
        int ans=inf,Max=-1;
        for(int i=0; i<n-1; i++)
        {
            Max=max(Max,a[i].y);
            ans=min(ans,a[i+1].x+Max);
        }
        printf("Case %d: %d\n",T++,ans);
    }
}

题意:
每个点有两个属性,a和b,将n个点分成两个集合A和B(集合不允许为空),求A集合中点的a属性最大值和B集合中点的b属性的最大值之和最小
思路:
如果A集合中的最大值如果确定的话,那么所有属性a小于这个最大值都应该放到集合A中(因为它们既不会有比这个最大值更大,如果放入B集合的话还有可能是最后的值变大)剩下的就是B集合的元素;从中找出属性b最大与刚才的最大的a相加;循环n-1次,每次更换一个a,找出其中的最小值即可。

猜你喜欢

转载自blog.csdn.net/li_hongcheng/article/details/79350336