ACM-ICPC 2018 徐州赛区网络预赛 G. Trace(set,模拟)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yz467796454/article/details/82661675

题目链接: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;
}

猜你喜欢

转载自blog.csdn.net/yz467796454/article/details/82661675
今日推荐