CodeForces MemSQL start[c]up Round 1 A题

来源:互联网 发布:超市管理系统c语言 编辑:程序博客网 时间:2024/05/17 01:49

先看题目

A. Square and Rectangles
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given n rectangles. The corners of rectangles have integer coordinates and their edges are parallel to theOx andOy axes. The rectangles may touch each other, but they do not overlap (that is, there are no points that belong to the interior of more than one rectangle).

Your task is to determine if the rectangles form a square. In other words, determine if the set of points inside or on the border of at least one rectangle is precisely equal to the set of points inside or on the border of some square.

Input

The first line contains a single integer n (1 ≤ n ≤ 5). Nextn lines contain four integers each, describing a single rectangle:x1,y1,x2,y2 (0 ≤ x1 < x2 ≤ 31400, 0 ≤ y1 < y2 ≤ 31400) — x1 and x2 are x-coordinates of the left and right edges of the rectangle, andy1 andy2 are y-coordinates of the bottom and top edges of the rectangle.

No two rectangles overlap (that is, there are no points that belong to the interior of more than one rectangle).

Output

In a single line print "YES", if the given rectangles form a square, or "NO" otherwise.

Sample test(s)
Input
50 0 2 30 3 3 52 0 5 23 2 5 52 2 3 3
Output
YES
Input
40 0 2 30 3 3 52 0 5 23 2 5 5
Output
NO
大意就是:

给出一组数据,他们每行有两个对角顶点的坐标,构成一个横平竖直的矩形。这些矩形保证不会有重叠。问你这些矩形能否组成一个大的紧密的正方形。


跟我一起做题的一位同志非常厉害。。他根据题目的提示,就是ortherway后面那句话,借鉴了一下系统动态内存管理的知识,用链表做的。。。相当麻烦,而且时间复杂度是O(n2)。。。。。

其实这题根据有一个良好的性质,就是不重叠,横平竖直。这样必须保证大正方形面积==小矩形面积之和。当然,先抽取大矩形的两个顶点(就是最大坐标(XMax,YMax)和最小坐标(XMin,YMin)),根据它们判断能否轮廓上构成正方形。如果这一点前提成立了,后面面积相等不成立的话必然有内部填不满的情况。如果两者都成立,那么必然是能够成立的。


附上代码(我的代码其实有点小菜。。。不过很好懂)和测试数据(给10轮。。多了没劲。。)

#include <iostream>using namespace std;int main(){    int n;    cin>>n;    int x1[5],y1[5],x2[5],y2[5];    if (n==1)    {        cin>>x1[0]>>y1[0]>>x2[0]>>y2[0];        if (x2[0]-x1[0]==y2[0]-y1[0]||x2[0]-x1[0]==y1[0]-y2[0])        {            cout<<"YES"<<endl;        }        else{            cout<<"NO"<<endl;        }    }    else{        int xs,xl,ys,yl;        xs=31400;ys=31400;xl=0;yl=0;        int s;        int totals1,totals2;        totals1=0;        for (int i = 0; i < n; ++i)        {            cin>>x1[i]>>y1[i]>>x2[i]>>y2[i];            s=(x2[i]-x1[i])*(y2[i]-y1[i]);            if (s<0)            {                s=-s;            }            totals1+=s;            if (x1[i]<xs)            {                xs=x1[i];            }            if (x2[i]<xs)            {                xs=x2[i];            }            if (y1[i]<ys)            {                ys=y1[i];            }            if (y2[i]<ys)            {                ys=y2[i];            }            if (x1[i]>xl)            {                xl=x1[i];            }            if (x2[i]>xl)            {                xl=x2[i];            }            if (y1[i]>yl)            {                yl=y1[i];            }            if (y2[i]>yl)            {                yl=y2[i];            }        }        totals2=(xl-xs)*(xl-xs);        if (xl-xs==yl-ys)        {            if (totals2==totals1)            {                cout<<"YES"<<endl;            }            else{                cout<<"NO"<<endl;            }        }        else{            cout<<"NO"<<endl;        }    }    return 0;}

测试数据:

Test: #1, time: 0 ms., memory: 0 KB, exit code: 0, checker exit code: 0, verdict: OK
Checker Log
ok single line: 'YES'
Test: #2, time: 0 ms., memory: 0 KB, exit code: 0, checker exit code: 0, verdict: OK
Checker Log
ok single line: 'NO'
Test: #3, time: 15 ms., memory: 0 KB, exit code: 0, checker exit code: 0, verdict: OK
Checker Log
ok single line: 'NO'
Test: #4, time: 0 ms., memory: 0 KB, exit code: 0, checker exit code: 0, verdict: OK
Checker Log
ok single line: 'YES'
Test: #5, time: 0 ms., memory: 0 KB, exit code: 0, checker exit code: 0, verdict: OK
Checker Log
ok single line: 'YES'
Test: #6, time: 0 ms., memory: 0 KB, exit code: 0, checker exit code: 0, verdict: OK
Checker Log
ok single line: 'NO'
Test: #7, time: 0 ms., memory: 0 KB, exit code: 0, checker exit code: 0, verdict: OK
Checker Log
ok single line: 'NO'
Test: #8, time: 0 ms., memory: 0 KB, exit code: 0, checker exit code: 0, verdict: OK
Checker Log
ok single line: 'YES'
Test: #9, time: 0 ms., memory: 0 KB, exit code: 0, checker exit code: 0, verdict: OK
Checker Log
ok single line: 'YES'
Test: #10, time: 0 ms., memory: 0 KB, exit code: 0, checker exit code: 0, verdict: OK
Checker Log
ok single line: 'YES'

原创粉丝点击