Codeforces 1028C(面积并/思维)
傳送門
題面:
C. Rectangles
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given?nn?rectangles on a plane with coordinates of their bottom left and upper right points. Some?(n?1)(n?1)?of the given?nn?rectangles have some common point. A point belongs to a rectangle if this point is strictly inside the rectangle or belongs to its boundary.
Find any point with integer coordinates that belongs to at least?(n?1)(n?1)?given rectangles.
Input
The first line contains a single integer?nn?(2≤n≤1326742≤n≤132674) — the number of given rectangles.
Each the next?nn?lines contains four integers?x1x1,?y1y1,?x2x2?and?y2y2?(?109≤x1<x2≤109?109≤x1<x2≤109,??109≤y1<y2≤109?109≤y1<y2≤109) — the coordinates of the bottom left and upper right corners of a rectangle.
Output
Print two integers?xx?and?yy?— the coordinates of any point that belongs to at least?(n?1)(n?1)?given rectangles.
Examples
input
Copy
3 0 0 1 1 1 1 2 2 3 0 4 1output
Copy
1 1input
Copy
3 0 0 1 1 0 1 1 2 1 0 2 1output
Copy
1 1input
Copy
4 0 0 5 5 0 0 4 4 1 1 4 4 1 1 4 4output
Copy
1 1input
Copy
5 0 0 10 8 1 2 6 7 2 3 5 6 3 4 4 5 8 1 9 2output
Copy
3 4Note
The picture below shows the rectangles in the first and second samples. The possible answers are highlighted.
The picture below shows the rectangles in the third and fourth samples.
題意:
? ? 給你n個矩陣的左下角的點和右上角的點,讓你求出一點使得這個點包含最少n-1個矩陣。(題目數(shù)據(jù)保證一定有解)。
題目分析:
? ? 因為題目中要求我們求出一個包含n-1個矩陣的點,因此我們不難想到這個點一定是在這n個矩形的面積并中。因此我們考慮從面積并入手解決問題。
? ? 考慮我們優(yōu)先對矩形進行正向遍歷,在遍歷過程中不斷維護前綴面積并pre,此時我們可以發(fā)現(xiàn),正向遍歷后的結(jié)果必為存在一個至少包含n-1個面積并矩形或者不存在矩形。而根據(jù)題意必定有解,則此時,倘若我們反向遍歷,在遍歷過程中維護后綴面積并suf,則此時遍歷后的結(jié)果必定有一個面積并矩形。而我們的答案則必定是在正反遍歷后得到的面積并矩陣之中。
? ? 因此我們只需要枚舉前綴面積并pre和后綴面積并suf,并在最后判斷得到的每一個面積并矩形是否能構(gòu)成矩形即可。
代碼:
#include <bits/stdc++.h> #define maxn 150000 using namespace std; const int INF=0x3f3f3f3f; struct node{int lx,ly,rx,ry;node(){}node(int _lx,int _ly,int _rx,int _ry){lx=_lx,ly=_ly,rx=_rx,ry=_ry;} }q[maxn],pre[maxn],suf[maxn]; bool judge(node a){if(a.lx>a.rx||a.ly>a.ry) return false;return true; } int main() {int n;scanf("%d",&n);for(int i=1;i<=n;i++){scanf("%d%d%d%d",&q[i].lx,&q[i].ly,&q[i].rx,&q[i].ry);}pre[0].lx=pre[0].ly=-INF;pre[0].rx=pre[0].ry=INF;for(int i=1;i<=n;i++){//維護前綴面積并pre[i].lx=max(pre[i-1].lx,q[i].lx);pre[i].ly=max(pre[i-1].ly,q[i].ly);pre[i].rx=min(pre[i-1].rx,q[i].rx);pre[i].ry=min(pre[i-1].ry,q[i].ry);}suf[n+1].lx=suf[n+1].ly=-INF;suf[n+1].rx=suf[n+1].ry=INF;for(int i=n;i>=1;i--){//維護后綴面積并suf[i].lx=max(suf[i+1].lx,q[i].lx);suf[i].ly=max(suf[i+1].ly,q[i].ly);suf[i].rx=min(suf[i+1].rx,q[i].rx);suf[i].ry=min(suf[i+1].ry,q[i].ry);}for(int i=1;i<=n;i++){int tmplx=max(pre[i-1].lx,suf[i+1].lx);int tmply=max(pre[i-1].ly,suf[i+1].ly);int tmprx=min(pre[i-1].rx,suf[i+1].rx);int tmpry=min(pre[i-1].ry,suf[i+1].ry);if(judge(node(tmplx,tmply,tmprx,tmpry))){//判斷是否能構(gòu)成矩形cout<<tmplx<<" "<<tmply<<endl;return 0;}}return 0; }?
轉(zhuǎn)載于:https://www.cnblogs.com/Chen-Jr/p/11007216.html
總結(jié)
以上是生活随笔為你收集整理的Codeforces 1028C(面积并/思维)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python 之路N01'
- 下一篇: 枚举值转换(字符串转换为枚举和整数转换为