标签:个数 namespace thml long integer memset element turn cin
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:
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; }
标签:个数 namespace thml long integer memset element turn cin
原文地址:https://www.cnblogs.com/asunayi/p/13131125.html