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 ",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 ",ans); 24 } 25 }