J - Kadj Squares POJ - 3347

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-8
struct 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值