T1
四个问互相包含,按顺序判断即可。
code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 200007;
const ll ee = 1e18;
string c1, s1, c2, s2;
int main(void) {
//freopen("data.in", "r", stdin);
//freopen("data.out", "w", stdout);
ios::sync_with_stdio(0), cin.tie(0);
cin >> c1 >> s1 >> c2 >> s2;
if (s1[2] == 'F') s1[2] = 'E';
if (s2[2] == 'F') s2[2] = 'E';
if (c1 != c2) cout << "0000\n";
else if (s1.substr(0, 2) != s2.substr(0, 2)) cout << "1000\n";
else if (min(s1[2], s2[2]) <= 'C' && max(s1[2], s2[2]) > 'C') cout << "1100\n";
else if (min(s1[2], s2[2]) + 1 != max(s1[2], s2[2])) cout << "1110\n";
else cout << "1111\n";
return 0;
}T2
用一正一反两个指针扫,每次挪较短的那一个。
code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 200007;
const ll ee = 1e18;
int n, a[maxn];
char c[maxn];
ll ans;
int main(void) {
//freopen("data.in", "r", stdin);
//freopen("data.out", "w", stdout);
ios::sync_with_stdio(0), cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i] >> c[i];
}
ll lc = 0, rc = 0;
for (int l = 1, r = n; l <= r; ) {
if (lc < rc) {
ans += (c[l] != c[r + 1]) * (min(rc, lc + a[l]) - lc);
lc += a[l];
l++;
} else if (lc > rc) {
ans += (c[l - 1] != c[r]) * (min(lc, rc + a[r]) - rc);
rc += a[r];
r--;
} else {
ans += (c[l] != c[r]) * min(a[l], a[r]);
lc += a[l], rc += a[r];
l++, r--;
}
}
cout << ans << "\n";
return 0;
}T3
按位考虑,从最高位到最低位,若能达到 个 就把这一位为 的所有数删掉并将答案该位置一。
code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 500007;
const ll ee = 1e18;
int n, m, a[maxn], vis[maxn], v2[maxn], ans;
int main(void) {
//freopen("data.in", "r", stdin);
//freopen("data.out", "w", stdout);
ios::sync_with_stdio(0), cin.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> a[i], vis[i] = 1;
for (int i = 29; i >= 0; i--) {
int cnt = 0, v;
for (int j = 1; j <= n; j++) {
if (!vis[j]) continue;
v = (a[j] >> i & 1);
cnt += v, v2[j] = v;
}
ans <<= 1;
if (cnt >= m) {
ans++;
for (int j = 1; j <= n; j++) {
vis[j] &= v2[j];
}
}
}
cout << ans << "\n";
return 0;
}T4
从最后一局开始往前考虑,前面多来一张卡 时,若 在后面未出现过则最终局面等于 的局面多一张 ,否则 被 匹配,最终局面等于 的局面。最终局面的等价性可以连边连成一棵树,隐式求子树大小即可。
code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 500007;
const ll ee = 1e18;
int n, m, a[maxn], cur[maxn], f[maxn], g[maxn], siz[maxn], ans[maxn];
int main(void) {
//freopen("data.in", "r", stdin);
//freopen("data.out", "w", stdout);
ios::sync_with_stdio(0), cin.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = n; i >= 1; i--) {
if (!cur[a[i]]) f[i] = i + 1, g[i] = 1;
else f[i] = cur[a[i]];
cur[a[i]] = i, siz[i] = 1;
}
for (int i = 1; i <= n; i++) siz[f[i]] += siz[i];
for (int i = 1; i <= n; i++)if(g[i]) ans[a[i]] += siz[i];
for (int i = 1; i <= m; i++) cout << ans[i] << " ";
cout << "\n";
return 0;
}