Codeforces Beta Round #97 (Div. 2) (判断正方形和矩形)

来源:互联网 发布:51单片机c语言编程入门 编辑:程序博客网 时间:2024/05/22 20:14
D. Rectangle and Square
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Little Petya very much likes rectangles and especially squares. Recently he has received 8 points on the plane as a gift from his mother. The points are pairwise distinct. Petya decided to split them into two sets each containing 4 points so that the points from the first set lay at the vertexes of some square and the points from the second set lay at the vertexes of a rectangle. Each point of initial 8 should belong to exactly one set. It is acceptable for a rectangle from the second set was also a square. If there are several partitions, Petya will be satisfied by any of them. Help him find such partition. Note that the rectangle and the square from the partition should have non-zero areas. The sides of the figuresdo not have to be parallel to the coordinate axes, though it might be the case.

<p< p=""><p< p="">
Input
<p< p="">

You are given 8 pairs of integers, a pair per line — the coordinates of the points Petya has. The absolute value of all coordinates does not exceed 104. It is guaranteed that no two points coincide.

<p< p=""><p< p="">
Output
<p< p="">

Print in the first output line "YES" (without the quotes), if the desired partition exists. In the second line output 4 space-separated numbers — point indexes from the input, which lie at the vertexes of the square. The points are numbered starting from 1. The numbers can be printed in any order. In the third line print the indexes of points lying at the vertexes of a rectangle in the similar format. All printed numbers should be pairwise distinct.

If the required partition does not exist, the first line should contain the word "NO" (without the quotes), after which no output is needed.

<p< p=""><p< p="">
Sample test(s)
<p< p=""><p< p="">
input
0 010 1110 00 111 12 22 11 2
output
YES5 6 7 81 2 3 4
input
0 01 12 23 34 45 56 67 7
output
NO
input
0 04 44 00 41 22 33 22 1
output
YES1 2 3 45 6 7 8
<p< p=""><p< p="">
Note
<p< p="">

Pay attention to the third example: the figures do not necessarily have to be parallel to the coordinate axes.

include<cstdio>#include<cmath>#include<algorithm>using std::sort;#define eps 1e-8#define zero(x) (((x)>0?(x):-(x))<eps)struct point{double x,y; int id;};struct line{point a,b;};//计算cross product (P1-P0)x(P2-P0)double xmult(point p1,point p2,point p0){    return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);}double xmult(double x1,double y1,double x2,double y2,double x0,double y0){    return (x1-x0)*(y2-y0)-(x2-x0)*(y1-y0);}//计算dot product (P1-P0).(P2-P0)double dmult(point p1,point p2,point p0){    return (p1.x-p0.x)*(p2.x-p0.x)+(p1.y-p0.y)*(p2.y-p0.y);}double dmult(double x1,double y1,double x2,double y2,double x0,double y0){    return (x1-x0)*(x2-x0)+(y1-y0)*(y2-y0);}//两点距离double distance(point p1,point p2){    return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));}double distance(double x1,double y1,double x2,double y2){    return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));}//判三点共线int dots_inline(point p1,point p2,point p3){    return zero(xmult(p1,p2,p3));}int dots_inline(double x1,double y1,double x2,double y2,double x3,double y3){    return zero(xmult(x1,y1,x2,y2,x3,y3));}//判点是否在线段上,包括端点int dot_online_in(point p,line l){    return zero(xmult(p,l.a,l.b))&&(l.a.x-p.x)*(l.b.x-p.x)<eps&&(l.a.y-p.y)*(l.b.y-p.y)<eps;}int dot_online_in(point p,point l1,point l2){    return zero(xmult(p,l1,l2))&&(l1.x-p.x)*(l2.x-p.x)<eps&&(l1.y-p.y)*(l2.y-p.y)<eps;}int dot_online_in(double x,double y,double x1,double y1,double x2,double y2){    return zero(xmult(x,y,x1,y1,x2,y2))&&(x1-x)*(x2-x)<eps&&(y1-y)*(y2-y)<eps;}//判点是否在线段上,不包括端点int dot_online_ex(point p,line l){    return dot_online_in(p,l)&&(!zero(p.x-l.a.x)||!zero(p.y-l.a.y))&&(!zero(p.x-l.b.x)||!zero(p.y-l.b.y));}int dot_online_ex(point p,point l1,point l2){    return dot_online_in(p,l1,l2)&&(!zero(p.x-l1.x)||!zero(p.y-l1.y))&&(!zero(p.x-l2.x)||!zero(p.y-l2.y));}int dot_online_ex(double x,double y,double x1,double y1,double x2,double y2){    return dot_online_in(x,y,x1,y1,x2,y2)&&(!zero(x-x1)||!zero(y-y1))&&(!zero(x-x2)||!zero(y-y2));}//判两点在线段同侧,点在线段上返回0int same_side(point p1,point p2,line l){    return xmult(l.a,p1,l.b)*xmult(l.a,p2,l.b)>eps;}int same_side(point p1,point p2,point l1,point l2){    return xmult(l1,p1,l2)*xmult(l1,p2,l2)>eps;}//判两点在线段异侧,点在线段上返回0int opposite_side(point p1,point p2,line l){    return xmult(l.a,p1,l.b)*xmult(l.a,p2,l.b)<-eps;}int opposite_side(point p1,point p2,point l1,point l2){    return xmult(l1,p1,l2)*xmult(l1,p2,l2)<-eps;}//判两直线平行int parallel(line u,line v){    return zero((u.a.x-u.b.x)*(v.a.y-v.b.y)-(v.a.x-v.b.x)*(u.a.y-u.b.y));}int parallel(point u1,point u2, point v1,point v2){    return zero((u1.x-u2.x)*(v1.y-v2.y)-(v1.x-v2.x)*(u1.y-u2.y));}//判两直线垂直int perpendicular(line u,line v){    return zero((u.a.x-u.b.x)*(v.a.x-v.b.x)+(u.a.y-u.b.y)*(v.a.y-v.b.y));}int perpendicular(point u1,point u2,point v1,point v2){    return zero((u1.x-u2.x)*(v1.x-v2.x)+(u1.y-u2.y)*(v1.y-v2.y));}//判两线段相交,包括端点和部分重合int intersect_in(line u,line v){    if (!dots_inline(u.a,u.b,v.a)||!dots_inline(u.a,u.b,v.b))        return !same_side(u.a,u.b,v)&&!same_side(v.a,v.b,u);    return dot_online_in(u.a,v)||dot_online_in(u.b,v)||dot_online_in(v.a,u)||dot_online_in(v.b,u);}int intersect_in(point u1,point u2,point v1,point v2){    if (!dots_inline(u1,u2,v1)||!dots_inline(u1,u2,v2))        return !same_side(u1,u2,v1,v2)&&!same_side(v1,v2,u1,u2);    return dot_online_in(u1,v1,v2)||dot_online_in(u2,v1,v2)||dot_online_in(v1,u1,u2)||dot_online_in(v2,u1,u2);}//判两线段相交,不包括端点和部分重合int intersect_ex(line u,line v){    return opposite_side(u.a,u.b,v)&&opposite_side(v.a,v.b,u);}int intersect_ex(point u1,point u2,point v1,point v2){    return opposite_side(u1,u2,v1,v2)&&opposite_side(v1,v2,u1,u2);}//计算两直线交点,注意事先判断直线是否平行!//线段交点请另外判线段相交(同时还是要判断是否平行!)point intersection(line u,line v){    point ret=u.a;    double t=((u.a.x-v.a.x)*(v.a.y-v.b.y)-(u.a.y-v.a.y)*(v.a.x-v.b.x))            /((u.a.x-u.b.x)*(v.a.y-v.b.y)-(u.a.y-u.b.y)*(v.a.x-v.b.x));    ret.x+=(u.b.x-u.a.x)*t;    ret.y+=(u.b.y-u.a.y)*t;    return ret;}point intersection(point u1,point u2,point v1,point v2){    point ret=u1;    double t=((u1.x-v1.x)*(v1.y-v2.y)-(u1.y-v1.y)*(v1.x-v2.x))            /((u1.x-u2.x)*(v1.y-v2.y)-(u1.y-u2.y)*(v1.x-v2.x));    ret.x+=(u2.x-u1.x)*t;    ret.y+=(u2.y-u1.y)*t;    return ret;}//点到直线上的最近点point ptoline(point p,line l){    point t=p;    t.x+=l.a.y-l.b.y,t.y+=l.b.x-l.a.x;    return intersection(p,t,l.a,l.b);}point ptoline(point p,point l1,point l2){    point t=p;    t.x+=l1.y-l2.y,t.y+=l2.x-l1.x;    return intersection(p,t,l1,l2);}//点到直线距离double disptoline(point p,line l){    return fabs(xmult(p,l.a,l.b))/distance(l.a,l.b);}double disptoline(point p,point l1,point l2){    return fabs(xmult(p,l1,l2))/distance(l1,l2);}double disptoline(double x,double y,double x1,double y1,double x2,double y2){    return fabs(xmult(x,y,x1,y1,x2,y2))/distance(x1,y1,x2,y2);}//点到线段上的最近点point ptoseg(point p,line l){    point t=p;    t.x+=l.a.y-l.b.y,t.y+=l.b.x-l.a.x;    if (xmult(l.a,t,p)*xmult(l.b,t,p)>eps)        return distance(p,l.a)<distance(p,l.b)?l.a:l.b;    return intersection(p,t,l.a,l.b);}point ptoseg(point p,point l1,point l2){    point t=p;    t.x+=l1.y-l2.y,t.y+=l2.x-l1.x;    if (xmult(l1,t,p)*xmult(l2,t,p)>eps)        return distance(p,l1)<distance(p,l2)?l1:l2;    return intersection(p,t,l1,l2);}//点到线段距离double disptoseg(point p,line l){    point t=p;    t.x+=l.a.y-l.b.y,t.y+=l.b.x-l.a.x;    if (xmult(l.a,t,p)*xmult(l.b,t,p)>eps)        return distance(p,l.a)<distance(p,l.b)?distance(p,l.a):distance(p,l.b);    return fabs(xmult(p,l.a,l.b))/distance(l.a,l.b);}double disptoseg(point p,point l1,point l2){    point t=p;    t.x+=l1.y-l2.y,t.y+=l2.x-l1.x;    if (xmult(l1,t,p)*xmult(l2,t,p)>eps)        return distance(p,l1)<distance(p,l2)?distance(p,l1):distance(p,l2);    return fabs(xmult(p,l1,l2))/distance(l1,l2);}//矢量V以P为顶点逆时针旋转angle并放大scale倍point rotate(point v,point p,double angle,double scale){    point ret=p;    v.x-=p.x,v.y-=p.y;    p.x=scale*cos(angle);    p.y=scale*sin(angle);    ret.x+=v.x*p.x-v.y*p.y;    ret.y+=v.x*p.y+v.y*p.x;    return ret;}point p[10];int set1[10], set2[10];bool check ( point p1, point p2, point p3, point p4 ){    double dis1 = distance(p1,p2);    double dis2 = distance(p2,p4); if ( dis1 != dis2 ) return false;    double dis3 = distance(p4,p3); if ( dis2 != dis3 ) return false;    double dis4 = distance(p3,p1); if ( dis3 != dis4 ) return false;    if ( !zero(distance(p1,p4)-distance(p2,p3)) ) return false;    if ( ! perpendicular ( p1, p4, p2, p3 ) ) return false;    p1 = p[set2[1]]; p2 = p[set2[2]]; p3 = p[set2[3]]; p4 = p[set2[4]];    dis1 = distance(p1,p2);    dis2 = distance(p2,p4);    dis3 = distance(p4,p3);    dis4 = distance(p3,p1);    if ( dis1 != dis3 || dis2 != dis4 ) return false;    if ( !zero(distance(p1,p4)-distance(p2,p3)) ) return false;    return true;}bool dfs ( int num, int index ){    if ( num > 4 )    {        bool mark[10] = {0};        mark[set1[1]] = mark[set1[2]] = mark[set1[3]] = mark[set1[4]] = true;        for ( int i = 1, j = 0; i <= 8; i++ ) if ( !mark[i] ) set2[++j] = i;        if ( check(p[set1[1]],p[set1[2]],p[set1[3]],p[set1[4]] )) return true;        return false;    }    if ( index > 8 ) return false;    set1[num] = index;    if ( dfs(num+1, index+1) ) return true;    if ( dfs(num, index+1) ) return true;    return false;}bool cmp ( const point &a, const point &b ){    if ( a.x == b.x )        return a.y < b.y;    return a.x < b.x;}int main(){    for ( int i = 1; i <= 8; i++ )    {        p[i].id = i;        scanf("%lf %lf",&p[i].x, &p[i].y);    }    sort(p+1,p+9,cmp);    if ( dfs(1,1) )    {        printf("YES\n");        printf("%d %d %d %d\n",p[set1[1]].id,p[set1[2]].id,p[set1[3]].id,p[set1[4]].id);        printf("%d %d %d %d\n",p[set2[1]].id,p[set2[2]].id,p[set2[3]].id,p[set2[4]].id);        putchar('\n');    }    else printf("NO\n");    return 0;}