10.21总结
CuteGirlGC · · 个人记录
预计:100+100+[60, 100]+20 实际:100+70+60+20
T1
后手要尽量使用奇数和偶数来使得答案减一,然后先手可以通过消除奇数或者消除偶数来减少可能的次数。因为最后合并了一定是偶数,所以很好做。
#include <iostream>
using namespace std;
using LL = long long;
const int kMaxN = 2e5 + 5;
LL n, a[kMaxN];
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
int o = 0, e = 0;
LL s = 0;
for (int i = 1; i <= n; i++) {
s += a[i], o += (a[i] % 2), e += (a[i] % 2 == 0);
if (o == 2) {
cout << s << ' ';
continue;
}
if (i == 1) {
cout << s << ' ';
continue;
}
int t = o / 3 + (o % 3 == 1 && (e + o / 3) > 0);
cout << s - t << ' ';
}
return 0;
}
T2
sb贪心,赛时最后才过大样例,最后前面的6个点还挂掉了。
cin >> n >> m;
for (int i = 1; i <= m; i++) {
cin >> a[i] >> b[i];
v[i] = 0;
c[i * 2 - 1] = {a[i], 0, i};
c[i * 2] = {b[i], 1, i};
}
sort(c + 1, c + 2 * m + 1, [](N a, N b) {
return a.v > b.v;
});
LL ans = 0, p = 0, o = 0;
for (int i = 1; i <= 2 * m; i++) {
if (o >= n) {
break;
}
if (!c[i].o) {
v[c[i].id] = 1;
p += c[i].v;
o++;
} else {
if (v[c[i].id]) {
ans = max(ans, c[i].v * (n - o) + p);
cout << ans << '\n';
return;
} else {
ans = max(ans, c[i].v * (n - o - 1) + a[c[i].id] + p);
}
}
}
cout << max(p, ans) << '\n';
T3
人类智慧怎么被卡到 60 了呜呜呜😭😭😭😭😭😭😭
原题 CF 的,可以用线段树优化 dp 或者单调栈优化 dp。
这里我写了一个单调栈优化 dp。
#include <iostream>
using namespace std;
using LL = long long;
const LL kMaxN = 1e5 + 10, kInf = -1e18;
LL h[kMaxN], b[kMaxN], s[kMaxN], val[kMaxN], f[kMaxN], p[kMaxN];
int n, t;
int main() {
cin >> n;
p[0] = kInf;
for (int i = 1; i <= n; i++) {
cin >> h[i];
}
for (int i = 1; i <= n; i++) {
cin >> b[i];
}
for (int i = 1; i <= n; i++) {
LL o = f[i - 1];
while (t && h[i] < h[s[t]]) {
o = max(o, val[t]);
t--;
}
s[++t] = i;
val[t] = o;
f[i] = p[t] = max(p[t - 1], o + b[i]);
}
cout << f[n];
return 0;
}
D
一个树形 dp,可爱长链剖分之前有意向学过,但是最后没有学/ll
#include <iostream>
#include <vector>
using namespace std;
using LL = long long;
const int kMaxN = 1e5 + 10;
vector<int> g[kMaxN];
LL p[kMaxN * 4], *f[kMaxN], *h[kMaxN], *o = p, ans;
int d[kMaxN], dep[kMaxN], son[kMaxN], n;
void D(int x, int fa) {
d[x] = d[fa] + 1;
for (int y : g[x]) {
if (y != fa) {
D(y, x);
if (dep[y] > dep[son[x]]) {
son[x] = y;
}
}
}
dep[x] = dep[son[x]] + 1;
}
void S(int x, int fa) {
if (son[x]) {
f[son[x]] = f[x] + 1;
h[son[x]] = h[x] - 1;
S(son[x], x);
}
f[x][0] = 1;
ans += h[x][0];
for (int y : g[x]) {
if (y != fa && y != son[x]) {
f[y] = o;
o += dep[y] << 1;
h[y] = o;
o += dep[y] << 1;
S(y, x);
for (int i = 0; i < dep[y]; i++) {
if (i) {
ans += f[x][i - 1] * h[y][i];
}
ans += h[x][i + 1] * f[y][i];
}
for (int i = 0; i < dep[y]; i++) {
h[x][i + 1] += f[x][i + 1] * f[y][i];
if (i) {
h[x][i - 1] += h[y][i];
}
f[x][i + 1] += f[y][i];
}
}
}
}
int main() {
cin >> n;
for (int i = 1, u, v; i < n; i++) {
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
D(1, 0);
f[1] = o;
o += dep[1] << 1;
h[1] = o;
o += dep[1] << 1;
S(1, 0);
cout << ans;
return 0;
}