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个袋鼠每个袋鼠的口袋里可以放一只体重小于其体重的小于它二分之一重量的袋鼠现在将一些袋鼠放进其它的袋鼠口袋里问最多能见到多少袋鼠。
二分法
#include<cstdio>
#include<algorithm>
using namespace std;
int n,i,a[+],ri,le,mid,ans;
bool f(int x)
{
int i;
for(i = mid ; i < n ; i++)
{
if(a[i]* > a[i-mid])
return ;
}
return ;
}
bool cmp(int a,int b)
{
return a>b;
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
for(i = ; i < n ; i++)
{
scanf("%d",&a[i]);
}
sort(a,a+n,cmp);
le=(n+)/;
ri=n;
while(le <= ri)
{
mid=(le + ri) / ;
if(f(mid))
{
ans=mid;
ri=mid-;
}
else
{
le=mid+;
}
}
printf("%d\n",ans);
}
}
贪心
#include<cstdio>
#include<algorithm>
using namespace std;
int main()
{
int ans,n,i,a[+];
while(scanf("%d",&n)!=EOF)
{
for(i = ; i < n ; i++)
{
scanf("%d",&a[i]);
}
sort(a,a+n);
ans=n;
for(i = n/- ; i >= ; i-- )
{
if(a[i]* <= a[n-])
{
ans--;
n--;
}
}
printf("%d\n",ans);
}
}
Counting Kangaroos is Fun 求最少可见袋鼠数的更多相关文章
- pku oj overhang叠加卡片求最少的卡片数
这个估计是里面第二简单的了,因为第一简单的是求a+b 哈哈,一submit就ac了 题目如下: Description How far can you make a stack of cards ov ...
- CodeForces 372 A. Counting Kangaroos is Fun
题意,有n只袋鼠,没每只袋鼠有个袋子,大小为si,一个袋鼠可以进入另外一个袋鼠的袋子里面,当且仅当另一个袋鼠的袋子是他的二倍或二倍一上,然后中国袋鼠就是不可见的,不能出现多个袋鼠嵌套的情况.让你求最少 ...
- HDU 1326 Box of Bricks(水~平均高度求最少移动砖)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1326 题目大意: 给n堵墙,每个墙的高度不同,求最少移动多少块转使得墙的的高度相同. 解题思路: 找到 ...
- poj 3352 Road Construction【边双连通求最少加多少条边使图双连通&&缩点】
Road Construction Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10141 Accepted: 503 ...
- 输入一个数字n 如果n为偶数则除以2,若为奇数则加1或者减1,直到n为1,求最少次数 写出一个函数
题目: 输入一个数字n 如果n为偶数则除以2,若为奇数则加1或者减1,直到n为1,求最少次数 写出一个函数 首先,这道题肯定可以用动态规划来解, n为整数时,n的解为 n/2 的解加1 n为奇数时 ...
- [Leetcode 216]求给定和的数集合 Combination Sum III
[题目] Find all possible combinations of k numbers that add up to a number n, given that only numbers ...
- 我的第一篇博客:不用sizeof求int的bit数
我的第一篇博客.. 还不会什么高端的东西就来点基础的. 不用sizeof求int的bit数 //不用sizeof求int的bit数 #include<stdio.h> int main( ...
- hdu 4587 2013南京邀请赛B题/ / 求割点后连通分量数变形。
题意:求一个无向图的,去掉两个不同的点后最多有几个连通分量. 思路:枚举每个点,假设去掉该点,然后对图求割点后连通分量数,更新最大的即可.算法相对简单,但是注意几个细节: 1:原图可能不连通. 2:有 ...
- java笔试之求最大连续bit数
功能: 求一个byte数字对应的二进制数字中1的最大连续数,例如3的二进制为00000011,最大连续2个1 输入: 一个byte型的数字 输出: 无 返回: 对应的二进制数字中1 ...
随机推荐
- iOS [CIContext initWithOptions:]: unrecognized selector sent to instance 模拟器 iOS 8.4
在模拟器(iPhone 4s,iOS 8.4)中运行应用时, 应用crash在了使用CIContext(options:nil) 这个API的一个纯Swift第三方库. StackOverFlow的解 ...
- offsetLeft 与 left的区别
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- threading多线程模块
1 基本实现 Thread(target=函数名,args=(以元组形式传递的实参,要加",")) th = threading.Thread(target=run,args=(i ...
- 微信BUG之微信内置的浏览器中window.location.href 不跳转
最近做微信开发遇到这个问题,查了一些文档,总结一下 1.url后面加参数 indow.location.href = url +'?timestamp='+ new Date().getTime()+ ...
- php连接mysql并读取数据
<?php $server_name="localhost:3306"; //数据库服务器名称 $username="root"; // 连接数据库用户名 ...
- 自己写一个websocket
import socket, base64, hashlib sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.setsock ...
- 转 Oracle中merge into的使用
http://www.cnblogs.com/highriver/archive/2011/08/02/2125043.html
- .NET Core WebAPI Swagger使用
相对于普通的webapi而言,.net core webapi本身并不具备文档的功能,所以可以借助第三方插件:swagger,使用的话很简单. 步骤一. Nuget Packages安装,使用程序包管 ...
- Dapper系列之三:Dapper的事务修改与删除
Dapepr的Update和Delete Dapper入门Dapper查询 上两篇文章我们介绍Dapper中添加和查询.本篇文章我们继续讲解修改和删除....如果本篇文章看不懂,请看阅读上两篇Dapp ...
- .net excel 导入 导出
哎,好好的代码今天说来个实验,结果用的是office15 气死人了,网上最高office14.dll 文章转自2012年 QQ群:13615607 MR.Young protected void Bt ...