• 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 }
    ——将来的你会感谢现在努力的自己。
  • 相关阅读:
    离线获取docker镜像(docker save vs load)&docker export vs import& docker commit
    深入分析JavaWeb的中文编码问题
    python中Django的基本使用
    element+springboot实现简单的商品管理
    springboot+thymeleaf自定义标签
    springboot整合shiro
    linux的namespace、docker网络模式
    Docker Compose、Swarm 集群管理
    iOS Share Extension 自定义分享界面
    Mixin Messenger 源码解读 1 — — WCDB Swift
  • 原文地址:https://www.cnblogs.com/yexiaozi/p/5712740.html
Copyright © 2020-2023  润新知