跳到正文

题解

P2061 [USACO07OPEN] City Horizon S 题解

Feny_233约 2 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 2 分钟
题目
P2061

版本

  1. v1当前

Solution

从左到右扫描,维护当前最高建筑。数据要求维护添加元素,删除元素并支持重复元素。而c++中的STL multiset 恰好支持这些操作。下面介绍一下multiset:

C++
multiset<int> ms;//构造multiset

ms.insert(x); //添加元素x
ms.find(x) //返回元素x对应的指针
ms.erase(ms.find(x)); //删除一个元素x
ms.rbegin() //返回集合中最大值的指针

Code

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e5;
int n,cnt,ans;
struct building{
	int p,h,opt;
	bool operator<(const building &u){
		return p<u.p;
	}
}a[N];
multiset<int> ms;
signed main()
{
	cin>>n;
	for(int i=1;i<=n;i++) {
		int x,y,h;cin>>x>>y>>h;
		a[++cnt]={x,h,1},a[++cnt]={y,h,-1};
	}
	sort(a+1,a+1+cnt);
	ms.insert(0);
	for(int i=1;i<=cnt;i++){
		ans+=(a[i].p-a[i-1].p)*(*ms.rbegin());
		if(a[i].opt==1) ms.insert(a[i].h);
		else ms.erase(ms.find(a[i].h));
	}
	cout<<ans;
	return 0;
}
原文