51nod-1420-greedy

Topic source:  CodeForces
Baseline Time Limit: 1 second Space Limit: 131072 KB Score: 40  Difficulty: Level 4 Algorithm Questions
 collect
 focus on

There are n kangaroos. The size of each kangaroo is represented by an integer. A wallaby can fit in a large kangaroo only if the large kangaroo is at least twice the size of the wallaby.

Each kangaroo can hold up to one kangaroo. Once a wallaby has been placed in a large kangaroo, it cannot contain other kangaroos.

Once the wallaby is packed into the big kangaroo, it cannot be seen by us. Find a way to pack kangaroos so that the fewest kangaroos are seen.

Input
A single set of test data.
The first line contains an integer n (1≤n≤5*10^5).
The next n lines, each with an integer si, represents the size of the ith kangaroo (1≤si≤10^5).
Output
Output an integer, the minimum number of kangaroos that can be seen.
Input example
8
2
5
7
6
9
8
4
2
Output example
5 At 

    least (n+1)/2 kangaroos can be seen, because each kangaroo can hold at most one kangaroo. Therefore, after sorting, consider using the second half to install the first half. For greedy consideration, we should use the big kangaroo to install the largest possible small kangaroo.

 1 #include<iostream>
 2 #include<cstring>
 3 #include<queue>
 4 #include<cstdio>
 5 #include<stack>
 6 #include<set>
 7 #include<cmath>
 8 #include<ctime>
 9 #include<time.h> 
10 #include<algorithm>
11 using namespace std;
12 #define mp make_pair
13 #define debug puts("debug")
14 #define LL long long 
15 #define pii pair<int,int>
16 vector<LL>a;
17 bool vis[500050];
18 int main(){
19     LL n,i,j,k;
20     cin>>n;
21     for(i=1;i<=n;++i){
22         scanf("%lld",&k);
23         a.push_back(k);
24     }
25     sort(a.begin(),a.end());
26     for(i=n/2-1;i>=0;--i){
27         if(a[i]*2<=a[n-1]){
28             n--;
29         }
30     }
31     cout<<n<<endl;
32     return 0;
33 }

 

 
 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324861631&siteId=291194637