J

来源:互联网 发布:js获取单选框选中属性 编辑:程序博客网 时间:2024/06/02 16:05

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
样例一
这个题目由于从边求正方形左端点的坐标和右端点的坐标,所以会有精度问题,所以直接将整体输入的边长扩大根号2倍,就可以避免精度问题了。
题目思路分三步:
1、对当前第i个正方形来说,让其与之前i-1个正方形做比较,如果i个正方形紧挨着j个正方形,那么它的左端点的横坐标就等于 = 第j个正方形的右端点横坐标-|边[i]-边[j]|;
2、然后再对第i个正方形进行处理,求其左右两端能看到点的坐标。
3、对左端点坐标大于等于右端点坐标的正方形可以去掉。

#include<iostream>#include<fstream>#include<iomanip>#include<cstdio>#include<cstring>#include<algorithm>#include<cstdlib>#include<cmath>#include<set>#include<map>#include<queue>#include<stack>#include<string>#include<vector>#include<sstream>#include<ctime>#include<cassert>using namespace std;#define EORR 1e-8struct sq{  int l, r;};int n;int a[55];sq q[55];bool used[55];int main(){  while(scanf("%d", &n) && n) {    for (int i = 1; i <= n; i++)      scanf("%d", &a[i]);    for (int i = 1; i <= n; i++) {      q[i].l = 0;      used[i] = 1;      for (int j = 1; j < i; j++)        q[i].l = max(q[i].l, q[j].r-abs(a[i]-a[j]));      q[i].r = q[i].l + 2*a[i];      for (int j = 1; j < i; j++) {        if(a[i] > a[j] && q[j].r > q[i].l)//          q[j].r = q[i].l;        if(a[i] < a[j] && q[j].r > q[i].l)//          q[i].l = q[j].r;      }    }    int flag = 1;    for (int i = 1; i <= n; i++) {      if(q[i].l < q[i].r) {        if(!flag) printf(" ");        else flag = 0;        printf("%d", i);      }    }    printf("\n");  }  return 0;}
0 0
原创粉丝点击