CodeForces 1579F :Array Stabilization (AND version) 思维
作者:互联网
传送门
题意
给定一个数组
n
n
n,下标从
0
0
0开始,只包含
0
0
0和
1
1
1.给定
d
d
d。现在需要你确定经过多少次操作后数组元素全为0。
操作为将数组循环移位
d
d
d,再和原数组相与得到新数组。
分析
首先0和任何数操作完都是0,所以只要考虑将0移动1的位置即可
代码
#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define debug(x) cout<<#x<<":"<<x<<endl;
#define dl(x) printf("%lld\n",x);
#define di(x) printf("%d\n",x);
#define _CRT_SECURE_NO_WARNINGS
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef vector<int> VI;
const int INF = 0x3f3f3f3f;
const int N = 2e6 + 10;
const ll mod = 1000000007;
const double eps = 1e-9;
const double PI = acos(-1);
template<typename T>inline void read(T &a) {
char c = getchar(); T x = 0, f = 1; while (!isdigit(c)) {if (c == '-')f = -1; c = getchar();}
while (isdigit(c)) {x = (x << 1) + (x << 3) + c - '0'; c = getchar();} a = f * x;
}
int gcd(int a, int b) {return (b > 0) ? gcd(b, a % b) : a;}
int n,a[N],d;
int solve(){
int res = 0;
queue<PII> q;
for(int i = 0;i < n;i++) if(!a[i]) q.push({0,i});
while(q.size()){
auto t = q.front();
q.pop();
int x = (t.se + d) % n;
if(a[x]){
a[x] = 0;
q.push({t.fi + 1,x});
res = max(res,t.fi + 1);
}
}
for(int i = 0;i < n;i++) if(a[i]) res = -1;
return res;
}
int main() {
int T;
read(T);
while(T--){
read(n),read(d);
for(int i = 0;i < n;i++) read(a[i]);
di(solve());
}
return 0;
}
标签:1579F,Stabilization,int,res,CodeForces,read,while,数组,const 来源: https://blog.csdn.net/tlyzxc/article/details/120616472