HDOJ--3552--I can do it!(思维)
2015-12-20 20:43
309 查看
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
1
3
1 100
2 100
3 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+10 using 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; }
相关文章推荐
- 浏览器兼容多文件上传控件
- Opencv开发android应用
- HTML 5 Web 存储
- python windows下的安装配置以及django的学习经验
- 2015-12-20 FFC
- BZOJ 3261: 最大异或和( 可持久化trie )
- WPF 策略模式
- Web存储格式语言的发展
- Web 存储简介
- 实验报告(实验五)
- 设计模式系列(十三)迭代器模式(Iterator Pattern)
- 华为oj 句子逆序
- JavaWeb Session问题整理
- SDUT 数据结构实验之查找四:二分查找
- html5离线储存,application cache,manifest使用体验
- ZOJ 3329 【概率DP】
- 批处理命令——echo 和 @
- 逆向工程(一):汇编、逆向工程基础篇
- swift-基础04-数字类型转换
- SecureCRT中python脚本编写