cf23C Oranges and Apples (贪心_好题)
2016-03-29 12:43
344 查看
给你2*n-1个篮子,分别装有若干苹果跟橘子,问取出n个篮子,使得苹果的总量占苹果sum的一半以上,且橘子也占橘子sum二等一半以上
仔细观察数据,你会发现这是一个贪心策略、。。。
我们一开始排序会想到,苹果从大往小排,去前n个,但不能保证橘子能占一半以上...
那么如果有两个橘子,我们当然取最大的最好,那么如果我们每次取橘子最大的,并舍弃另一个,那么橘子能保证了,但是苹果不能保证....
由此,我们想到了苹果从小到大排,那么两者窦娥能保证了....
Description
In 2N - 1 boxes there are apples and oranges. Your task is to choose N boxes so, that they will contain not less than half of
all the apples and not less than half of all the oranges.
Input
The first input line contains one number T — amount of tests. The description of each test starts with a natural number N —
amount of boxes. Each of the following 2N - 1 lines contains numbers ai and oi —
amount of apples and oranges in the i-th box (0 ≤ ai, oi ≤ 109).
The sum of N in all the tests in the input doesn't exceed 105. All the input numbers are integer.
Output
For each test output two lines. In the first line output YES, if it's possible to choose N boxes, or NO otherwise.
If the answer is positive output in the second line N numbers — indexes of the chosen boxes. Boxes are numbered from 1 in the input order. Otherwise leave the second line empty. Separate the
numbers with one space.
Sample Input
Input
Output
仔细观察数据,你会发现这是一个贪心策略、。。。
我们一开始排序会想到,苹果从大往小排,去前n个,但不能保证橘子能占一半以上...
那么如果有两个橘子,我们当然取最大的最好,那么如果我们每次取橘子最大的,并舍弃另一个,那么橘子能保证了,但是苹果不能保证....
由此,我们想到了苹果从小到大排,那么两者窦娥能保证了....
Description
In 2N - 1 boxes there are apples and oranges. Your task is to choose N boxes so, that they will contain not less than half of
all the apples and not less than half of all the oranges.
Input
The first input line contains one number T — amount of tests. The description of each test starts with a natural number N —
amount of boxes. Each of the following 2N - 1 lines contains numbers ai and oi —
amount of apples and oranges in the i-th box (0 ≤ ai, oi ≤ 109).
The sum of N in all the tests in the input doesn't exceed 105. All the input numbers are integer.
Output
For each test output two lines. In the first line output YES, if it's possible to choose N boxes, or NO otherwise.
If the answer is positive output in the second line N numbers — indexes of the chosen boxes. Boxes are numbered from 1 in the input order. Otherwise leave the second line empty. Separate the
numbers with one space.
Sample Input
Input
2 2 10 15 5 7 20 18 1 0 0
Output
YES 1 3 YES 1
#include<bits/stdc++.h> using namespace std; const int N = 2*1e5+10; struct node { int x; int y; int index; }a ; bool cmp(node t1,node t2) { if(t1.x<t2.x) return true; return false; } int main() { int T,i,j,n; scanf("%d",&T); while(T--) { scanf("%d",&n); for(i=1;i<=n+n-1;i++) { scanf("%d%d",&a[i].x,&a[i].y); a[i].index=i; } sort(a+1,a+1+n+n-1,cmp); printf("YES\n"); for(i=2;i<n+n-1;i=i+2) { if(a[i].y>a[i-1].y) printf("%d ",a[i].index); else printf("%d ",a[i-1].index); } printf("%d\n",a[n+n-1].index); } return 0; }
相关文章推荐
- Android自定义标题时出现You cannot combine custom titles with other title features
- 【Android应用开发】 OpenGL ES -- 透视投影 和 正交投影
- applicationIconBadgeNumber不显示
- Android Studio 添加注释模板
- android的task任务栈
- android 沉浸模式(状态栏透明 跟随布局颜色)
- Android 播放视频(三)媒体支持格式
- iOS开发学习路径的一些建议
- 中文 iOS/Mac 开发博客列表
- Objective-C编码规范[译]
- 将中文转为拼音 iOS
- cocos2d 3.10环境搭建
- IOS开发前的准备
- HTML5[5]:在移动端禁用长按选中文本功能
- android中.9.png的认识
- Ubuntu QML中的WebView及HTML混合编程(hybrid programming)
- iOS实现textfield随键盘移动
- IOS学习——苹果开发者类型
- iOS 画虚线的重新理解
- Android studio 百度地图开发(2)地图定位