题目描述:
133. Border
time limit per test: 0.5 sec.
memory limit per test: 4096 KB
Along the border betweenstates A and B there are N defence outposts. For everyoutpost k, the interval [Ak,Bk] whichis guarded by it is known. Because of financial reasons, the presidentof country A decided that some of the outposts should be abandoned.In fact, all the redundant outposts will be abandoned. An outpost iis redundant if there exists some outpost j such that Aj<Aiand Bi<Bj. Your task is to find the numberof redundant outposts.
Input
The first line ofthe input will contain the integer number N (1<=N<=16 000).Nlines will follow, each of them containing 2 integers:Akand Bk (0<= Ak < Bk <=2 000 000 000), separated by blanks. All the numbers Ak willbe different. All the numbersBk will be different.
Output
You should print thenumber of redundant outposts.
Sample Input
5
0 10
2 9
3 8
1 15
6 11
Sample Output
3
我们可以先离散化,因为数据范围比较大。
然后按坐标做个排序 假设排序后的坐标是 ix1 ix2 iy1 iy2 ......
观察发现,只要某个左边的右半部分出现,如果其左半部分的左边仍有点,那么这个区间就可以忽略。
我是用了一种类似链表的东西来储存依次出现的区间端点。
添加和删除复杂度都是O(1) ;
贴个代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<set>
#include<algorithm>
#include<vector>
#include<cstdlib>
#define inf 0xfffffff
#define CLR(a,b) memset((a),(b),sizeof((a)))
#define FOR(a,b) for(int a=1;a<=(b);(a)++)
using namespace std;
int const nMax = 40000;
int const base = 10;
typedef int LL;
typedef pair<LL,LL> pij;
struct Int{
int a,x;
bool operator <(const Int& b)const {
if(x==b.x)return a<b.a;
return x<b.x;
}
}p[nMax];
int L[nMax],R[nMax];
int main(){
int n;
int ans(0);
scanf("%d",&n);
for(int i=1,j=1;i<=n;i++,j+=2){
scanf("%d%d",&p[j].x,&p[j+1].x);
p[j].a=-i;p[j+1].a=i;
}
sort(p+1,p+1+2*n);R[0]=L[0]=0;
for(int i=1;i<=n;i++)L[i]=R[i]=-1;
for(int i=1;i<=2*n;i++){
int tmp=abs(p[i].a);
if(R[tmp]==-1){
R[L[0]]=tmp;
L[tmp]=L[0];
R[tmp]=0;
L[0]=tmp;
}else {
if(L[tmp]){
ans++;
}
int u=R[tmp];
R[L[tmp]]=u;
L[u]=L[tmp];
L[tmp]=R[tmp]=-1;
}
}
printf("%d\n",ans);
return 0;
}