ACM-ICPC 2018 徐州赛区网络预赛 G Trace(思维+set)

https://nanti.jisuanke.com/t/31459

题意

n个矩阵,不存在包含,矩阵左下角都在(0,0),给右上角坐标,后来的矩阵会覆盖前面的矩阵,求矩阵周长。

分析

set按照x或者y从大到小排序,从后往前遍历,放入set,找到当前矩阵在set中的位置,当前矩阵的x或者y,与set中后一位矩阵的x或者y的差值,就是增加的横线或者竖线的长度

感觉套个线段树求周长的模板也行。

#include<queue>
#include<cstring>
#include<string>
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<set>
using namespace std;
typedef long long ll;
 
struct node{
    int x,y;
}a[50004];
 
struct cmp1{
    bool operator()(const node &a,const node &b){
        if(a.x==b.x)return a.y>b.y;
        return a.x>b.x;
    }
};
struct cmp2{
    bool operator()(const node &a,const node &b){
        if(a.y==b.y)return a.x>b.x;
        return a.y>b.y;
    }
};
set<node,cmp1>s1;
set<node,cmp2>s2;
set<node,cmp1>::iterator it1;
set<node,cmp1>::iterator it2;
 
int main(){
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d%d",&a[i].x,&a[i].y);
    }
    ll ans=0;
    node now;
    now.x=0;now.y=0;
    s1.insert(now);
    s2.insert(now);
    for(int i=n;i>=1;i--){
        s1.insert(a[i]);
        s2.insert(a[i]);
        it1=s1.find(a[i]);
        it1++;
        node pre=*it1;
        ans+=a[i].x-pre.x;
        it2=s2.find(a[i]);
        it2++;
        pre=*it2;
        ans+=a[i].y-pre.y;
    }
    printf("%lld\n",ans);
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/fht-litost/p/9668630.html