Codeforces Round #219 (Div. 2)--C. Counting Kangaroos is Fun
2016-02-11 22:09
288 查看
C. Counting Kangaroos is Fun
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
There are n kangaroos with pockets. Each kangaroo has a size (integer number). A kangaroo can go into another kangaroo's pocket if
and only if the size of kangaroo who hold the kangaroo is at least twice as large as the size of kangaroo who is held.
Each kangaroo can hold at most one kangaroo, and the kangaroo who is held by another kangaroo cannot hold any kangaroos.
The kangaroo who is held by another kangaroo cannot be visible from outside. Please, find a plan of holding kangaroos with the minimal number of kangaroos who is visible.
Input
The first line contains a single integer — n (1 ≤ n ≤ 5·105).
Each of the next n lines contains an integer si —
the size of the i-th kangaroo (1 ≤ si ≤ 105).
Output
Output a single integer — the optimal number of visible kangaroos.
Sample test(s)
input
output
input
output
大体题意:
给你n个袋鼠的口袋大小,当一个袋鼠的口袋大于等于另一个袋鼠口袋2倍时,那个小袋鼠就可以跳入大袋鼠的袋
子里,并且跳入其他的袋鼠口袋的袋鼠不能再装其他袋鼠了。问最后有几只袋鼠。
思路:
显然是个贪心题目,最大极限情况就是前一半个袋鼠全部能装入后一半个袋鼠,所以循环最多只循环n/2 次就可以了!
控制前一半,扫描后一半,一个一个判断,假设前一半可用a[l]表示,后一半可用a[r]表示,则发现2 * a[l] <= a[r]后就可以sum--,l++,依次找下去!
代码如下:
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
There are n kangaroos with pockets. Each kangaroo has a size (integer number). A kangaroo can go into another kangaroo's pocket if
and only if the size of kangaroo who hold the kangaroo is at least twice as large as the size of kangaroo who is held.
Each kangaroo can hold at most one kangaroo, and the kangaroo who is held by another kangaroo cannot hold any kangaroos.
The kangaroo who is held by another kangaroo cannot be visible from outside. Please, find a plan of holding kangaroos with the minimal number of kangaroos who is visible.
Input
The first line contains a single integer — n (1 ≤ n ≤ 5·105).
Each of the next n lines contains an integer si —
the size of the i-th kangaroo (1 ≤ si ≤ 105).
Output
Output a single integer — the optimal number of visible kangaroos.
Sample test(s)
input
8 2 5 7 6 9 8 4 2
output
5
input
8
9
1
6
2
6
58
3
output
5
大体题意:
给你n个袋鼠的口袋大小,当一个袋鼠的口袋大于等于另一个袋鼠口袋2倍时,那个小袋鼠就可以跳入大袋鼠的袋
子里,并且跳入其他的袋鼠口袋的袋鼠不能再装其他袋鼠了。问最后有几只袋鼠。
思路:
显然是个贪心题目,最大极限情况就是前一半个袋鼠全部能装入后一半个袋鼠,所以循环最多只循环n/2 次就可以了!
控制前一半,扫描后一半,一个一个判断,假设前一半可用a[l]表示,后一半可用a[r]表示,则发现2 * a[l] <= a[r]后就可以sum--,l++,依次找下去!
代码如下:
#include<stack> #include<string> #include<queue> #include<vector> #include<algorithm> #include<cstring> #include<cmath> #include<cstdlib> #include<cctype> #include<set> #include<map> #include<cstdio> #include<iostream> #define mem(x) memset(x,0,sizeof(x)); #define mem1(x) memset(x,-1,sizeof(x)); using namespace std; const int maxn = 500000 + 10; const int maxt = 100 + 10; const int INF = 1e8; const double eps = 1e-8; const double pi = acos(-1.0); int a[maxn]; int main() { int n; while(scanf("%d",&n) == 1 && n){ for (int i = 0; i < n; ++i)scanf("%d",&a[i]); sort(a,a+n); int sum=n; for (int r=n/2,l=0;r<n && l<n/2;++r) if (a[r]>=2*a[l]){--sum;++l;} printf("%d\n",sum); } return 0; }
相关文章推荐
- Android开发学习之路--UI之简单聊天界面
- JAVA比较两个日期相差的天数
- FPS的Log代码
- iOS之延时器(NSTimer)
- 初始HTTP和CSS第一篇span的练习
- poj3616 Milking Time 入门dp
- 198. House Robber LeetCode
- BZOJ3676: [Apio2014]回文串
- [linux] Linux的bg和fg命令简单介绍
- 三层ViewPager嵌套 的事件处理
- 静态库和动态库的使用
- Tomcat
- Android辅助功能AccessibilityService与抢红包辅助
- 欢迎使用CSDN-markdown编辑器
- HDU 1029 基础dp
- PCB设计之原理图绘制笔记
- socket 中的read
- iOS之运行时(Runtime)
- 2016/02/11
- Arcgis server java develop begining