人生天地之|

⅔钱强

园龄:1年4个月粉丝:5关注:17

ABC341总结

ABC341总结

Score:1825

Rank:737

F

其实按照题意,原图可能有环,但是因为转移有权值限定,转换一下就是DAG,进行拓扑排序。

G

AK所差最后一题,使用数形结合思想,x轴为数组下标,y轴为值域。

题意是给出左端点,右端点任意,求区间平均值最大

进行前缀和处理,然后会惊奇的发现,平均数转化成了两点间直线斜率。接着使用凸包,单调栈排除不可能决策,栈顶即为当前答案。

时空复杂度均为 O(n)

// Problem: G - Highest Ratio
// Contest: AtCoder - Toyota Programming Contest 2024#2(AtCoder Beginner Contest 341)
// URL: https://atcoder.jp/contests/abc341/tasks/abc341_g
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
#define EPS 1e-8
#define ll long long
#define ld long double
#define PII pair<int,int>
#define PDD pair<int,int>
#define max3(a,b,c) max(max(a,b),c)
#define max4(a,b,c,d) max(max3(a,b,c),d)
#define lowbit(x) ((x)&-(x))
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define Yes printf("Yes")
#define No printf("No")
using namespace std;
//#pragma GCC optimize(2)
int read(){
	int res=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9')f=ch=='-'?-1:f,ch=getchar();
    while(ch>='0' && ch<='9')res=res*10+ch-'0',ch=getchar();
    return res*f;
}
void write(int x){
	if(x<0)putchar('-'),x=-x;
	if(x>9)write(x/10);
	putchar(x%10+'0');
}
const int N=2e5+10;
int n;
ll a[N];
int stk[N],tp;
ld ans[N];
ld slope(int i,int j){
	return (ld)((ld)a[j]-(ld)a[i])/(ld)(j-i);
}
int main(){
	cin>>n;
	a[0]=0;
	for(int i=1;i<=n;++i){
		cin>>a[i];
		a[i]+=a[i-1];
	}
	stk[tp=1]=n;
	for(int i=n-1;i>=0;--i){
		// printf("%d %d\n",i,stk[tp]);
		while(tp>1&&
			slope(i,stk[tp])<=slope(i,stk[tp-1]))
				tp--;//i也是凸包的一部分
		ans[i+1]=slope(i,stk[tp]);
		stk[++tp]=i;
	}
	rep(i,1,n)printf("%.8Lf\n",ans[i]);
	puts("");
	return 0;
}

本文作者:⅔钱强

本文链接:https://www.cnblogs.com/life-of-a-libertine/p/18028452

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   ⅔钱强  阅读(25)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起