Counting Kangaroos is Fun 求最少可见袋鼠数

袋鼠装袋问题
本文探讨了一种有趣的算法问题——袋鼠装袋问题。给定一系列不同大小的袋鼠,目标是确定最少数量的可见袋鼠,使得每个较小的袋鼠能够被至少两倍大的袋鼠装入。通过两种算法解决此问题:一种使用二分法,另一种采用贪心策略。

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<cstdio>
 2 #include<algorithm>
 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             else
39             {
40                 le=mid+1;
41             }
42         }
43         printf("%d\n",ans);
44     }
45 }

 贪心

 1 #include<cstdio>
 2 #include<algorithm>
 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值