博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Counting Kangaroos is Fun 求最少可见袋鼠数
阅读量:6877 次
发布时间:2019-06-26

本文共 2416 字,大约阅读时间需要 8 分钟。

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 5 8 3

Output

5

有N个袋鼠每个袋鼠的口袋里可以放一只体重小于其体重的小于它二分之一重量的袋鼠现在将一些袋鼠放进其它的袋鼠口袋里问最多能见到多少袋鼠。

二分法
1 #include
2 #include
3 using namespace std; 4 int n,i,a[500000+11],ri,le,mid,ans; 5 bool f(int x) 6 { 7 int i; 8 for(i = mid ; i < n ; i++) 9 {10 if(a[i]*2 > a[i-mid])11 return 0;12 }13 return 1;14 }15 bool cmp(int a,int b)16 {17 return a>b;18 }19 int main()20 {21 while(scanf("%d",&n)!=EOF)22 {23 for(i = 0 ; i < n ; i++)24 {25 scanf("%d",&a[i]);26 }27 sort(a,a+n,cmp);28 le=(n+1)/2;29 ri=n;30 while(le <= ri)31 {32 mid=(le + ri) / 2;33 if(f(mid))34 {35 ans=mid;36 ri=mid-1;37 }38 else39 {40 le=mid+1;41 }42 }43 printf("%d\n",ans);44 }45 }

 贪心

1 #include
2 #include
3 using namespace std; 4 int main() 5 { 6 int ans,n,i,a[500000+11]; 7 while(scanf("%d",&n)!=EOF) 8 { 9 for(i = 0 ; i < n ; i++)10 {11 scanf("%d",&a[i]);12 }13 sort(a,a+n);14 ans=n;15 for(i = n/2-1 ; i >= 0 ; i-- )16 {17 if(a[i]*2 <= a[n-1])18 {19 ans--;20 n--;21 }22 }23 printf("%d\n",ans);24 }25 }

 

转载于:https://www.cnblogs.com/yexiaozi/p/5712740.html

你可能感兴趣的文章
转载/验证码
查看>>
Surface、SurfaceView、SurfaceHolder和SurfaceHolder.Callback之间的联系
查看>>
什么是Data Store and Data Collector?
查看>>
我的友情链接
查看>>
php培训11.30
查看>>
Effective Java读后感
查看>>
windows下两个无线网卡 一个内网 一个外网
查看>>
tcp nat 负载均衡
查看>>
起点,游戏开发的梦想与技能点
查看>>
MPLS 流量工程的配置与各大属性调整详解
查看>>
107个常用Javascript语句
查看>>
我的友情链接
查看>>
Dataram_RAMDisk_v4_0_0安装和配置
查看>>
在window XP下使用vsphere client 5.5 访问vCenter 或者 ESXi5.5 连接错误
查看>>
35 个超棒的 Coming Soon 页面设计案例
查看>>
C语言第四天(位运算)
查看>>
硬RAID可以为NVMe SSD数据可靠性保驾护航吗?
查看>>
iPad 2 移植Siri 新手完全教程 适用所有越狱设备
查看>>
编程题:用函数实现,用户输入年月日,来计算出该日期为当年第几天?
查看>>
Pro Android学习笔记(十一):了解Intent(中)
查看>>