A题传送
B题传送
A. Single Push
AC代码
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 3;
int a[N], b[N];
int n, t;
int main() {
scanf("%d", &t);
while(t --) {
scanf("%d", &n);
vector<int> v, s;
for(int i = 0; i < n; i++) scanf("%d", &a[i]);
for(int i = 0; i < n; i++) {
scanf("%d", &b[i]);
if(b[i] - a[i] != 0) s.push_back(i), v.push_back(b[i] - a[i]);
}
bool f = 0;
if(s.size() > 1)
for(int i = 1; i < s.size(); i++) if(s[i] != s[i - 1] + 1) {f = 1; break;}
if(v.size()) {
if(v[0] < 0) f = 1;
for(int i = 1; i < v.size(); i++) {
if(v[i] != v[i - 1]) {
f = 1; break;
}
}
}
if(f) puts("NO");
else puts("YES");
}
return 0;
}
B. Silly Mistake
AC代码
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 100;
long long a[N], s[N];
int n, k;
vector<long long> b;
bool solve(int n) {
b.push_back(0);
set<long long> se;
for(int i = 1; i <= n; i++) scanf("%lld", &a[i]);
for(int i = 1; i <= n; i++) {
s[i] = s[i - 1] + a[i];
if(a[i] < 0 && !se.count(-a[i])) return 0;
else if(se.count(a[i])) return 0;
se.insert(a[i]);
if(s[i] == 0) {
se.clear();
b.push_back(i);
}
}
if(s[n] != 0) return 0;
return 1;
}
int main() {
scanf("%d", &n);
if(!solve(n) || (n & 1)) cout << "-1" << '\n';
else {
cout << b.size() - 1 << '\n';
for(int i = 1; i < b.size(); i++) cout << b[i] - b[i - 1] << ' ';
}
return 0;
}