Codeforces 35E

本文介绍了一种用于求解城市天际线轮廓线的高效算法。该算法利用multiset数据结构处理多栋建筑物的高度信息,并通过一系列逻辑判断生成轮廓线的顶点坐标,最终输出轮廓线的最小面积和最短路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

E. Parade
time limit per test
4 seconds
memory limit per test
64 megabytes
input
input.txt
output
output.txt

No Great Victory anniversary in Berland has ever passed without the war parade. This year is not an exception. That’s why the preparations are on in full strength. Tanks are building a line, artillery mounts are ready to fire, soldiers are marching on the main square... And the air forces general Mr. Generalov is in trouble again. This year a lot of sky-scrapers have been built which makes it difficult for the airplanes to fly above the city. It was decided that the planes should fly strictly from south to north. Moreover, there must be no sky scraper on a plane’s route, otherwise the anniversary will become a tragedy. The Ministry of Building gave the data on n sky scrapers (the rest of the buildings are rather small and will not be a problem to the planes). When looking at the city from south to north as a geometrical plane, the i-th building is a rectangle of height hi. Its westernmost point has the x-coordinate of li and the easternmost — of ri. The terrain of the area is plain so that all the buildings stand on one level. Your task as the Ministry of Defence’s head programmer is to find an enveloping polyline using the data on the sky-scrapers. The polyline’s properties are as follows:

  • If you look at the city from south to north as a plane, then any part of any building will be inside or on the boarder of the area that the polyline encloses together with the land surface.
  • The polyline starts and ends on the land level, i.e. at the height equal to 0.
  • The segments of the polyline are parallel to the coordinate axes, i.e. they can only be vertical or horizontal.
  • The polyline’s vertices should have integer coordinates.
  • If you look at the city from south to north the polyline (together with the land surface) must enclose the minimum possible area.
  • The polyline must have the smallest length among all the polylines, enclosing the minimum possible area with the land.
  • The consecutive segments of the polyline must be perpendicular.

Picture to the second sample test (the enveloping polyline is marked on the right).

Input

The first input line contains integer n (1 ≤ n ≤ 100000). Then follow n lines, each containing three integers hiliri (1 ≤ hi ≤ 109,  - 109 ≤ li < ri ≤ 109).

Output

In the first line output integer m — amount of vertices of the enveloping polyline. The next m lines should contain 2 integers each — the position and the height of the polyline’s vertex. Output the coordinates of each vertex in the order of traversing the polyline from west to east. Remember that the first and the last vertices of the polyline should have the height of 0.

Sample test(s)
input
2
3 0 2
4 1 3
output
6
0 0
0 3
1 3
1 4
3 4
3 0
input
5
3 -3 0
2 -1 1
4 2 4
2 3 7
3 6 8
output
14
-3 0
-3 3
0 3
0 2
1 2
1 0
2 0
2 4
4 4
4 2
6 2
6 3
8 3
8 0

分析:求轮廓线

对于multiset S,S.erase(n)表示擦除所有n,S.erase(S.find(n))表示擦除其中1个n

#include<cstdio>
#include<set>
#include<algorithm>
using namespace std;
const int N=100002;
template <class T>  
inline bool scan_d(T &ret){  
	char c; int sgn;  
	if(c=getchar(),c==EOF) return 0; 
	while(c!='-'&&(c<'0'||c>'9')) c=getchar();  
	sgn=(c=='-')?-1:1;  
	ret=(c=='-')?0:(c-'0');  
	while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');  
	ret*=sgn;  
	return 1;  
}
inline void out(int x){ 
	if(x<0){putchar('-');x=-x;}
	if(x>9)out(x/10);  
	putchar(x%10+'0');  
}
struct line{
	int x,h;
	bool operator<(line p)const{return x<p.x;}
}a[N<<1];
int x[N<<2],y[N<<2];
multiset<int> S;
multiset<int>::iterator it;
int main(){
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
	int n,nn,i,h,l,r,max,cnt;
	while(scan_d(n)){
		S.clear();
		for(i=0;i<n;i++){
			scan_d(h),scan_d(l),scan_d(r);
			a[i<<1].x=l,a[i<<1|1].x=r;
			a[i<<1].h=h,a[i<<1|1].h=-h;
		}nn=n<<1;
		sort(a,a+nn);
		S.insert(0);
		x[0]=a[0].x,y[0]=0,cnt=1;
		for(i=0;i<nn;i++){
			if(a[i].h>0)S.insert(a[i].h);
			else S.erase(S.find(-a[i].h));
			if(i==nn-1||a[i].x!=a[i+1].x){
				it=S.end();
				max=*--it;
				if(max!=y[cnt-1]){
					x[cnt]=a[i].x,y[cnt]=y[cnt-1],cnt++;
					x[cnt]=a[i].x,y[cnt]=max,cnt++;
				}
			}
		}
		out(cnt-1),putchar('\n');
		for(i=1;i<cnt;i++)out(x[i]),putchar(' '),out(y[i]),putchar('\n');
	}
	return 0;
}


### Codeforces 887E Problem Solution and Discussion The problem **887E - The Great Game** on Codeforces involves a strategic game between two players who take turns to perform operations under specific rules. To tackle this challenge effectively, understanding both dynamic programming (DP) techniques and bitwise manipulation is crucial. #### Dynamic Programming Approach One effective method to approach this problem utilizes DP with memoization. By defining `dp[i][j]` as the optimal result when starting from state `(i,j)` where `i` represents current position and `j` indicates some status flag related to previous moves: ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = ...; // Define based on constraints int dp[MAXN][2]; // Function to calculate minimum steps using top-down DP int minSteps(int pos, bool prevMoveType) { if (pos >= N) return 0; if (dp[pos][prevMoveType] != -1) return dp[pos][prevMoveType]; int res = INT_MAX; // Try all possible next positions and update 'res' for (...) { /* Logic here */ } dp[pos][prevMoveType] = res; return res; } ``` This code snippet outlines how one might structure a solution involving recursive calls combined with caching results through an array named `dp`. #### Bitwise Operations Insight Another critical aspect lies within efficiently handling large integers via bitwise operators instead of arithmetic ones whenever applicable. This optimization can significantly reduce computation time especially given tight limits often found in competitive coding challenges like those hosted by platforms such as Codeforces[^1]. For detailed discussions about similar problems or more insights into solving strategies specifically tailored towards contest preparation, visiting forums dedicated to algorithmic contests would be beneficial. Websites associated directly with Codeforces offer rich resources including editorials written after each round which provide comprehensive explanations alongside alternative approaches taken by successful contestants during live events. --related questions-- 1. What are common pitfalls encountered while implementing dynamic programming solutions? 2. How does bit manipulation improve performance in algorithms dealing with integer values? 3. Can you recommend any online communities focused on discussing competitive programming tactics? 4. Are there particular patterns that frequently appear across different levels of difficulty within Codeforces contests?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值