AT_ddcc2019_final_a 题解

原题传送门

题目描述:

企鹅经过 $1$ 个雪地方格需要 $1$ 秒,经过 $1$ 个冰地方格需要 $\frac{1}{(k + 2)}$ 秒。$k$ 是紧接着冰雪方格之前的冰雪方格数。在企鹅开始之前,高桥可以把 $1$ 个雪方块变成冰方块。问企鹅离开起点后到达终点最少需要多少时间?

思路分析:

这道题是 模拟+贪心 题,企鹅连续通过的冰方块越多,通过时间便越少,我们只需要枚举连续的最长冰方块,同时把总时间记录下来,最后把雪方块替换掉(延长最长冰方块的数量)就行了。

代码:

#include <bits/stdc++.h>
using namespace std;
int n,cntx=0,cntb=0;
double t=0;
string s;
int main()
{
	cin>>n;
	cin>>s;
	for(int i=0;i<n;i++)
	{
		if(s[i]=='-')
		{
			t ++;
			cntb = 0;
		}
		if(s[i]=='>')
		{
			t += 1*1.0/(2+cntb);
			cntb ++;
			cntx = max(cntx,cntb);//更新最长冰方块
		}
		
	}
	printf("%.15lf",t+1*1.0/(cntx+2)-1);//保留15位小数
	return 0;
}

posted @ 2024-02-07 09:22  shixuanbin  阅读(18)  评论(0)    收藏  举报