Codeforces Round #395(Div. 2)D. Timofey and rectangles【思维】好题!

来源:互联网 发布:thinking in java第5版 编辑:程序博客网 时间:2024/05/16 14:48

D. Timofey and rectangles
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

One of Timofey's birthday presents is a colourbook in a shape of an infinite plane. On the planen rectangles with sides parallel to coordinate axes are situated. All sides of the rectangles haveodd length. Rectangles cannot intersect, but they can touch each other.

Help Timofey to color his rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color, or determine that it is impossible.

Two rectangles intersect if their intersection has positive area. Two rectangles touch by sides if there is a pair of sides such that their intersection has non-zero length

The picture corresponds to the first example
Input

The first line contains single integer n (1 ≤ n ≤ 5·105) — the number of rectangles.

n lines follow. The i-th of these lines contains four integers x1, y1,x2 and y2 ( - 109 ≤ x1 < x2 ≤ 109, - 109 ≤ y1 < y2 ≤ 109), that means that points(x1, y1) and(x2, y2) are the coordinates of two opposite corners of thei-th rectangle.

It is guaranteed, that all sides of the rectangles have odd lengths and rectangles don't intersect each other.

Output

Print "NO" in the only line if it is impossible to color the rectangles in4 different colors in such a way that every two rectangles touching each other by side would have different color.

Otherwise, print "YES" in the first line. Then printn lines, in the i-th of them print single integerci (1 ≤ ci ≤ 4) — the color ofi-th rectangle.

Example
Input
80 0 5 32 -1 5 0-3 -4 2 -1-1 -1 2 0-3 0 0 55 2 10 37 -3 10 24 -2 7 -1
Output
YES12232241

题目大意:

一共有N个长、宽都是奇数长度的矩形,给出两个端点,表示这个矩形,让你给这些矩形进行涂色,使得相互接触的矩形的颜色不同。

保证没有相互重叠的部分,两个矩形只能相互接触,不能相互重叠。


思路(补题参考自:http://blog.csdn.net/qq_34731703/article/details/54845983):


1、首先四色定理大家是都知道了的,对于一个平面图来讲,肯定能够用最多四种颜色来给每个面进行涂色之后,使得相邻的两个面的颜色不相同。


2、平常接触的四色定理的题目一般点都会非常少,作为练习Dfs的好题,或者是再延展一些,多一些剪枝的操作之类。

但是这里点的数目最多可达5*1e5.那么我们暴力染色肯定是不行的了。那么观察问题的关键点:对于这N个矩形,其长和宽保证长度都是奇数。

那么考虑极限思维:

①我萌只考虑这个矩形的一个左下角的点,设定x坐标和y坐标都是奇数的矩形颜色为1.那么此时大家不妨在纸上画一画,颜色为1的这些矩形肯定不会相互接触到,因为这种矩形的左上角的y坐标以及右下角的x坐标肯定是偶数。那么肯定不会有矩形能够与之接触到。

②那么接下来考虑和颜色1上下接触的可能,对于左下角的点,如果其y坐标是偶数,且x坐标是奇数,那么其就可以作为上下接触的矩形出现,那么我萌设定颜色为2.

③再接下来考虑和颜色1左右接触的可能,对于左下角的点,如果x坐标是偶数,且y坐标是奇数,那么其就可以作为左右接触的矩形出现,那么我萌设定颜色为3.

④其余可能设定颜色为4即可。

既满足四色定理,又满足当前情况的染色。


Ac代码:

#include<stdio.h>#include<string.h>using namespace std;int x[500500];int y[500500];int main(){    int n;    while(~scanf("%d",&n))    {        for(int i=1; i<=n; i++)        {            int tmp1,tmp2;            scanf("%d%d%d%d",&x[i],&y[i],&tmp1,&tmp2);        }        printf("YES\n");        for(int i=1; i<=n; i++)        {            if(x[i]<0)x[i]=-x[i];            if(y[i]<0)y[i]=-y[i];            if(x[i]%2==1&&y[i]%2==1)printf("1\n");            else if(x[i]%2==1&&y[i]%2==0)printf("2\n");            else if(x[i]%2==0&&y[i]%2==1)printf("3\n");            else printf("4\n");        }    }}








0 0
原创粉丝点击