zoukankan      html  css  js  c++  java
  • Counting Kangaroos is Fun 求最少可见袋鼠数

    Description

    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.

    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>
     2 #include<algorithm>
     3 using namespace std;
     4 int n,i,a[500000+11],ri,le,mid,ans;
     5 bool f(int x)
     6 {
     7     int i;
     8     for(i = mid ; i < n ; i++)
     9     {
    10         if(a[i]*2 > a[i-mid])
    11             return 0;
    12     }
    13     return 1;
    14 }
    15 bool cmp(int a,int b)
    16 {
    17     return a>b;
    18 }
    19 int main()
    20 {
    21     while(scanf("%d",&n)!=EOF)
    22     {
    23         for(i = 0 ; i < n ; i++)
    24         {
    25             scanf("%d",&a[i]);
    26         }
    27         sort(a,a+n,cmp);
    28         le=(n+1)/2;
    29         ri=n;
    30         while(le <= ri)
    31         {
    32             mid=(le + ri) / 2;
    33             if(f(mid))
    34             {
    35                 ans=mid;
    36                 ri=mid-1;
    37             }
    38             else
    39             {
    40                 le=mid+1;
    41             }
    42         }
    43         printf("%d
    ",ans);
    44     }
    45 }

     贪心

     1 #include<cstdio>
     2 #include<algorithm>
     3 using namespace std;
     4 int main()
     5 {
     6     int ans,n,i,a[500000+11];
     7     while(scanf("%d",&n)!=EOF)
     8     {
     9         for(i = 0 ; i < n ; i++)
    10         {
    11             scanf("%d",&a[i]);
    12         }
    13         sort(a,a+n);
    14         ans=n;
    15         for(i = n/2-1 ; i >= 0 ; i-- )
    16         {
    17             if(a[i]*2 <= a[n-1])
    18             {
    19                 ans--;
    20                 n--;
    21             }
    22         }
    23         printf("%d
    ",ans);
    24     }
    25 }
    ——将来的你会感谢现在努力的自己。
  • 相关阅读:
    之前没弄明白的链表和return
    读Bsautiful Soup库有感
    Beautiful Soup库
    XML和JSON的系列操作
    urllib和requests的区别(持续更新)
    request请求把cookie当作参数传入请求
    Python统计文件夹下文件的个数
    基础算法之查找
    timeit用法(不完整)
    spring初识
  • 原文地址:https://www.cnblogs.com/yexiaozi/p/5712740.html
Copyright © 2011-2022 走看看