码迷,mamicode.com
首页 > 其他好文 > 详细

C - Lamps

时间:2020-06-15 15:42:11      阅读:80      评论:0      收藏:0      [点我收藏+]

标签:个数   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 xd0.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

标签:个数   namespace   thml   long   integer   memset   element   turn   cin   

原文地址:https://www.cnblogs.com/asunayi/p/13131125.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!