HDOJ--3552--I can do it!(思维)

来源:互联网 发布:新疆人 知乎 编辑:程序博客网 时间:2024/04/30 02:15

I can do it!

Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 1038    Accepted Submission(s): 482


Problem Description
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 , 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. (1 <= 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
131 1002 1003 1
 

Sample Output
Case 1: 3
 
题意:题目的大概意思是,给你N个元素,每个元素里面包括两条性质A,B,你需要把这N个元素分成两个集合A,B。对于这两个集合,选出A集合中最大的,和B集合中最大的.并求出最终MAXA+MAXB的最小值。
     难点:1.就是题目的理解。2,,不能用普通的枚举方法,不然会超时,需要想出来一种比较巧妙的枚举方法。
     思路:由题意可知,集合A 和集合B 有多种可能 所以需要 一一枚举,每次枚举出A集合中的最大值max,然后比这个最大值MAX还要大的肯定不是集合A中的元素。从而找到集合B ,不断更新min值就好。
ac代码:
#include<stdio.h>#include<string.h>#include<algorithm>#define INF 0x3f3f3f3f#define MAX 100000+10using namespace std;struct pro{int a,b;}num[MAX];bool cmp(pro x,pro y){return x.a>y.a;}int main(){int T,i;scanf("%d",&T);for(i=1;i<=T;i++){int n;scanf("%d",&n);for(int j=1;j<=n;j++)scanf("%d%d",&num[j].a,&num[j].b);sort(num+1,num+n+1,cmp);int B=0,mincost=INF;for(int j=2;j<=n;j++){B=max(B,num[j-1].b);mincost=min(mincost,num[j].a+B);}printf("Case %d: %d\n",i,mincost);}return 0;} 


0 0
原创粉丝点击