sgu 133 Border

23 篇文章 0 订阅
18 篇文章 0 订阅

题目描述:

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;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值