CF1209B Koala and Lights

[亡魂溺海] 提交于 2019-11-29 17:24:13

It is a holiday season, and Koala is decorating his house with cool lights! He owns n lights, all of which flash periodically.

After taking a quick glance at them, Koala realizes that each of his lights can be described with two parameters ai and bi. Light with parameters ai and bi will toggle (on to off, or off to on) every ai seconds starting from the bi-th second. In other words, it will toggle at the moments bi, bi+aibi+2ai and so on.

You know for each light whether it's initially on or off and its corresponding parameters ai and bi. Koala is wondering what is the maximum number of lights that will ever be on at the same time. So you need to find that out.

Here is a graphic for the first example.
Input

The first line contains a single integer n (1≤n≤100), the number of lights.

The next line contains a string s of n characters. The ii-th character is "1", if the i-th lamp is initially on. Otherwise, i-th character is "0".

The i-th of the following n lines contains two integers ai and bi (1≤ai,bi≤5)  — the parameters of the i-th light.

Output

Print a single integer — the maximum number of lights that will ever be on at the same time.

Examples
input
3  101  3 3  3 2  3 1  
output
2  
input
4  1111  3 4  5 2  3 1  3 2  
output
4  
input
6  011100  5 3  5 5  2 4  3 5  4 2  1 5  
output
6  
Note

For first example, the lamps' states are shown in the picture above. The largest number of simultaneously on lamps is 2 (e.g. at the moment 2).

In the second example, all lights are initially on. So the answer is 4.

解释题意:有n盏灯,1代表开0代表关,初始状态已给出,每盏灯有a和b两个属性,bs之后灯切换一次状态,之后每a秒切换一次状态。求最多同时亮几盏灯。

不难想到我们可以模拟所有的状态而所有状态的周期最多是2*3*4*5+5种。保险起见可以多跑几次

#include <bits/stdc++.h>  using namespace std;  bool a[105];  int l[105];  int r[105];  int c[105];  int f[105];  int main()  {      int n;      cin>>n;      string str;      cin>>str;      for(int i=0;i<str.size();++i)      {          if(str[i]=='0')          {              a[i+1]=0;          }          else          {              a[i+1]=1;          }      }      for(int i=1;i<=n;++i)      {          cin>>l[i]>>r[i];          c[i]=l[i];      }      int mmax=0;      int tmp=0;      for(int i=1;i<=n;++i)      {          if(a[i]==1)          {              tmp++;              mmax=max(mmax,tmp);          }      }      for(int k=1;k<=1000;++k)      {          int tmp=0;          for(int i=1;i<=n;++i)          {              if(a[i]==1)              {                  tmp++;                  mmax=max(mmax,tmp);              }          }          for(int i=1;i<=n;++i)          {              if(f[i]==1&&r[i]==0)              {                  c[i]--;                  if(c[i]==0)                  {                      c[i]=l[i];                      a[i]=!a[i];                  }              }              if(r[i]>0)              {                  r[i]--;                  if(r[i]==0)                  {                      a[i]=!a[i];                      f[i]=1;                  }              }          }      }      cout<<mmax;      return 0;  }
View Code

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!