您的位置:首页 > 其它

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
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: