C. Sakurako’s Field Trip

思路

对于每个位置计算交换以及不交换分别会产生的扰乱度,根据扰乱度的大小决定是否进行交换,最后统计一次答案即可

Solution
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
using ll = long long;

void solve() {
int n;
cin >> n;
vector<int> a(n + 1);
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 2; i <= n / 2; i++) {
int pre = (a[i] == a[i - 1]) + (a[n - i + 1] == a[n - i + 2]);
int nex = (a[i] == a[n - i + 2]) + (a[n - i + 1] == a[i - 1]);
if (nex < pre)
swap(a[i], a[n - i + 1]);
}
int ans = 0;
for (int i = 2; i <= n; i++) {
if (a[i] == a[i - 1])
ans++;
}
cout << ans << '\n';
}

int main() {
cin.tie(nullptr)->sync_with_stdio(false);
// cout << fixed << setprecision(20);
int tt = 1;
cin >> tt;
while (tt--) {
solve();
}
return 0;
}

D. Kousuke’s Assignment

思路

前缀和相同说明有一段为0

Solution
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
using ll = long long;

void solve() {
int n;
cin >> n;
vector<ll> v(n);
vector<ll> pre(n + 1, 0);
for (int i = 0; i < n; ++i)
cin >> v[i];
for (int i = 0; i < n; i++) {
pre[i + 1] = pre[i] + v[i];
}
unordered_map<ll, ll> mp;
int ans = 0;
for (int i = 0; i <= n; i++) {
if (mp[pre[i]]) {
ans++;
mp.clear();
mp[pre[i]] = 1;
} else {
mp[pre[i]] = 1;
}
}
cout << ans << '\n';
}

int main() {
cin.tie(nullptr)->sync_with_stdio(false);
// cout << fixed << setprecision(20);
int tt = 1;
cin >> tt;
while (tt--) {
solve();
}
return 0;
}