哈理工oj 1079 I can do it【贪心】
2016-03-19 10:14
429 查看
I can do it | ||||||
| ||||||
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 (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 |
思路:将输入中的左边一列Ai排序,将输入种右边一列Bi也排序,我们找到拥有更大价值的一个组合,取其最小值、然后在另外一边找到最大值,加和就是最终贪心出来的结果。
简单证明:因为我们从两边找到各自的最大值,我们怎样也逃脱不了一方集合的最大值选取,也就是说最终结果里边一定有A【】里边的最大Ai或者B【】里边最大值Bi,那么我们为了更小的取值,我们只能避免加入max(A【max】,B【max】)这个元素,这样我们就达到了贪心的目的。
对应AC代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int x[100005];
int y[100005];
int main()
{
int kase=0;
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d%d",&x[i],&y[i]);
}
sort(x,x+n);
sort(y,y+n);
int pos=-1;
for(int i=n-1;i>=0;i--)
{
if(x[i]!=y[i])
{
pos=i;
break;
}
}
printf("Case %d: ",++kase);
if(x[pos]>y[pos])
{
int vala=x[0];
for(int i=n-1;i>=0;i--)
{
if(i!=pos)
{
vala+=y[i];
break;
}
}
printf("%d\n",vala);
}
else
{
int valb=y[0];
for(int i=n-1;i>=0;i--)
{
if(i!=pos)
{
valb+=x[i];
break;
}
}
printf("%d\n",valb);
}
}
}
相关文章推荐
- 数据挖掘---Kmeans算法
- 数据挖掘---Kmeans算法
- 数据挖掘---Kmeans算法
- 数据挖掘---Kmeans算法
- 四则运算三
- 线性表
- Leetcode #16 3Sum Closest 找3数之和最接近 解题小节
- ionic页面footer按钮右下方的小三角实现
- Starting MySQL... ERROR! The server quit without updating PID file (/mydata/data/t1.pid).
- K3WIES 笔记
- 大视野1195: [HNOI2006]最短母串
- iOS远程推送(APNS)测试工具SmartPush for Mac
- 4004: [JLOI2015]装备购买 贪心+线性基
- 贝塞尔曲线 & CAShapeLayer & Stroke 动画 浅谈
- hdu 1160 FatMouse's Speed 贪心
- MFC---GBK文件与UTF8文件编码互转
- thinkphp3.2.3 自定义标签图文教程,Taglib放置项目路径
- 单链表倒置
- iOS开发之(APNS)远程推送实现,证书与真机调试
- 关于JS变量的作用域,作用域链与闭包