Codeforces Round #371 (Div. 2) D. Searching Rectangles 平面矩形二分法、交互题
来源:互联网 发布:淘宝上投诉卖家有用吗 编辑:程序博客网 时间:2024/05/11 19:07
Filya just learned new geometry object — rectangle. He is given a field consisting of n × n unit cells. Rows are numbered from bottom to top with integer from 1 to n. Columns are numbered from left to right with integers from 1 to n. Cell, located at the intersection of the row r and column c is denoted as (r, c). Filya has painted two rectangles, such that their sides are parallel to coordinate axes and each cell lies fully inside or fully outside each of them. Moreover, no cell lies in both rectangles.
Later, hedgehog Filya became interested in the location of his rectangles but was unable to find the sheet of paper they were painted on. They were taken by Sonya and now she wants to play a little game with Filya. He tells her a query rectangle and she replies with the number of initial rectangles that lie fully inside the given query rectangle. The query rectangle should match the same conditions as initial rectangles. Rectangle lies fully inside the query if each o its cells lies inside the query.
Filya knows Sonya really well, so is sure that if he asks more than 200 questions she will stop to reply.
The first line of the input contains an integer n (2 ≤ n ≤ 216) — size of the field.
For each query an integer between 0 and 2 is returned — the number of initial rectangles that lie fully inside the query rectangle.
To make a query you have to print "? x1 y1 x2 y2" (without quotes) (1 ≤ x1 ≤ x2 ≤ n, 1 ≤ y1 ≤ y2 ≤ n), where (x1, y1) stands for the position of the bottom left cell of the query and (x2, y2) stands for the up right cell of the query. You are allowed to ask no more than 200 queries. After each query you should perform "flush" operation and read the answer.
In case you suppose you've already determined the location of two rectangles (or run out of queries) you should print "! x11 y11 x12 y12 x21 y21x22 y22" (without quotes), where first four integers describe the bottom left and up right cells of the first rectangle, and following four describe the corresponding cells of the second rectangle. You can print the rectangles in an arbitrary order. After you have printed the answer, print the end of the line and perform "flush". Your program should terminate immediately after it print the answer.
To flush you can use (just after printing an integer and end-of-line):
- fflush(stdout) in C++;
- System.out.flush() in Java;
- stdout.flush() in Python;
- flush(output) in Pascal;
- See the documentation for other languages.
You will get the Wrong Answer verdict if you ask more than 200 queries, or if you print an incorrect coordinates.
You will get the Idleness Limit Exceeded verdict if you don't print anything (but you should) or if you forget about flushing the output (more info below).
Hacking.
The first line should contain an integer n (2 ≤ n ≤ 216).
The second line should contain four integers x1, y1, x2, y2 (1 ≤ x1 ≤ x2 ≤ n, 1 ≤ y1 ≤ y2 ≤ n) — the description of the first rectangle.
The third line contains the description of the second rectangle in the similar way.
521011101
? 1 1 5 5? 1 1 3 3? 1 1 3 1? 2 2 2 2? 3 3 5 5? 3 3 3 5? 3 3 3 4? 3 4 3 5! 2 2 2 2 3 4 3 5
Source
Codeforces Round #371 (Div. 2)
My Solution
平面矩形二分法、交互题
先切一条与x轴平行的线把 2个矩形分隔开, 然后变成从 框框类确定一个矩形的坐标这样的子问题,
对于那条与x轴平行且把2个矩形分隔开的线可以通过 二分法 logn的复杂度找到,
如果没有与x轴平行且把2个矩形分隔开的线, 则 比有一条 与 y轴平行且把2个矩形分隔开的线, 也是 logn的复杂度可以找到
然后确定单个矩形所在的大致区域以后 可以 分成4次 用 4个二分 分别二分x = x1, x = x2, y = y1, y = y2 这4条线, 确定一条线以后就确定了一个相应的坐标,从而求出 x1, x2, y1, y2
---------------------------------
| |
| |
| |
---------------------------------
总共要确定2个矩形, 共 8 个点, 所以 复杂度 O(8logn);
此外在代码实现上,还有一些小注意点, 已经用注释的方式标在了源码里 ☺☺。 题目挺好的, 想到思路不难,但正确的代码实现挺辛苦了, 写了许久,也WA好几发,Debug了比较长的时间 ⊙﹏⊙‖∣。
还有,做交互题的时候, 不要(不能) 用 cin.tie(0); 否则会出问题的。
对于 "cin.tie(0);" 的功能,
在默认的情况下cin绑定的是cout,每次执行 << 操作符的时候都要调用flush,这样会增加IO负担。可以通过tie(0)(0表示NULL)来解除cin与cout的绑定,进一步加快执行效率。
#include <iostream>#include <cstdio>#include <cstdlib>using namespace std;typedef long long LL;const int maxn = 1e6 + 8;void confirm_a_rectangle(LL &x1, LL &y1, LL &x2, LL &y2){ LL op, tptr, mid; //x1 mid = x1; tptr = x2; if(x1 + 1 < tptr) cout << "? " << x1 + 1 << " " << y1 + 1 << " " << x2 << " " << y2 << "\n"; fflush(stdout); while(x1 + 1 < tptr){ cin >> op; if(op == 1){ x1 = mid; mid = (x1 + tptr) / 2; if(!(x1 + 1 < tptr)) break; // 不多余的输出下面这句 cout << "? " << mid + 1 << " " << y1 + 1 << " " << x2 << " " << y2 << "\n"; fflush(stdout); } else if(op == 0){ tptr = mid; mid = (x1 + tptr) / 2; if(!(x1 + 1 < tptr)) break; // 不多余的输出下面这句 cout << "? " << mid + 1 << " " << y1 + 1 << " " << x2 << " " << y2 << "\n"; fflush(stdout); } } //cout << "x1 " << x1 << endl; //x2 mid = x2; tptr = x1; if(tptr + 1 < x2) cout << "? " << x1 + 1 << " " << y1 + 1 << " " << x2 << " " << y2 << "\n"; fflush(stdout); while(tptr + 1 < x2){ cin >> op; if(op == 1){ x2 = mid; mid = (x2 + tptr) / 2; // if(mid == 0) break; if(!(tptr + 1 < x2)) break; // 不多余的输出下面这句 cout << "? " << x1 + 1 << " " << y1 + 1 << " " << mid << " " << y2 << "\n"; fflush(stdout); } else if(op == 0){ tptr = mid; mid = (x2 + tptr) / 2; // if(mid == 0) break; if(!(tptr + 1 < x2)) break; // 不多余的输出下面这句 cout << "? " << x1 + 1 << " " << y1 + 1 << " " << mid << " " << y2 << "\n"; fflush(stdout); } } //cout << "x2 " << x2 << endl; //y1 mid = y1; tptr = y2; if(y1 + 1 < tptr) cout << "? " << x1 + 1 << " " << y1 + 1 << " " << x2 << " " << y2 << "\n"; fflush(stdout); while(y1 + 1 < tptr){ cin >> op; if(op == 1){ y1 = mid; mid = (y1 + tptr) / 2; if(!(y1 + 1 < tptr)) break; // 不多余的输出下面这句 cout << "? " << x1 + 1 << " " << mid + 1 << " " << x2 << " " << y2 << "\n"; fflush(stdout); } else if(op == 0){ tptr = mid; mid = (y1 + tptr) / 2; if(!(y1 + 1 < tptr)) break; // 不多余的输出下面这句 cout << "? " << x1 + 1 << " " << mid + 1 << " " << x2 << " " << y2 << "\n"; fflush(stdout); } } //cout << "y1 " << y1 << endl; //y2 mid = y2; tptr = y1; if(tptr + 1 < y2) cout << "? " << x1 + 1 << " " << y1 + 1 << " " << x2 << " " << y2 << "\n"; fflush(stdout); //前面WA是因为这里, 直接输出了, 但并没有进入while while(tptr + 1 < y2){ cin >> op; if(op == 1){ y2 = mid; mid = (y2 + tptr) / 2; // if(mid == 0) break; if(!(tptr + 1 < y2)) break; //// 不多余的输出下面这句 cout << "? " << x1 + 1 << " " << y1 + 1 << " " << x2 << " " << mid << "\n"; fflush(stdout); } else if(op == 0){ tptr = mid; mid = (y2 + tptr) / 2; // if(mid == 0) break; if(!(tptr + 1 < y2)) break; //// 不多余的输出下面这句 cout << "? " << x1 + 1 << " " << y1 + 1 << " " << x2 << " " << mid << "\n"; fflush(stdout); } } //cout << "y2 " << y2 << endl;}int main(){ #ifdef LOCAL //freopen("d.txt", "r", stdin); //freopen("o.txt", "w", stdout); int T = 1; while(T--){ #endif // LOCAL ios::sync_with_stdio(false); //cin.tie(0);j交互题 不能用 cin.tie(0); 来加速IO LL n, op, x1, y1, x2, y2, mid, ansx1 = -1, ansy1 = -1, ansx2 = -1, ansy2 = -1, ansx3 = -1, ansy3 = -1, ansx4 = -1, ansy4 = -1; cin >> n; x1 = y1 = 0, x2 = y2 = n; //while(true){ //go down or go down cout << "? " << 1 << " " << 1 << " " << n << " " << n << "\n"; fflush(stdout); mid = n; while(y1 + 1 < y2){ cin >> op; if(op == 2){ y2 = mid; mid = (y1 + y2) / 2; // if(mid == 0) break; if(!(y1 + 1 < y2)) break; cout << "? " << 1 << " " << 1 << " " << n << " " << mid << "\n"; fflush(stdout); } else if(op == 1){ cout << "? " << 1 << " " << mid + 1 << " " << n << " " << n << "\n"; fflush(stdout); cin >> op; if(op == 1){ //!!!!!! 已划分出2个矩形 ansx1 = 0, ansy1 = 0, ansx2 = n, ansy2 = mid; confirm_a_rectangle(ansx1, ansy1, ansx2, ansy2); ansx3 = 0, ansy3 = mid, ansx4 = n, ansy4 = n; confirm_a_rectangle(ansx3, ansy3, ansx4, ansy4); cout << "! " << ansx1 + 1 << " " << ansy1 + 1 << " " << ansx2 << " " << ansy2 <<" " << ansx3 + 1 << " " << ansy3 + 1 << " " << ansx4 << " " << ansy4 << "\n"; fflush(stdout); exit(0); } else if(op == 0){ y2 = mid; mid = (y1 + y2) / 2; // if(mid == 0) break; if(!(y1 + 1 < y2)) break; cout << "? " << 1 << " " << 1 << " " << n << " " << mid << "\n"; fflush(stdout); } } else if(op == 0){ y1 = mid; mid = (y1 + y2) / 2; // if(mid == 0) break; if(!(y1 + 1 < y2)) break; cout << "? " << 1 << " " << 1 << " " << n << " " << mid << "\n"; fflush(stdout); } } //go rights or left //cin >> op; cout << "? " << 1 << " " << 1 << " " << n << " " << n << "\n"; fflush(stdout); mid = n; while(x1 + 1 < x2){ cin >> op; if(op == 2){ x2 = mid; mid = (x1 + x2) / 2; cout << "? " << 1 << " " << 1 << " " << mid << " " << n << "\n"; fflush(stdout); } else if(op == 1){ cout << "? " << mid + 1 << " " << 1 << " " << n << " " << n << "\n"; fflush(stdout); cin >> op; if(op == 1){ //!!!!!! 已划分出2个矩形 ansx1 = 0, ansy1 = 0, ansx2 = mid, ansy2 = n; confirm_a_rectangle(ansx1, ansy1, ansx2, ansy2); ansx3 = mid, ansy3 = 0, ansx4 = n, ansy4 = n; confirm_a_rectangle(ansx3, ansy3, ansx4, ansy4); cout << "! " << ansx1 + 1 << " " << ansy1 + 1 << " " << ansx2 << " " << ansy2 <<" " << ansx3 + 1 << " " << ansy3 + 1 << " " << ansx4 << " " << ansy4 << "\n"; fflush(stdout); exit(0); } else if(op == 0){ x2 = mid; mid = (x1 + x2) / 2; cout << "? " << 1 << " " << 1 << " " << mid << " " << n << "\n"; fflush(stdout); } } else if(op == 0){ x1 = mid; mid = (x1 + x2) / 2; cout << "? " << 1 << " " << 1 << " " << mid << " " << n << "\n"; fflush(stdout); } } //} #ifdef LOCAL cout << endl; } #endif // LOCAL return 0;}
Thank you!
------from ProLights
- Codeforces Round #371 (Div. 2) D. Searching Rectangles 平面矩形二分法、交互题
- Codeforces Round #371 (Div. 2)D. Searching Rectangles交互题
- Codeforces Round #371 (Div. 1)B Searching Rectangles
- Codeforces Round #219 (Div. 2) D. Counting Rectangles is Fun
- 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 714D Searching Rectangles
- Codeforces Round #395(Div. 2)D. Timofey and rectangles【思维】好题!
- Codeforces Round #395 (Div. 2) D Timofey and rectangles(思维题)
- Codeforces Round #337 (Div. 2) D (矩形面积并
- Codeforces Round #395 (Div. 2) D. Timofey and rectangles(思路)
- CF371 D Searching Rectangles
- Codeforces Round #416 (Div. 2) D. Vladik and Favorite Game【交互题+BFS】
- Codeforces Round #261 (Div. 2) D题
- Codeforces Round #277 (Div. 2)D题
- Codeforces Round #371 (Div. 2) C. Sonya and Queries 压位、二进制来状态压缩
- Microsoft.VisualStudio.Dialogs.DialogInitializationException 问题解决
- 【工具推荐】PDF和其他格式的相关的转换
- leetcode:399. Evaluate Division
- 312. Burst Balloons
- Codeforces Round #371 (Div. 2) D. Searching Rectangles 平面矩形二分法、交互题
- 组合数求模
- 撰写合格的REST API
- easyUI TreeGrid 学习笔记
- 安卓学习笔记
- Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
- s3c2440移植MQTT
- Java Socket编程之TCP协议
- Leetcode 392. Is Subsequence (Medium) (cpp)