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=1000010;
int a[maxn],q[maxn],head,tail;
int main()
{
    int T,N,i,j,ans,Case=0;
    scanf("%d",&T);
    while(T--){
        head=tail=ans=0;
        scanf("%d",&N);
        for(i=1;i<=N;i++) scanf("%d",&a[i]);
        sort(a+1,a+N+1);
        for(i=1;i<=N;i++){
            if(head==tail) ans++;
            else if(q[tail+1]<=a[i]) tail++;
            else ans++;
            q[++head]=a[i]*2;
        }
        printf("Case %d: %d\n",++Case,ans);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/hua-dong/p/8971727.html

时间: 2024-08-29 21:45:36

SPOJ:Decreasing Number of Visible Box(不错的,背包?贪心?)的相关文章

452. Minimum Number of Arrows to Burst Balloons——排序+贪心算法

There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided input is the start and end coordinates of the horizontal diameter. Since it's horizontal, y-coordinates don't matter and hence the x-coordinates of s

数论 - 欧拉函数的运用 --- poj 3090 : Visible Lattice Points

Visible Lattice Points Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5636   Accepted: 3317 Description A lattice point (x, y) in the first quadrant (x and y are integers greater than or equal to 0), other than the origin, is visible fr

POJ 3090 Visible Lattice Points

Description A lattice point (x, y) in the first quadrant (x and y are integers greater than or equal to 0), other than the origin, is visible from the origin if the line from (0, 0) to (x, y) does not pass through any other lattice point. For example

POJ 3090 Visible Lattice Points 布雷级数

Visible Lattice Points Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5779   Accepted: 3409 Description A lattice point (x, y) in the first quadrant (x and y are integers greater than or equal to 0), other than the origin, is visible fr

poj3090 Visible Lattice Points [欧拉函数]

Description A lattice point (x, y) in the first quadrant (x and y are integers greater than or equal to 0), other than the origin, is visible from the origin if the line from (0, 0) to (x, y) does not pass through any other lattice point. For example

POJ 3090 ZOJ 2777 UVALive 3571 Visible Lattice Points(用递推比用欧拉函数更好)

题目: Description A lattice point (x, y) in the first quadrant (x and y are integers greater than or equal to 0), other than the origin, is visible from the origin if the line from (0, 0) to (x, y) does not pass through any other lattice point. For exa

pku3090 Visible Lattice Points:人生第一个欧拉函数

这是一道很有纪念价值的题目! Visible Lattice Points Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5812   Accepted: 3434 Description A lattice point (x, y) in the first quadrant (x and y are integers greater than or equal to 0), other than the origin

【POJ】3090 Visible Lattice Points

Visible Lattice Points Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7705   Accepted: 4707 Description A lattice point (x, y) in the first quadrant (x and y are integers greater than or equal to 0), other than the origin, is visible fr

poj 3060 Visible Lattice Points

http://poj.org/problem?id=3090 Visible Lattice Points Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6153   Accepted: 3662 Description A lattice point (x, y) in the first quadrant (x and y are integers greater than or equal to 0), other