0
点赞
收藏
分享

微信扫一扫

Codeforces-Counting Kangaroos is Fun【贪心】【二分】

8052cf60ff5c 2022-08-24 阅读 68



Counting Kangaroos is Fun


Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u


Submit  ​​Status​​


Description



There are n

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>
#include<algorithm>
#define Q 500000+10
using namespace std;
int n,a[Q];
int main()
{
while(~scanf("%d",&n))
{
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
sort(a,a+n);
int ans=n-1;
for(int i=n/2-1;i>=0;i--)
{
if(a[i]*2<=a[ans]) ans--;
}
printf("%d\n",ans+1);
}
return 0;
}



代码2:

#include<iostream>  
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<list>
#include<functional>
using namespace std;
const int maxn=1000010;
int num[maxn],temp[maxn],n;
bool judge(int mid){
if(mid<(n+1)/2)return false;
for(int i=mid+1;i<=n;++i){
if(num[i]*2>num[i-mid])return false;
}
return true;
}
bool cmp(int a,int b){
return a>b;
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;++i){
scanf("%d",&num[i]);
}
sort(num+1,num+n+1,cmp);
int left=1,right=n,ans;
while(left<=right){
int mid=(left+right)/2;
if(judge(mid)){
ans=mid;
right=mid-1;
}
else {
left=mid+1;
}
}
printf("%d\n",ans);
return 0;
}



举报

相关推荐

0 条评论