Codeforces 1849D Array Painting

Problem Link

Notice that the number on each element of the array indicates how many times the element can spread its red color to adjacent elements. Therefore, each consecutive strips of non-zero elements can be painted with one coin.

Then, the point of this problem lies with the patches of zero element.

For each isolated zero element, one coin is certainly required to paint it red. However, for each non-zero patch, at least one and at most two adjacent zero elements can be painted for free, depending on whether there is an element equal to 2 2 2. Therefore, we can simply implement a two-pointer method that greediely paints the zero-elements using the trickle-down effect of the non-zero patches.

In my code, I greedily paint the leftmost zero-elements first. Notice border conditions require special conditions (e.g. no zero elements at the first or last elements).

#include<cstdio>
#include<iostream>
using namespace std;
const int Maxn=2e5+10;
int a[Maxn];
int n,ans;
inline int read()
{
	int s=0,w=1;
	char ch=getchar();
	while(ch<'0' || ch>'9'){if(ch=='-')w=-1;ch=getchar();}
	while(ch>='0' && ch<='9')s=(s<<3)+(s<<1)+(ch^48),ch=getchar();
	return s*w;
}
int main()
{
	// freopen("in.txt","r",stdin);
	n=read();
	for(int i=1;i<=n;++i)
	a[i]=read();
	int l=1,r=1;
	while(l<=n)
	{
		// printf("l = %d\n",l);
		bool flag=0;
		r=l;
		while(!a[r] && r<=n)++ans,++r;
		while(a[r] && r<=n)flag|=(a[r++]==2);
		++ans;
		if(!a[l])--ans;
		if(flag && r<=n || a[l])l=r+1;
		else l=r;
	}
	printf("%d\n",ans);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值