Codeforece#219(DIV2) C题

来源:互联网 发布:天津市软件学院答辩 编辑:程序博客网 时间:2024/04/28 02:23
C. Counting Kangaroos is Fun
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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 nextn 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.

题目大意:给定一个长度为n的数组,如果一只袋鼠的大小小于另一只,则将它放入另一只里面,已经放了袋鼠的就不能在放了。问最后最少还有多少只袋鼠。

思路:贪心,先将数组按升序排序,然后两个指针分别指向l=0和r=n/2,初始化总是为ans=n

        自增r的过程中,如果a[l]*2<=a[r],则l++,ans--;

由于最多只能装一只袋鼠,所以最少也会有n/2只袋鼠,故r=n/2开始,l=0开始,终止条件为l<n/2&&r<n

/*    @author : liuwen*/#include <iostream>#include <algorithm>#include <cstring>#include <cstdio>#include <cstdlib>#include <queue>#include <stack>#include <map>#include <vector>#include <cmath>using namespace std;const int maxn=5000000+5;bool cmp(const int &a,const int &b){    return a<b;}int a[maxn],vis[maxn];int main(){   // freopen("in.txt","r",stdin);    int n;    while(scanf("%d",&n)==1){        memset(a,0,sizeof(a));        for(int i=0;i<n;i++){            scanf("%d",&a[i]);        }        sort(a,a+n,cmp);        int l,r,ans;        ans=n,l=0,r=n/2;        for(l=0,r=n/2;l<n/2&&r<n;r++){             if(a[l]*2<=a[r]){                ans--;                l++;            }        }        cout<<ans<<endl;    }    return 0;}


0 0
原创粉丝点击