1041: Sequence
Description
Giving a collection S of points on two dimension plane. (S = {(x0,y0), (x1,y1), ... }) We define a point is greater than another point when all its coordinate on two axis are both greater than or equel to another one. Namely, p is greater than q when xp >= xq and yp >= yq. A sequence is a list points < p1, p2, ... > satisfy that i < j => pi is greater than pj. You can use the elements in S to construct sequences, how many sequences needed to cover a S at least?
Input
The input consists of several test cases. Each test case start with a line containing a number n(0 < n <= 1000000), the number of points in S. Then n lines follows, each line containing two number, xi, yi(0 <= xi, yi < 100000), the position of point i. The input end with EOF.
Output
You have to print minium number of sequences needed to cover S in a single line for each case.
Sample Input
4
1 1
2 2
3 3
4 4
4
1 5
2 6
2 3
3 4
Sample Output
1
2
HINT
Source
#include<bits/stdc++.h>
#define maxn 1000010
using namespace std;
struct node{
int x,y;
bool operator <(const node&a)const{
if(x!=a.x) return x<a.x;
return y<a.y;
}
}A[maxn];
int N;
set<int> st;set<int>::iterator it;
int judge(int k){
it=st.lower_bound(A[k].y);//利用set的自排序和“二分”查找可以方便很多
if(it!=st.end()){
st.erase(*it);st.insert(A[k].y);
return 1;
}
st.insert(A[k].y);return 0;
}
int main(){
//freopen("in.txt","r",stdin);
while(cin>>N){
for(int i=0;i<N;i++)
scanf("%d %d",&A[i].x,&A[i].y);
sort(A,A+N);
int cnt=0;
st.clear();
for(int i=N-1;i>=0;i--)
if(judge(i)) continue;
else cnt++;
printf("%d\n",cnt);
}
return 0;
}