CF1788A One and Two
对于一个序列,题目要求蓝色部分的乘积等于绿色部分的乘积,因为序列中只有 \(1\) 和 \(2\),所以我们只要蓝色部分和绿色部分的 \(2\) 的数量相等即可,使用前缀和统计即可。
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int N = 1010;
int n;
int a[N];
void solve() {
memset(a, 0, sizeof(a));
cin >> n;
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
if (x == 2) a[i] = 1;
}
for (int i = 1; i <= n; i++) a[i] += a[i - 1];
for (int i = 1; i < n; i++) {
if (a[i] == a[n] - a[i]) {
cout << i << '\n';
return;
}
}
cout << -1 << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T;
cin >> T;
while (T--) solve();
return 0;
}
标签:851,int,题解,CF1788,solve,include
From: https://www.cnblogs.com/PlayWithCPP/p/17223761.html