Codeforece#219(DIV2) C题
来源:互联网 发布:天津市软件学院答辩 编辑:程序博客网 时间:2024/04/28 02:23
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.
The first line contains a single integer — n(1 ≤ n ≤ 5·105). Each of the nextn lines contains an integer si — the size of the i-th kangaroo (1 ≤ si ≤ 105).
Output a single integer — the optimal number of visible kangaroos.
题目大意:给定一个长度为n的数组,如果一只袋鼠的大小小于另一只,则将它放入另一只里面,已经放了袋鼠的就不能在放了。问最后最少还有多少只袋鼠。
思路:贪心,先将数组按升序排序,然后两个指针分别指向l=0和r=n/2,初始化总是为ans=n
自增r的过程中,如果a[l]*2<=a[r],则l++,ans--;
由于最多只能装一只袋鼠,所以最少也会有n/2只袋鼠,故r=n/2开始,l=0开始,终止条件为l<n/2&&r<n
/* @author : liuwen*/#include <iostream>#include <algorithm>#include <cstring>#include <cstdio>#include <cstdlib>#include <queue>#include <stack>#include <map>#include <vector>#include <cmath>using namespace std;const int maxn=5000000+5;bool cmp(const int &a,const int &b){ return a<b;}int a[maxn],vis[maxn];int main(){ // freopen("in.txt","r",stdin); int n; while(scanf("%d",&n)==1){ memset(a,0,sizeof(a)); for(int i=0;i<n;i++){ scanf("%d",&a[i]); } sort(a,a+n,cmp); int l,r,ans; ans=n,l=0,r=n/2; for(l=0,r=n/2;l<n/2&&r<n;r++){ if(a[l]*2<=a[r]){ ans--; l++; } } cout<<ans<<endl; } return 0;}
- Codeforece#219(DIV2) C题
- codeforece 18c
- CodeForece 724C Ray Tracing 扩展欧几里得
- codeforces147 div2 C题
- CF Div2-206-C题
- codeforces 260 div2 C题
- cf div2(262)c题
- Div2(317C)Div2(219) B 二分法判定
- CF-Div2-207-C题+线段树
- codeforce round #295(div2) C题
- 【套题】Codeforces#306(div2)[A-C]
- 思路题 codeforces div2 C Replacement
- codeforce 343div2 C dp好题
- CF#345 div2 A\B\C题
- Codeforces Div2 2016.08.29 C题
- cf div2 370 C题贪心
- Codeforces Round #402 (Div2)C题
- CF round 419 (div2)C题
- Struts2.0中ActionInvocation使用
- linux高级编程的理解
- 关于ARM的22个常用概念
- SQLServer As
- CString string 的相互转化,宽字符,多字符的转换
- Codeforece#219(DIV2) C题
- Cookie的基础知识
- 20131214-第七天
- 在异常处理中处理析构函数
- ++i和i++效率谁高
- android scrollview 嵌套listview 或者listview嵌套listview 计算子view高度
- Fedora18设置终端快捷键 和 桌面快捷方式
- UltraGrid中绑定UltraTextEditor在AfterEditorButtonCloseUp事件中获得所属UltraGridCell
- 使用管道批量删除deb