C - Lamps
Posted asunayi
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C - Lamps相关的知识,希望对你有一定的参考价值。
We have N bulbs arranged on a number line, numbered 1 to N from left to right. Bulb i is at coordinate i.
Each bulb has a non-negative integer parameter called intensity. When there is a bulb of intensity d at coordinate x, the bulb illuminates the segment from coordinate x−d−0.5 to x+d+0.5. Initially, the intensity of Bulb i is Ai. We will now do the following operation K times in a row:
- For each integer i between 1 and N (inclusive), let Bi be the number of bulbs illuminating coordinate i. Then, change the intensity of each bulb i to Bi.
Find the intensity of each bulb after the K operations.
题意:坐标轴上1-n有n个灯泡,给出每个灯泡的初始亮度,进行k次操作,每次用照亮该坐标的灯泡个数更新该坐标上灯泡的亮度,最后输出每个灯泡亮度。
思路:常规做法一定会超时,所以用差分数组+每个灯泡亮度最大时退出循环特判。
#include <iostream> #include <string.h> using namespace std; #define ll long long const int N=2e5+5; int a[N],b[N]; int main(){ int n,k;cin>>n>>k; for(int i=0;i<n;i++) cin>>a[i]; while(k--){ int flag=0; memset(b,0,sizeof(b)); for(int i=0;i<n;i++){ b[max(i-a[i],0)]++; if(i+a[i]+1<=n-1) b[i+a[i]+1]--; } a[0]=b[0]; for(int i=1;i<n;i++){ b[i]+=b[i-1]; a[i]=b[i]; } for(int i=1;i<n;i++){ if(a[i]!=n){ flag=1; break; } } if(flag==0) break; } for(int i=0;i<n;i++) cout<<a[i]<<‘ ‘; return 0; }
以上是关于C - Lamps的主要内容,如果未能解决你的问题,请参考以下文章
Post Lamps CodeForces - 990E(暴力出奇迹?)
codeforce 985B Switches and Lamps(暴力+思维)