T1:Adjacent Product
模拟
代码实现
n = int(input())
a = list(map(int, input().split()))
for i in range(n-1):
print(a[i]*a[i+1])
T2:Piano
周期性
代码实现
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
int main() {
int w, b;
cin >> w >> b;
int n = w+b;
string s = "wbwbwwbwbwbw";
int m = s.size();
rep(si, m) {
int cnt = 0;
rep(i, n) {
if (s[(si+i)%m] == 'w') cnt++;
}
if (cnt == w) {
puts("Yes");
return 0;
}
}
puts("No");
return 0;
}
T3:Σ
\(1 + 2 + \cdots + K = \frac{K(K+1)}{2}\) 减去已经出现在序列 \(A\) 中的数
代码实现
n, k = map(int, input().split())
a = list(map(int, input().split()))
ans = k*(k+1)//2
s = set(a)
for x in s:
if x <= k:
ans -= x
print(ans)
T4:Gomamayo Sequence
枚举应该让哪个位置满足 \(T_i = T_{i+1}\)
最终形态有两种:010..xx..010
和 101..xx...101
两种做法操作都是类似的,只介绍第一种
我们可以先将所有奇数位置的字符都翻转过来,即 0
\(\to\) 1
,1
\(\to\) 0
然后我们希望最后会变成从某个位置开始往左都是 0
以及它右边都是 1
的形式
预处理一下前后缀扫一下即可
代码实现
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
int main() {
int n;
string s;
cin >> n >> s;
vector<int> c(n);
rep(i, n) cin >> c[i];
rep(i, n) {
if (i%2 == 1) s[i] ^= '1'^'0';
}
ll ans = 1e18;
rep(bi, 2) {
vector<ll> l(n+1), r(n+1);
rep(i, n) {
l[i+1] = l[i];
if (s[i] == '1') l[i+1] += c[i];
}
for (int i = n-1; i >= 0; --i) {
r[i] = r[i+1];
if (s[i] == '0') r[i] += c[i];
}
for (int i = 1; i < n; ++i) {
ans = min(ans, l[i]+r[i]);
}
rep(i, n) s[i] ^= '0'^'1';
}
cout << ans << '\n';
return 0;
}