I can do it!(贪心)

I can do it!

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

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

Author

HyperHexagon

题解:题意好难理解啊,好像是给你一些元素,这些元素有两个性质,把这两个性质分成两个集合,然后找A几何中最大值加上B集合中最大值的最小化值;可以用贪心来找。。。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define mem(x,y) memset(x,y,sizeof(x))
#define T_T while(T--)
#define SI(x) scanf("%d",&x)
#define SL(x) scanf("%lld",&x)
typedef long long LL;
const int INF=0x3f3f3f3f;
const int MAXN=100010;
struct Node{
	int a,b;
	friend bool operator < (Node x,Node y){
		return x.a>y.a;
	}
};
Node dt[MAXN];
int main(){
	int T,N,kase=0;
	SI(T);
	T_T{
		SI(N);
		for(int i=0;i<N;i++)SI(dt[i].a),SI(dt[i].b);
		sort(dt,dt+N);
		int A,B=0,ans=INF;
		for(int i=0;i<N;i++){
			ans=min(ans,B+dt[i].a);
			B=max(dt[i].b,B);
		}
		printf("Case %d: %d\n",++kase,ans);
	}
	return 0;
}

  

时间: 2024-08-05 02:40:49

I can do it!(贪心)的相关文章

【uva 1615】Highway(算法效率--贪心 区间选点问题)

题意:给定平面上N个点和一个值D,要求在x轴上选出尽量少的点,使得对于给定的每个店,都有一个选出的点离它的欧几里德距离不超过D. 解法:先把问题转换成模型,把对平面的点满足条件的点在x轴的直线上可得到一个个区间,这样就是选最小的点覆盖所有的区间的问题了.我之前的一篇博文有较详细的解释:关于贪心算法的经典问题(算法效率 or 动态规划).代码实现我先空着.挖坑~

【贪心+Treap】BZOJ1691-[Usaco2007 Dec]挑剔的美食家

[题目大意] 有n头奶牛m种牧草,每种牧草有它的价格和鲜嫩度.每头奶牛要求它的牧草的鲜嫩度要不低于一个值,价格也不低于一个值.每种牧草只会被一头牛选择.问最少要多少钱? [思路] 显然的贪心,把奶牛和牧草都按照鲜嫩度由大到小排序,对于每奶牛把鲜嫩度大于它的都扔进treap,然后找出后继. 不过注意后继的概念是大于它且最小的,然而我们这里是可以等于的,所以应该是找cow[i].fresh-1的后继,注意一下…… 1 #include<iostream> 2 #include<cstdio&

POJ1017 Packets(贪心算法训练)

Time Limit: 1000MS          Memory Limit: 10000K          Total Submissions: 51306          Accepted: 17391 Description A factory produces products packed in square packets of the same height h and of the sizes 1*1, 2*2, 3*3, 4*4, 5*5, 6*6. These pro

ZOJ 3946 Highway Project 贪心+最短路

题目链接: http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=3946 题解: 用dijkstra跑单元最短路径,如果对于顶点v,存在一系列边(ui,v)使得dis[v]最小(dis[v]表示0到v的距离).这些边能且只能选一条,那么我们自然应该选cost最小的那个边了. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #inc

CoderForce 140C-New Year Snowmen(贪心)

题目大意:有n个已知半径的雪球.堆一个雪人需要三个尺寸不同的雪球,问用这些雪球最多能堆多少个雪人? 题目分析:先统计一下每种尺寸的球的个数,从三种最多的种类中各取出一个堆成雪人,这样贪心能保证的到的数目最多. 代码如下: # include<iostream> # include<map> # include<vector> # include<cstdio> # include<queue> # include<algorithm>

计蒜客 跳跃游戏(贪心)

给定一个非负整数数组,假定你的初始位置为数组第一个下标.数组中的每个元素代表你在那个位置能够跳跃的最大长度. 请确认你是否能够跳跃到数组的最后一个下标. 例如: A = [2,3,1,1,4], return ture A = [3,2,1,0,4], return false. 格式: 第一行输入一个正整数n,接下来的一行,输入数组A[n].如果能跳到最后一个下标,输出"true",否则输出"false" 样例1 ????输入:???? ????????5 ???

BZOJ 2525 [Poi2011]Dynamite 二分+树形贪心

题意: n个点,一棵树,有些点是关键点,可以将m个点染色. 求所有关键点到最近的被染色点的距离的最大值最小. 解析: 反正从这道题我是学了一种做题思路? 大爷讲课的时候说的:一般选择某些点的代价相同的话都是贪心,代价不同的话一般都是DP. 想想也挺对的,不过就是没有感悟到过? 反正这题考试的时候我是直接D了贪心的- -! 忘了为啥D了. 显然最大值最小我们需要二分一下这个值. 然后接下来我们从下往上扫整棵树. 节点的状态有几个? 第一种是 子树内没有不被覆盖的关键点,并且子树中有一个节点的贡献可

CF 500 C. New Year Book Reading 贪心 简单题

New Year is coming, and Jaehyun decided to read many books during 2015, unlike this year. He has n books numbered by integers from 1 to n. The weight of the i-th (1 ≤ i ≤ n) book is wi. As Jaehyun's house is not large enough to have a bookshelf, he k

贪心 --- HNU 13320 Please, go first

Please, go first Problem's Link: http://acm.hnu.cn/online/?action=problem&type=show&id=13320 Mean: n个人一起去滑雪,要坐电梯到山顶,电梯每5分钟可以送一个人上去.这n个人中有的是组好团一起滑雪的,所以要等到齐了才能开始滑. 但是他们到达电梯下的时间都不是统一的,对于单独一个人来说,他在山下等和在山上等的时间都是一样的. 但是对于n个人的集体来说,如果让他后面的人先上去,这样可能会更节省时间.

二分+贪心

上海邀请赛热身时候,C题是一个二分+贪心的题目.起初并不会,问了旁边的复旦大神.这几天无意发现VJ上一个专题.擦原来是一个经典类型. 二分+贪心 这类题目注意数据范围,1e8,1e9一般都是这样. 注意事项 二分法有很多写法,推荐用lf+1 < rf的写法.这个也符合计算机中数据存取的原则.对于浮点数,直接就循环100次,精度绝对够. 一般有两种类型,一种是询问最优,即数列中无重复.一种是多个即lower_bound ,upper_bound这类函数问题. 贪心使用,就是这个问题枚举答案可被验证