Problem Description
Problem Analysis
Given n kangaroos and their corresponding sizes, put the smaller ones into the larger ones’ pockets, and determine the minimum number of kangaroos left with their heads outside
The condition for being put inside is a[i]*2<=a[j]
First sort the array from smallest to largest
If we want x kangaroos outside
then a[0]~a[n-x] must be packed
so we can use this to check
and perform Binary Search
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
| #include<stdio.h>
#include<algorithm>
using namespace std;
int a[500200];
int n;
bool judge(int x) {
if(x<(n+1)/2)
return false;
for(int i=0; i<n-x; i++) {
if(a[i]*2>a[i+x])
return false;
}
return true;
}
int main() {
while(scanf("%d",&n)!=EOF) {
for(int i=0; i<n; i++)
scanf("%d",&a[i]);
sort(a,a+n);
int l=1,r=n,res;
while(l<=r) {
int mid=(l+r)/2;
if(judge(mid)) {
res=mid;
r=mid-1;
} else
l=mid+1;
}
printf("%d\n",res);
}
return 0;
}
|