Codeforces Round #395 (Div. 2)D. Timofey and rectangles
2017-02-03 14:42
417 查看
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 plane n rectangles with sides parallel to
coordinate axes are situated. All sides of the rectangles have odd 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 the i-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 in 4 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 print n lines,
in the i-th of them print single integer ci (1 ≤ ci ≤ 4) —
the color of i-th rectangle.
Example
input
output
题意:给出一些边相接触的矩形,用四种颜色里面的一种给矩形上色,问是否存在一种方案让所有边接触的矩形颜色
都不同。有的话打印出来每个矩形的颜色,没有的话打印NO。
思路:题意中有个很奇怪的条件,边长为奇数。今天想了一上午才想明白。
对于一个坐标点(x, y) 如果x是奇数,那么加奇数长度的值就是偶数,如果是偶数加奇数长度值就是奇数。
用左下角点表示一个矩形位置,对于某个点只有四种状态 (奇数,奇数)(偶数,偶数)(奇数,偶数)(偶数,奇数)之一,
而边接触的矩形位置则是其它3种之一,而颜色只有4种,所以一种状态给一种颜色,什么情况都会有解。
printf("%d\n", (2*(x%2) + y%2)+1);
官方题解给的是上面的这种表达式,其实就是先把4种状态用00 01 10 11编码,x是高位,y是低位,2×x就是让x的值
存在高位。这样得到0 1 2 3,颜色是1 2 3 4所以要加1。
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 plane n rectangles with sides parallel to
coordinate axes are situated. All sides of the rectangles have odd 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 the i-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 in 4 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 print n lines,
in the i-th of them print single integer ci (1 ≤ ci ≤ 4) —
the color of i-th rectangle.
Example
input
8 0 0 5 3 2 -1 5 0 -3 -4 2 -1 -1 -1 2 0 -3 0 0 5 5 2 10 3 7 -3 10 2 4 -2 7 -1
output
YES 1 2 2 3 2 2 4 1
题意:给出一些边相接触的矩形,用四种颜色里面的一种给矩形上色,问是否存在一种方案让所有边接触的矩形颜色
都不同。有的话打印出来每个矩形的颜色,没有的话打印NO。
思路:题意中有个很奇怪的条件,边长为奇数。今天想了一上午才想明白。
对于一个坐标点(x, y) 如果x是奇数,那么加奇数长度的值就是偶数,如果是偶数加奇数长度值就是奇数。
用左下角点表示一个矩形位置,对于某个点只有四种状态 (奇数,奇数)(偶数,偶数)(奇数,偶数)(偶数,奇数)之一,
而边接触的矩形位置则是其它3种之一,而颜色只有4种,所以一种状态给一种颜色,什么情况都会有解。
printf("%d\n", (2*(x%2) + y%2)+1);
官方题解给的是上面的这种表达式,其实就是先把4种状态用00 01 10 11编码,x是高位,y是低位,2×x就是让x的值
存在高位。这样得到0 1 2 3,颜色是1 2 3 4所以要加1。
相关文章推荐
- Codeforces Round #395 (Div. 2) D. Timofey and rectangles
- 【分类讨论】Codeforces Round #395 (Div. 2) D. Timofey and rectangles
- Codeforces Round #395(Div. 2)D. Timofey and rectangles
- Codeforces Round #395 (Div. 2) D. Timofey and rectangles
- Codeforces Round #395 (Div. 2) D. Timofey and rectangles_0
- Codeforces Round #395 (Div. 2)-D. Timofey and rectangles
- Codeforces Round #395 (Div. 2) D Timofey and rectangles(思维题)
- Codeforces Round #395 (Div. 2) D. Timofey and rectangles(思路)
- Codeforces Round #395(Div. 2)D. Timofey and rectangles【思维】好题!
- Codeforces Round #395 (Div. 2) D. Timofey and rectangles
- Codeforces Round #395 (Div. 2) D. Timofey and rectangles
- Codeforces Round #395 (Div. 2) B. Timofey and cubes(水题)
- Codeforces Round #395 (Div. 2)Timofey and a tree
- Timofey and rectangles
- Codeforces Round #395 (Div. 2)E: Timofey and remoduling(数学+数论)
- Codeforces Round #395 (Div. 2) C. Timofey and a tree(树的基础应用)
- Codeforces 763B-Timofey and rectangles
- Codeforces Round #395 (Div. 2) C. Timofey and a tree(树的基础应用)
- Codeforces Round #395 (Div. 1) E. Timofey and our friends animals(lct动态维护mst)
- Codeforces Round #395(Div. 2)B. Timofey and cubes【模拟】