BOJ 394

Problem I. The Longest Sequence of
Rectangles
Description
A rectangle is specified by a pair of coordinates (x1 , y1) and (x2 , y2)
indicating its lower-left and upper-right corners (x1 <= x2 and y1 <= y2). For
a pair of rectangles, A = ((X_A1, Y_A1), (X_A2, Y_A2)) and B = ((X_B1, Y_B1),
(X_B2, Y_B2)), we define A <= B if X_A2<X_B1 and Y_A2<Y_B1. Given a
number of rectangles on the plane, you are asked to find the length L of the
longest sequence of rectangles (A1, A2, ..., AL) such that A1 <= A2 <= ...
<= AL.
Input
The input contains multiple test cases. The first line of the input is an integer
T, indicating the number of test cases. Each test case begins with a line
containing a single integer n (1 <= n <= 100000), indicating the number of
rectangles. Each of the next n lines contains four integers x1, y1, x2, y2 (-
1000000 <= x1 < x2 <= 1000000, -1000000 <= y1 < y2 <= 1000000),
indicating the lower left and upper right corners of a rectangle.
Output
For each input test case, output a single integer indicating the length of the
longest sequence.
Sample Input
2
3
1 5 2 8
3 -1 5 4
10 10 20 20
2
2 1 4 5
6 5 8 10
Sample Output
2
1

#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;

const int MV=200010;

struct point{
int x,y,flag,id;
bool operator<(const point& t)const{
if(x!=t.x)return x<t.x;
return flag < t.flag;
}
}p[MV];

int y[MV], pos[MV];

int main(){
int n, T;
scanf("%d", &T);
while(T--){
scanf("%d", &n);
if(n==0)break;
for( int i=0;i<n;i++){
scanf("%d%d",&p[2*i].x,&p[2*i].y);
p[2*i].flag=0;

scanf("%d%d",&p[2*i+1].x,&p[2*i+1].y);
p[2*i+1].flag=1;

p[2*i].id=p[2*i+1].id=i;
}
n*=2;
sort(p,p+n);

int cnt=0;

for(int i=0;i<n;i++){
if(p[i].flag == 0){
pos[p[i].id] = lower_bound(y+1, y+1+cnt, p[i].y) - y ;
} else {
int tp = pos[p[i].id];
if(tp > cnt)y[++cnt] = p[i].y;
else y[tp] = min(y[tp], p[i].y);
}
}

printf("%d\n",cnt);
}
return 0;
}


LeetCode 394. Decode String 解题报告

2016-10-27 21:01:20

BOJ 394（LIS应用）（nlogn算法）

2014-03-10 17:22:50

【leetcode】394. Decode String

2016-09-19 16:07:33

HDU 4630、BOJ 某题

2013-08-01 00:08:16

BOJ题目1023. Ancient Keyboard 源代码

2012年10月08日 561B 下载

boj 0809复试模拟题答案

2010年04月22日 853KB 下载

火影忍者漫画394话中文.日文情报

2008-03-21 01:09:00

阿里云云服务器活动来临啦 一年99

2018-06-09 18:09:03

NEFU394 素数价值

2013-02-07 00:13:40

LeetCode—394. Decode String

2016-09-06 10:59:39