【CodeForces - 764D】Timofey and rectangles (四色定理 + 找规律 + 构造)
題干:
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?4different 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 -1Output
YES 1 2 2 3 2 2 4 1題目大意:
? ??一個無限大的平面被分為無數個小正方形格子,一些相連的格子們可以組成矩形。給出左下角和右上角的坐標來表示該矩形(比如給出 0 0 5 3 即代表坐標為(0,0)開始到(5,3)之間的所有格子組成的矩形)。矩形的長和寬只能是奇數,給出一些矩形的坐標表示,要求寫程序為所有的矩形染色,相鄰的矩形不可染同種顏色(題中已經給出所有的矩形沒有兩兩相交的情況出現,且相鄰的條件為存在大于0長度的公共邊),如果存在染色方案則輸出YES并且輸出N個矩形的每種染色情況(如果存在多種情況輸出任意一種即可),如果不存在染色方案則輸出NO結束。
解題報告:
思路描述: 由于我們已知任意一個地圖都4可著色,因此第一行一定永遠是"YES"。關于著色方案,我們重點關注所有的邊均為奇數的條件。
由于所有的邊均為奇數,那么對于兩個相鄰的矩形,我們考慮其左下角的坐標:設其為A (x1,y1)和B (x2,y2);
當A與B水平相鄰時,即如下圖所示(黃色為A,藍綠色為B,其中(x1,y1)以及(x2,y2)分別表示紅色的點和藍色的點):
我們可以看出對于任意兩個相鄰的矩形,其必有(x1與x2)或(y1與y2)的奇偶性不一致(因為所有矩形邊長均為奇數),由于我們有4種染色方案(設為1 2 3 4),因此我們可以構造其染色情況為
[(x%2+2*(y%2))+4]%4+1(其中x和y表示該矩形左下角坐標),即可保證相鄰的矩形中沒有同色的情況(因為該染色情況下同色當且僅當x與y的奇偶性一致)。
AC代碼:
#include<iostream> #include<cmath> using namespace std; int main() {int n, a, b, c, d;ios::sync_with_stdio(false);cin >> n;cout <<"YES"<<endl;while(n--) {cin>>a>>b>>c>>d;int tmp = abs(a) % 2 + 2 * (abs(b) % 2);cout << tmp + 1 << endl;}return 0; }總結:
[(x%2+2*(y%2))+4]%4+1這個公式也可以,這兩種構造的方式雖然結果不同但是都符合題意。注意負數取模問題!
總結
以上是生活随笔為你收集整理的【CodeForces - 764D】Timofey and rectangles (四色定理 + 找规律 + 构造)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 浦发信用卡查询进度方法 四种方法简单快捷
- 下一篇: sdstat.exe - sdstat是