Kadj Squares - POJ 3347 几何
2015-07-29 23:10
363 查看
Kadj Squares
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
Sample Output
题意:按照如图所示的方法放这些矩形,问有哪些矩形其正上面没有被完全覆盖。
思路:我们将矩形的边长乘上根号2,保证其端点都在整数坐标上。首先找到所有矩形最下方的那个点的位置。因为n比较小,一点点向右移动枚举其是否与前面的矩形有重叠即可。然后得到每个矩形的位置后,也可以得到其l-r的范围,按照s的大小排序,然后遍历找到这个矩形l-r是否已经完全被覆盖。
AC代码如下:
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 2801 | Accepted: 1090 |
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,保证其端点都在整数坐标上。首先找到所有矩形最下方的那个点的位置。因为n比较小,一点点向右移动枚举其是否与前面的矩形有重叠即可。然后得到每个矩形的位置后,也可以得到其l-r的范围,按照s的大小排序,然后遍历找到这个矩形l-r是否已经完全被覆盖。
AC代码如下:
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; struct Point { int x,y; Point(int x=0,int y=0):x(x),y(y){} }; struct node { int id,l,r,h; }line[55]; bool cmp(node a,node b) { return a.h>b.h; } Point p[55][4]; int n,m,s[55],pos[55]; bool vis[100010]; int ans[55]; bool check(int i,int x) { int j,k; for(j=1;j<i;j++) if(x-pos[j]<min(s[j],s[i])*2) return 0; return 1; } int main() { int i,j,k; bool flag; while(~scanf("%d",&n) && n>0) { m=0; for(i=1;i<=n;i++) { scanf("%d",&s[i]); while(true) { m=max(m,s[i]); if(check(i,m)) { pos[i]=m; m++; break; } m++; } } for(i=1;i<=n;i++) { line[i].l=pos[i]-s[i]+1; line[i].r=pos[i]+s[i]; line[i].h=s[i]; line[i].id=i; } sort(line+1,line+1+n,cmp); ans[0]=0; memset(vis,0,sizeof(vis)); for(i=1;i<=n;i++) { flag=0; for(j=line[i].l;j<=line[i].r;j++) { if(!vis[j]) flag=1; vis[j]=1; } if(flag) ans[++ans[0]]=line[i].id; } sort(ans+1,ans+1+ans[0]); printf("%d",ans[1]); for(i=2;i<=ans[0];i++) printf(" %d",ans[i]); printf("\n"); } }
相关文章推荐
- 黑马程序员------oc之面向对象特性 封装、多态、继承等
- Java内存管理原理及内存区域详解
- 计算机基础
- coding.net
- 7月29日所学
- springmvc 和mybatis 整合(一)
- dropwizard metrics - 基本使用介绍
- CSS基础-7CSS样式-文本
- 编程语言的学习生涯 连载一北京校区53班赵凯
- 图形系统总结
- Android 平台上的 ARP 和 ARP欺骗
- 初学java,写了一个mysql类(增删改查)
- 趣味问题:画图(c++实现)
- mtk android 4.4 audio framework 代码分析(未完成)
- Java使用Dom解析xml学习记录
- 《机器学习实战》(二)k-近邻算法(KNN)
- EularProject 36:2进制和10进制回文数
- 黑马程序员——System类常用方法及流对象操作规律
- Android Audio代码分析 - Audio Strategy
- 【Leetcode】Rotate List