[CEOI2011]Balloons 单调栈

原题:https://www.luogu.org/problemnew/show/P4697

题解:给n个气球,和最大的膨胀半径,当这个气球与他之前相切或到最大的膨胀半径时停止充气,求每个气球的半径。考虑暴力的方法,每个气球应与他之前的都比一遍,最小半径就是这个点的半径。若一个点的半径比之前的一个大,那么它前面一个就是答案。所以有效半径应该是递减的,可以用单调栈弹栈时算答案就行了。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=220000;
struct node{
	double x;
	double r;
}stack[N];
int n,top;
inline double pow(double x){
	return x*x;
}
double a[N],r,x;
double calc(double xi,double xj,double rj){
	return 0.25*pow(xi-xj)/rj;
}
int main(){
//	freopen("bal2.in","r",stdin);
	scanf("%d",&n);
	top=0;
	scanf("%lf%lf",&x,&r);
	stack[++top].x=x;stack[top].r=r;
	a[1]=stack[top].r;
	for(int i=2;i<=n;i++){
		double x,r; 
		scanf("%lf%lf",&x,&a[i]);
		while(top){
			a[i]=min(a[i],calc(x,stack[top].x,stack[top].r));
			if(a[i]>stack[top].r) top--;
			else break;
		}
		stack[++top].x=x;stack[top].r=a[i]; 
	}
	for(int i=1;i<=n;i++) printf("%lf\n",a[i]);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_39689721/article/details/88079542