CodeForces 372A Counting Kangaroos is Fun袋鼠口袋问题折半搜索
2016-07-27 08:58
323 查看
Counting Kangaroos is Fun
Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d
& %I64u
Submit Status
Description
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 Input
Input
Output
Input
Output
Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d
& %I64u
Submit Status
Description
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 Input
Input
8 2 5 7 6 9 8 4 2
Output
5
Input
8
9
1
6
2
6
58
3
Output
5
题意说是有n个袋鼠,口袋A<=2*口袋B,那么这个小袋鼠能被大的袋鼠装下,现在让尽量多的袋鼠被装下,求这个状态的最小值;先将所有袋鼠按口袋大小排序,然后贪心搜索
#include<stdio.h> #include<algorithm> #include<iostream> using namespace std; int a[1000000]; int main() { int n; while(~scanf("%d",&n)) { for(int i=0;i<n;i++) scanf("%d",&a[i]); sort(a,a+n); int p=n-1; for (int i =n/2-1;i>=0; i--)//将排好序的袋鼠分为两部分,然后枚举搜索 { if (a[i]*2<=a[p]) { p--; } } printf("%d\n",p+1); } }
相关文章推荐
- 解决nginx负载均衡的session共享问题
- 系统基础之命令历史及快捷键使用
- 数据库测试
- Android:学习AIDL,这一篇文章就够了(下)
- Spring框架使用ByName自动注入同名问题剖析
- POJ:2507 Crossed ladders(二分+几何数学)
- 消息队列
- Android之ViewPagerindicator
- Emmet插件神奇用法
- javascript简单实现等比例缩小图片的方法
- win10 uwp 入门
- STL之Map:Map的基本用法
- 使用logging模块发送邮件
- hdoj-4135-Co-prime
- VC++中subclassdlgitem函数的功能及用法
- Nginx负载均衡的4种方案配置实例
- 实习见闻(5)——mysql解压版的安装与环境变量搭建
- 给The Lab Renderer for Unity中地形添加阴影
- iOS滑动视图通过偏移量判断显示第几个item
- linux启动时间测试