SPOJ:Decreasing Number of Visible Box(不错的,背包?贪心?)
Shadowman loves to collect box but his roommates woogieman and itman don't like box and so shadowman wants to hide boxes as many as possible. A box can be kept hidden inside of another box if and only if the box in which it will be held is empty and the size of the box is at least twice as large as the size of the box.
Print the minimum number of box that can be shown.
Input
The input set starts with single line integer T (1<=T<=50) the number of test cases. Then following T cases starts with an integer N (1<=N<=100000) denoting the number of box. The next line contains N space separated positive integer. i-th of them contains a numbers Ai(1<=Ai<=100000) size of the i-th box.
Output
Output the the case number and the minimum number of box that can be shown.
Example
Input:
2
4
1 2 4 8
4
1 3 4 5 Output:
Case 1: 1
Case 2: 3
题意:给定N个盒子,现在每个盒子最多可以装一个盒子到内部,而且要满足外面的盒子体积大于等于内部的盒子的两倍。
思路:开始以为是平衡树,每次在树上找到一个大于等于两倍体积的点,减减。但是复杂度太高,优化看一下也超时。最后又是手欠看了别人的代码。
方法大概是,先排序,然后新建一个数组q,代表目前最小的盒子的两倍,如果后面的盒子大于这个q,就可以把q装进去,装一次代表答案减一。
(主要是利用了单调性,贪心地装目前最小的,秒啊。
#include<bits/stdc++.h>
using namespace std;
const int maxn=;
int a[maxn],q[maxn],head,tail;
int main()
{
int T,N,i,j,ans,Case=;
scanf("%d",&T);
while(T--){
head=tail=ans=;
scanf("%d",&N);
for(i=;i<=N;i++) scanf("%d",&a[i]);
sort(a+,a+N+);
for(i=;i<=N;i++){
if(head==tail) ans++;
else if(q[tail+]<=a[i]) tail++;
else ans++;
q[++head]=a[i]*;
}
printf("Case %d: %d\n",++Case,ans);
}
return ;
}
SPOJ:Decreasing Number of Visible Box(不错的,背包?贪心?)的更多相关文章
- Spoj-VISIBLEBOX Decreasing Number of Visible Box
Shadowman loves to collect box but his roommates woogieman and itman don't like box and so shadowman ...
- SPOJ:Strange Waca(不错的搜索&贪心&剪枝)
Waca loves maths,.. a lot. He always think that 1 is an unique number. After playing in hours, Waca ...
- 452. Minimum Number of Arrows to Burst Balloons——排序+贪心算法
There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...
- SPOJ Find the max XOR value(二进制,贪心即可)
You have two integers L and R, and you are required to find the max xor value of a and b where L < ...
- 数论 - 欧拉函数的运用 --- poj 3090 : Visible Lattice Points
Visible Lattice Points Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5636 Accepted: ...
- poj 3060 Visible Lattice Points
http://poj.org/problem?id=3090 Visible Lattice Points Time Limit: 1000MS Memory Limit: 65536K Tota ...
- P8 Visible Lattice Points
P8 Visible Lattice Points Time Limit:1000ms, Memory Limit:65536KB Description A lattice point (x ...
- 【POJ】3090 Visible Lattice Points(欧拉函数)
Visible Lattice Points Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7705 Accepted: ...
- POJ_3090 Visible Lattice Points 【欧拉函数 + 递推】
一.题目 A lattice point (x, y) in the first quadrant (x and y are integers greater than or equal to 0), ...
随机推荐
- Day 11 正则表达式
正则表达式 一.简介 Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配到的行打印出来.grep全称是Globally search for a Regular ...
- 数学知识巧学JCF(Java Collections framework)
不知你是否还记得高中我们学过的集合,映射,函数,数学确实很牛逼,拿它来研究java集合类,轻而易举的就把知识理解了.本篇文章适合初学java集合类的小白,也适合补充知识漏缺的学习者,同时也是面试者可以 ...
- NOIPSB评测机+SB题DAY2
忍者钩爪 题目描述 小 Q 是一名酷爱钩爪的忍者, 最喜欢飞檐走壁的感觉, 有一天小 Q 发现一个练习使用钩 爪的好地方,决定在这里大显身手. 场景的天花板可以被描述为一个无穷长的数轴, 初始小 Q ...
- Chrome 浏览器安装Vue插件方法 (十分详细)
博主最近在研究Vue,无奈新手想安装Chrome的Vue插件,整理下安装流程: 1.首先去github下载vue.zip文件插件(还有npm安装方法这里就不介绍了自行百度)下载地址:https://g ...
- 【RESTful】1.理解REST和RESTful
简记:一套设计良好的RESTful可以帮助互联网产品支持[单个服务端+多个客户端]的场景!!!
- 统计显著性(Statistical significance)
显著性,又称统计显著性(Statistical significance), 是指零假设为真的情况下拒绝零假设所要承担的风险水平,又叫概率水平,或者显著水平. [1] 显著性的含义是指两个群体的态度之 ...
- BUPT复试专题—最长连续等差子数列(2014软院)
题目描述 给定-个长度为N的整数数列,你需要在其中找到最长的连续子数列的长度, 并满足这个子数列是等差的.注意公差小于或等于0的情况也是允许的. 输入 第一行为数据组数T(1~100),表示测试数 ...
- push代码到github时,每次都要输入用户名和密码的问题
问题原由 我在Github上 建立了一个小项目TauStreamingServer,可是在每次push代码 的时候,都要求输入用户名和密码,很是麻烦. 如何才能避免每次都输入用户名和密码呢? 解决办法 ...
- 关于UDID和UUID的区别
关于UDID和UUID的区别 一.UDID(Unique Device Identifier) UDID是Unique Device Identifier的缩写,中文意思是设备唯一标识. 在很多需 ...
- linux进程间通信消息队列:msgsnd: Invalid argument
今天写了个消息队列的小测试程序结果send端程序总是出现:msgsnd: Invalid argument,搞了半个小时也没搞明白,后来查资料发现我将(st_msg_buf.msg_type = 0; ...