灯的亮灭问题 枚举

 题意   :

给你 N 灯泡的初始状态(开或者关),然后对于每一个灯泡,给出两个值 a,b; a 代表快关状态切换的周期,b代表第一次切换的时刻。即在b, b +a, b+2*a的时候,状态会改变。问在某一时刻,能够同时亮的灯泡数目的最大值。

题目链接

思路:

     枚举每一秒的灯的状态,然后更新最大值。但是,枚举多少秒钟才够,我不知道。反正数据小,只要不超时,往大的整就行。

如果数据比较大的话,就需要用另外的方法了

   

#pragma warning(disable:4996)
#include<iostream>
#include<cstring>
#include<cstdio>
#include<set>
#include<string>
#include<cmath>
#include<algorithm>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
int a[105], b[105], state[105];
char s[105];
int main()
{
	int n, i, j, cnt, ans;
	while (scanf("%d", &n) == 1) {
		scanf("%s", s);
		memset(a, 0, sizeof(a));
		memset(b, 0, sizeof(b));
		for (i = 0; i < n; i++)scanf("%d%d", &a[i], &b[i]);
		ans = 0;
		cnt = 0;
		for (i = 0; i < n; i++) {
			if (s[i] == '1') {
				state[i] = 1;
				ans++;
			}
			else state[i] = 0;
		}
		for (i = 1; i <= 10000; i++) {
			for (j = 0; j < n; j++) {
				if (i >= b[j]) {
					if ((i - b[j]) % a[j] == 0) {
						state[j] ^= 1;
					}
				}
			}
			cnt = 0;
			for (j = 0; j < n; j++) {
				if (state[j] == 1) {
					cnt++;
				}
			}
			if (ans < cnt)ans = cnt;
		}
		printf("%d\n", ans);
	}
	return 0;
}

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值