poj 3347 Kadj Squares(线段切割)

Kadj Squares
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 1911 Accepted: 752

Description

In this problem, you are given a sequence S1S2, ..., Sn of squares of different sizes. The sides of the squares are integer numbers. We locate the squares on the positive x-y quarter of the plane, such that their sides make 45 degrees with x and y axes, and one of their vertices are on y=0 line. Let bi be the x coordinates of the bottom vertex of Si. First, put S1 such that its left vertex lies on x=0. Then, put S1, (i > 1) at minimum bi such that

  • bi > bi-1 and
  • the interior of Si does not have intersection with the interior of S1...Si-1.

The goal is to find which squares are visible, either entirely or partially, when viewed from above. In the example above, the squares S1S2, and S4 have this property. More formally, Si is visible from above if it contains a point p, such that no square other than Si intersect the vertical half-line drawn from p upwards.

Input

The input consists of multiple test cases. The first line of each test case is n (1 ≤ n ≤ 50), the number of squares. The second line contains n integers between 1 to 30, where the ith number is the length of the sides of Si. The input is terminated by a line containing a zero number.

Output

For each test case, output a single line containing the index of the visible squares in the input sequence, in ascending order, separated by blank characters.

Sample Input

4
3 5 1 4
3
2 1 2
0

Sample Output

1 2 4
1 3

Source

题意:一堆正方形,按给定顺序尽量靠左摆放,问最后从上面看有哪几个被看到
分析:首先求出每个正方形的坐标,然后大一点的正方形一定在小一点的正方形上面,把每个正方形转换成一条线段,按正方形的由小到大排序,然后做线段切割即可
代码:
#include<cmath>
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
const int mm=55;
struct seg
{
    int id,l,r;
}g[mm];
int a[mm],b[mm];
int i,j,n;
bool cmp(seg u,seg v)
{
    return a[u.id]<a[v.id];
}
bool ok(int i,int l,int r)
{
    if(i>=n)return 1;
    if(r<=g[i].l||l>=g[i].r)
    {
        if(ok(i+1,l,r))return 1;
    }
    else if(l<g[i].l)
    {
        if(ok(i+1,l,g[i].l))return 1;
    }
    else if(r>g[i].r)
    {
        if(ok(i+1,g[i].r,r))return 1;
    }
    return 0;
}
int main()
{
    while(scanf("%d",&n),n)
    {
        for(i=0;i<n;++i)
        {
            scanf("%d",&a[i]);
            b[i]=a[i]*2;
            for(j=0;j<i;++j)
                if(a[i]>=a[j])b[i]=max(b[i],b[j]+a[j]*4);
                else b[i]=max(b[i],b[j]+a[i]*4);
            g[i].id=i;
            g[i].l=b[i]-a[i]*2;
            g[i].r=b[i]+a[i]*2;
        }
        sort(g,g+n,cmp);
        for(j=i=0;i<n;++i)
            if(ok(i+1,g[i].l,g[i].r))
                a[j++]=g[i].id+1;
        sort(a,a+j);
        for(i=0;i<j;++i)
            printf("%d%c",a[i],i<j-1?' ':'\n');
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值