poj 3347 Kadj Squares(线段切割)

来源:互联网 发布:最大公约数怎么求算法 编辑:程序博客网 时间:2024/05/01 05:09
Kadj Squares
Time Limit: 2000MS Memory Limit: 65536KTotal 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

43 5 1 432 1 20

Sample Output

1 2 41 3

Source

Tehran 2006
题目:http://poj.org/problem?id=3347
题意:一堆正方形,按给定顺序尽量靠左摆放,问最后从上面看有哪几个被看到
分析:首先求出每个正方形的坐标,然后大一点的正方形一定在小一点的正方形上面,把每个正方形转换成一条线段,按正方形的由小到大排序,然后做线段切割即可
代码:
#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;}


原创粉丝点击