ACM-ICPC 2018 徐州赛区网络预赛 G. Trace(set,模拟)
程序员文章站
2022-06-04 15:16:20
...
题目链接:https://nanti.jisuanke.com/t/31459
样例输入
3
1 4
4 1
3 3
样例输出
10
题意:n个矩阵,不存在包含,矩阵左下角都在(0,0),给右上角坐标,后来的矩阵会覆盖前面的矩阵,求矩阵周长
思路:set按照x从大到小排序,从后往前遍历,放入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;
}
推荐阅读
-
ACM-ICPC 2018 徐州赛区网络预赛 H - Ryuji doesn't want to study
-
ACM-ICPC 2018 徐州赛区网络预赛 H. Ryuji doesn't want to study(线段树区间求和)
-
ACM-ICPC 2018 徐州赛区网络预赛 H. Ryuji doesn't want to study—— 树状数组
-
ACM-ICPC 2018 徐州赛区网络预赛 H Ryuji doesn't want to study(线段树 两种做法)
-
【ACM-ICPC 2018 徐州赛区网络预赛】H题 Features Track ---- 树状数组
-
ACM-ICPC 2018 徐州赛区网络预赛 G. Trace (线段树维护)
-
ACM-ICPC 2018 徐州赛区网络预赛 Trace
-
Trace-----ACM-ICPC 2018 徐州赛区网络预赛
-
ACM-ICPC 2018 徐州赛区网络预赛-G Trace 题解 树状数组
-
ACM-ICPC 2018 徐州赛区网络预赛 - G Trace - 思维