Kadj Squares
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 2937 Accepted: 1151
Description
In this problem, you are given a sequence S1, S2, …, 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 S1, S2, 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
题目大意:
给定一个数 n ,表示有 n 个正方形,然后n个整数,表示正方形的的边长,将正方形旋转45°,没个正方形都尽量靠左摆放,但是横坐标不能超过0,问的是从上往下看,能够看到几个正方形。
解题思路:
第一步:首先我们要做的事是将这n个正方形的左右端点的横坐标求出来,咋求呢,其实就是每次求下标
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
using namespace std;
const int MAXN = 55;
const double eps = 1e-8;
const double sq2 = sqrt(2.0);
struct Point
{
double left, right, len;
}p[MAXN];
int main()
{
int n;
while(cin>>n,n)
{
for(int i=1; i<=n; i++)
scanf("%lf",&p[i].len);
p[1].left = 0;
for(int i=1; i<=n; i++)
{
double l = 0;
for(int j=1; j<i; j++)
{
l = max(l, p[j].right-fabs(p[i].len-p[j].len)/sq2);
}
p[i].left = l;
p[i].right = p[i].left + p[i].len*sq2;
}
for(int i=1; i<=n; i++)
{
for(int j=1; j<i; j++)
{
if(p[i].len>p[j].len && p[i].left<p[j].right)
p[j].right = p[i].left;
if(p[i].len<p[j].len && p[i].left<p[j].right)
p[i].left = p[j].right;
}
}
for(int i=1; i<=n; i++)
{
if(p[i].right-p[i].left > eps)
printf("%d ",i);
}
puts("");
}
return 0;
}