CodeForces MemSQL start[c]up Round 1 A题
来源:互联网 发布:超市管理系统c语言 编辑:程序博客网 时间:2024/05/17 01:49
先看题目
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.
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).
In a single line print "YES", if the given rectangles form a square, or "NO" otherwise.
50 0 2 30 3 3 52 0 5 23 2 5 52 2 3 3
YES
40 0 2 30 3 3 52 0 5 23 2 5 5
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;}
测试数据:
50 0 2 30 3 3 52 0 5 23 2 5 52 2 3 3
YES
YES
ok single line: 'YES'
40 0 2 30 3 3 52 0 5 23 2 5 5
NO
NO
ok single line: 'NO'
50 0 10000 2000010000 0 15000 1999910000 19999 14999 200000 20000 15000 3140015000 0 31400 31400
NO
NO
ok single line: 'NO'
50 0 10000 2000010000 0 15000 1999910000 19999 15000 200000 20000 15000 3140015000 0 31400 31400
YES
YES
ok single line: 'YES'
510359 859 28918 43842895 26520 28918 268822895 26424 28918 265202895 859 10359 43842895 4384 28918 26424
YES
YES
ok single line: 'YES'
512750 0 25688 11094 0 12750 10 0 956 1956 0 1094 125688 0 31400 1
NO
NO
ok single line: 'NO'
418006 16484 25725 314000 0 31400 1648429563 16484 31400 3140025725 16484 29563 31400
NO
NO
ok single line: 'NO'
10 0 31400 31400
YES
YES
ok single line: 'YES'
20 0 31400 133130 13313 31400 31400
YES
YES
ok single line: 'YES'
30 9388 31400 3140026020 0 31400 93880 0 26020 9388
YES
YES
ok single line: 'YES'
- CodeForces MemSQL start[c]up Round 1 A题
- codeforces MemSQL Start[c]UP 2.0 - Round 1 A. Eevee
- Codeforces MemSQL start[c]up Round 1
- Codeforces Round MemSQL start[c]up Round 1
- Codeforces MemSQL Start[c]UP 2.0 - Round 1
- CodeForces-MemSQL start[c]up Round 1-A. Square and Rectangles
- Codeforces MemSQL start[c]up Round 1 /325A Square and Rectangles(模拟)
- MemSQL Start[c]UP 2.0 - Round 1 .A
- MemSQL Start[c]UP 2.0 - Round 1A(构造)
- MemSQL start[c]up Round 1
- MemSQL Start[c]UP 2.0 - Round 1
- MemSQL Start[c]UP 2.0 - Round 2 A
- CodeForces-MemSQL start[c]up Round 1-B. Stadium and Games
- codeforces MemSQL Start[c]UP 2.0 - Round 1 B. 4-point polyline
- MemSQL Start[c]UP 2.0 - Round 1 C. Magic Trick
- cf MemSQL start[c]up Round 1 A Square and Rectangles
- CF MemSQL Start[c]UP 2.0 - Round 1 D
- MemSQL Start[c]UP 2.0 Round 2
- 电感知识
- 【详解php中const与define的异同】
- ubuntu配置网络
- TextView设置不可见
- 常用电子元件基础知识(图解)
- CodeForces MemSQL start[c]up Round 1 A题
- erlang lists模块函数使用大全
- Professoion Trouble shooting for Full filesystem
- 电子元件基础知识
- MySQL数据库引擎ISAM,myISAM,heap
- 翻转句子中单词的顺序-运用vector
- 基于堆结构的TopN问题实现
- LED节能灯制作方法 以及电路图
- 【Cocos2d-X开发学习笔记】第08期:渲染框架之文本类的使用