Codeforece#219(DIV2) C題

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

題目大意:給定一個長度爲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;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章