9.22~9.27
AtCoder abc346_d
题目要求只有一处相邻元素相同。
需要考虑的因素有:
- 代价(最优化属性)
- 目前是第几位
- 当前位是否翻转
- 是否出现过两个相邻元素相同
定义:
转移:
当
-
dp[i][0][1] = min(dp[i - 1][0][0], dp[i - 1][1][1]) -
dp[i][0][0] = dp[i - 1][1][0] -
dp[i][1][1] = min(dp[i - 1][1][0], dp[i - 1][0][1]) + c[i] -
dp[i][1][0] = dp[i - 1][0][0] + c[i] 否则
-
dp[i][0][1] = min(dp[i - 1][1][0], dp[i - 1][0][1]) -
dp[i][0][0] = dp[i - 1][0][0] -
dp[i][1][1] = min(dp[i - 1][0][0], dp[i - 1][1][1]) + c[i] -
dp[i][1][0] = dp[i - 1][1][0] + c[i]
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 2e5 + 5;
const ll inf = 1e18;
int n;
ll c[N], dp[N][2][2];
string s;
int main() {
ios::sync_with_stdio(false), cin.tie(0);
cin >> n >> s;
s = " " + s;
for (int i = 1; i <= n; ++ i) {
cin >> c[i];
dp[i][0][0] = dp[i][0][1] = dp[i][1][0] = dp[i][1][1] = inf;
}
dp[1][0][0] = 0, dp[1][1][0] = c[1];
for (int i = 2; i <= n; ++ i) {
if (s[i - 1] == s[i]) {
dp[i][0][1] = min(dp[i - 1][0][0], dp[i - 1][1][1]);
dp[i][0][0] = dp[i - 1][1][0];
dp[i][1][1] = min(dp[i - 1][1][0], dp[i - 1][0][1]) + c[i];
dp[i][1][0] = dp[i - 1][0][0] + c[i];
}
else {
dp[i][0][1] = min(dp[i - 1][1][0], dp[i - 1][0][1]);
dp[i][0][0] = dp[i - 1][0][0];
dp[i][1][1] = min(dp[i - 1][0][0], dp[i - 1][1][1]) + c[i];
dp[i][1][0] = dp[i - 1][1][0] + c[i];
}
}
cout << min(dp[n][0][1], dp[n][1][1]);
return 0;
}
AtCoder abc261_d
需要考虑的因素:
- 获得的钱数(最优化属性)
- 第几次抛硬币
- 当前连击次数
定义:
转移:
-
-
dp_{i, 0} = max(dp_{i - 1, 0}, dp_{i - 1, j - 1})
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 5e3 + 5;
const ll inf = 1e18;
ll x[N];
ll val[N], dp[N][N];
int n, m;
int main() {
ios::sync_with_stdio(false), cin.tie(0);
cin >> n >> m;
for (int i = 0; i <= n; ++ i) {
for (int j = 0; j <= n; ++ j) {
dp[i][j] = -inf;
}
}
dp[0][0] = 0;
for (int i = 1; i <= n; ++ i) {
cin >> x[i];
}
for (int i = 1; i <= m; ++ i) {
ll c, y;
cin >> c >> y;
val[c] += y;
}
for (int i = 1; i <= n; ++ i) {
dp[i][0] = dp[i - 1][0];
for (int j = 1; j <= n; ++ j) {
dp[i][j] = dp[i - 1][j - 1] + val[j] + x[i];
dp[i][0] = max(dp[i][0], dp[i - 1][j - 1]);
}
}
ll ans = 0;
for (int j = 1; j <= n; ++ j) {
ans = max(dp[n][j], ans);
}
cout << ans;
return 0;
}
AtCoder abc312_d
要考虑的因素:
- 第几个元素
- 还有几个左括号没有匹配
- 方案数
定义:
转移:
- 若
s[i] == '(',dp_{i, j} += dp_{i - 1, j - 1} - 若
s[i] == '?',dp_{i, j} += (dp_{i - 1, j + 1} + dp_{i - 1, j - 1}) - 若
s[i] == ')',dp_{i, j} += dp_{i - 1, j + 1}
#include<bits/stdc++.h>
using namespace std;
const int N = 3e3 + 5, mod = 998244353;
string s;
int dp[N][N];
int main() {
ios::sync_with_stdio(false), cin.tie(0);
cin >> s;
int n = s.size();
s = ' ' + s;
dp[0][0] = 1;
for (int i = 1; i <= n; ++ i) {
if (s[i] == '(') {
for (int j = 1; j <= i; ++ j) {
dp[i][j] += dp[i - 1][j - 1];
dp[i][j] %= mod;
}
}
if (s[i] == '?') {
for (int j = 0; j <= i; ++ j) {
if (j > 0) {
dp[i][j] += dp[i - 1][j - 1];
dp[i][j] %= mod;
}
if (j < n) {
dp[i][j] += dp[i - 1][j + 1];
dp[i][j] %= mod;
}
}
}
if (s[i] == ')') {
for (int j = 0; j <= i; ++ j) {
dp[i][j] += dp[i - 1][j + 1];
dp[i][j] %= mod;
}
}
}
cout << dp[n][0];
return 0;
}
AtCoder abc344_d
需要考虑的因素:
- 第几次操作
- 花费代价(最优化属性)
- 目前已拼接的长度
定义:
转移:
- 若
j >= s[i][k].size() && t.substr(j - s[i][k].size() + 1, s[i][k].size()) == s[i][k],dp_{i, j} = min(dp_{i - 1, j - s_{i, k}.size()} + 1, dp_{i - 1, j}) - 否则
dp_{i, j} = dp_{i - 1, j}
#include<bits/stdc++.h>
using namespace std;
const int N = 105, inf = 1e9;
string t, s[N][N];
int n, a[N], dp[N][N];
int main() {
ios::sync_with_stdio(false), cin.tie(0);
cin >> t >> n;
int m = t.size();
t = " " + t;
for (int i = 0; i <= n; ++ i) {
for (int j = 1; j <= m; ++ j) {
dp[i][j] = inf;
}
}
for (int i = 1; i <= n; ++ i) {
cin >> a[i];
for (int j = 1; j <= a[i]; ++ j) {
cin >> s[i][j];
}
}
for (int i = 1; i <= n; ++ i) {
for (int j = 1; j <= m; ++ j) {
dp[i][j] = dp[i - 1][j];
for (int k = 1; k <= a[i]; ++ k) {
if (j >= s[i][k].size() && t.substr(j - s[i][k].size() + 1, s[i][k].size()) == s[i][k]) {
dp[i][j] = min(dp[i][j], dp[i - 1][j - s[i][k].size()] + 1);
}
}
}
}
cout << (dp[n][m] == inf ? -1 : dp[n][m]);
return 0;
}
AtCoder abc262_d
要考虑的因素:
- 当前元素编号
- 共选出了几个元素
- 平均值为整数的方案数
- 平均值
-
dp_{i + 1, j + 1, (k + a_{i + 1})\mod len} += dp_{i, j, k}
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 105, MOD = 998244353;
ll dp[N][N][N], a[N];
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
ll ans = 0;
for (int k = 1; k <= n; ++ k) {
for (int i = 0; i <= n; ++ i) {
for (int j = 0; j <= k; ++ j) {
for (int s = 0; s < k; ++ s) {
dp[i][j][s] = 0;
}
}
}
dp[0][0][0] = 1;
for (int i = 0; i < n; ++ i) {
for (int j = 0; j <= k; ++ j) {
for (int s = 0; s < k; ++ s) {
ll v = dp[i][j][s];
if (!v) {
continue;
}
dp[i + 1][j][s] = (dp[i + 1][j][s] + v) % MOD;
if (j < k) {
dp[i + 1][j + 1][(s + a[i + 1]) % k] = (dp[i + 1][j + 1][(s + a[i + 1]) % k] + v) % MOD;
}
}
}
}
ans = (ans + dp[n][k][0]) % MOD;
}
cout << ans;
return 0;
}
AtCoder abc317_d
需要考虑的因素:
- 第几个区
- 高桥获得的总席位数
- 该区要有多少人改投高桥(最优化属性)
第
定义:
转移:
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 105, V = 1e5 + 5;
const ll inf = 1e18;
ll x[N], y[N], dp[N][V];
int n, z[N];
int main() {
ios::sync_with_stdio(false), cin.tie(0);
cin >> n;
int sumz = 0;
for (int i = 1; i <= n; ++ i) {
cin >> x[i] >> y[i] >> z[i];
sumz += z[i];
}
for (int i = 0; i <= n; ++ i) {
for (int j = 0; j <= sumz; ++ j) {
dp[i][j] = inf;
}
}
dp[0][0] = 0;
for (int i = 1; i <= n; ++ i) {
for (int j = 0; j <= sumz; ++ j) {
dp[i][j] = dp[i - 1][j];
if (j >= z[i]) {
dp[i][j] = min(dp[i][j], dp[i - 1][j - z[i]] + max((y[i] - x[i] + 1) / 2, 0ll));
}
}
}
ll ans = inf;
for (int j = (sumz + 1) / 2; j <= sumz; ++ j) {
ans = min(dp[n][j], ans);
}
cout << ans;
return 0;
}
AtCoder abc264_f
需要考虑的因素:
- 当前位置
- 当前行是否被翻转过
- 当前列是否被翻转过
- 花费(最优化属性)
定义:
转移:
令
#include<bits/stdc++.h>
using namespace std;
const int N = 1e3 + 5, inf = 1e9;
int h, w, a[N][N], dp[N][2][2];
bool ck(int i, int op1, int op2) {
for (int j = 1; j <= w; ++ j) {
if (a[i][j] != a[i][j - 1] && a[i][j] != a[i][j + 1] && a[i][j] != (op1 ^ a[i - 1][j]) && a[i][j] != (op2 ^ a[i + 1][j])) {
return 0;
}
}
return 1;
}
int main() {
ios::sync_with_stdio(false), cin.tie(0);
cin >> h >> w;
for (int i = 0; i <= h + 1; ++ i) {
for (int j = 0; j <= w + 1; ++ j) {
a[i][j] = inf;
}
}
for (int i = 1; i <= h; ++ i) {
for (int j = 1; j <= w; ++ j) {
cin >> a[i][j];
}
}
for (int i = 0; i <= h + 1; ++ i) {
dp[i][0][0] = dp[i][0][1] = dp[i][1][0] = dp[i][1][1] = inf;
}
dp[1][0][0] = 0, dp[1][1][0] = 1;
for (int i = 2; i <= h + 1; ++ i) {
for (int op1 = 0; op1 < 2; ++ op1) {
for (int op2 = 0; op2 < 2; ++ op2) {
for (int op3 = 0; op3 < 2; ++ op3) {
if (ck(i - 1, op2 ^ op3, op1 ^ op2)) {
dp[i][op1][op2] = min(dp[i][op1][op2], dp[i - 1][op2][op3] + op1);
}
}
}
}
}
int ans = min(dp[h + 1][0][1], dp[h + 1][0][0]);
cout << (ans <= h ? ans : -1);
return 0;
}
CodeForces 1353F
结论:至少有一个点高度不变。
证明:假设最优路径上所有点的高度组成的序列被修改为:
枚举不变的点,以固定高度。
那需要考虑的因素有:
- 当前的位置
- 操作次数(最优化属性)
定义:
特殊处理一下和
每个
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 105;
const ll inf = 1e18;
int n, m;
ll a[N][N], dp1[N][N], dp2[N][N], ans = inf;
ll dist(int ax, int ay, int bx, int by) {
return (abs(ax - bx) + abs(ay - by));
}
void work(int sx, int sy) {
for (int i = 0; i <= n; ++ i) {
for (int j = 0; j <= m; ++ j) {
dp1[i][j] = dp2[i][j] = inf;
}
}
dp1[sx][sy] = dp2[sx][sy] = 0;
for (int i = sx - 1; i >= 1; -- i) {
ll cnt = a[i][sy] - a[sx][sy] + dist(i, sy, sx, sy);
if (cnt >= 0) {
dp1[i][sy] = dp1[i + 1][sy] + cnt;
}
}
for (int j = sy - 1; j >= 1; -- j) {
ll cnt = a[sx][j] - a[sx][sy] + dist(sx, j, sx, sy);
if (cnt >= 0) {
dp1[sx][j] = dp1[sx][j + 1] + cnt;
}
}
for (int i = sx - 1; i >= 1; -- i) {
for (int j = sy - 1; j >= 1; -- j) {
ll cnt = a[i][j] - a[sx][sy] + dist(i, j, sx, sy);
if (cnt >= 0) {
dp1[i][j] = min(dp1[i + 1][j], dp1[i][j + 1]) + cnt;
}
}
}
for (int i = sx + 1; i <= n; ++ i) {
ll now = a[i][sy] - (a[sx][sy] + dist(i, sy, sx, sy));
if (now >= 0) {
dp2[i][sy] = dp2[i - 1][sy] + now;
}
}
for (int i = sy + 1; i <= m; ++ i) {
ll now = a[sx][i] - (a[sx][sy] + dist(sx, i, sx, sy));
if (now >= 0) {
dp2[sx][i] = dp2[sx][i - 1] + now;
}
}
for (int i = sx + 1; i <= n; ++ i) {
for (int j = sy + 1; j <= m; ++ j) {
ll cnt = a[i][j] - (a[sx][sy] + dist(i, j, sx, sy));
if (cnt >= 0) {
dp2[i][j] = min(dp2[i - 1][j], dp2[i][j - 1]) + cnt;
}
}
}
ans = min(ans, dp1[1][1] + dp2[n][m]);
}
void solve() {
cin >> n >> m;
for (int i = 1; i <= n; ++ i) {
for (int j = 1; j <= m; ++ j) {
cin >> a[i][j];
}
}
ans = inf;
if (n == 1 && m == 1) {
cout << "0\n";
return ;
}
for (int i = 1; i <= n; ++ i) {
for (int j = 1; j <= m; ++ j) {
work(i, j);
}
}
cout << ans << "\n";
}
int main() {
ios::sync_with_stdio(false), cin.tie(0);
int T;
cin >> T;
while (T --) {
solve();
}
return 0;
}
CodeForces 1042D
暴力的做法是预处理前缀和,再枚举左右端点统计区间和小于等于
若
#include<bits/stdc++.h>
#define lowbit(x) (x & (-x))
using namespace std;
using ll = long long;
const int N = 2e5 + 5;
int n, a[N];
ll t, tr[2 * N], sum[N], tmp[N];
void update(int x, ll k) {
while (x <= 2 * n) {
tr[x] += k;
x += lowbit(x);
}
}
ll query(int x) {
ll cnt = 0;
while (x > 0) {
cnt += tr[x];
x -= lowbit(x);
}
return cnt;
}
int main() {
ios::sync_with_stdio(false), cin.tie(0);
cin >> n >> t;
for (int i = 1; i <= n; ++ i) {
cin >> a[i];
sum[i] = sum[i - 1] + a[i];
tmp[i] = sum[i];
}
sort(tmp, tmp + n + 1);
ll ans = 0;
for (int i = 1; i <= n; ++ i) {
int pos = lower_bound(tmp, tmp + n + 1, sum[i - 1]) - tmp + 1;
update(pos, 1);
pos = upper_bound(tmp, tmp + n + 1, sum[i] - t) - tmp;
ans += (i - query(pos));
}
cout << ans;
return 0;
}
AtCoder abc378_e
求:
不考虑出现负数情况下,令
因为
#include<bits/stdc++.h>
#define lowbit(x) (x & (-x))
using namespace std;
using ll = long long;
const int N = 2e5 + 5;
ll n, m, a[N], cnt, ans;
ll tr[N], sum[N];
int l = 1;
struct Node{
ll val, id;
} s[N];
bool operator<(const Node &a, const Node &b) {
if (a.val == b.val) {
return a.id < b.id;
}
return a.val < b.val;
}
void update(ll x, ll k) {
while(x <= l) {
tr[x] += k;
x += lowbit(x);
}
}
ll query(ll x) {
ll sum = 0;
while(x) {
sum += tr[x];
x -= lowbit(x);
}
return sum;
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; ++ i) {
cin >> a[i];
s[i].val = (s[i - 1].val + a[i]) % m;
s[i].id = i;
}
for (int i = 1; i <= n; ++ i) {
ans += (2 * i - n) * s[i].val;
}
sort(s + 1, s + n + 1);
sum[s[1].id] = 1;
for (int i = 2; i <= n; ++ i) {
sum[s[i].id] = (s[i].val == s[i - 1].val ? l : ++ l);
}
for (int i = 1; i <= n; ++ i) {
update(sum[i], 1);
cnt += i - query(sum[i]);
}
cout << ans + m * cnt;
return 0;
}
CodeForces 474F
结论:获得自由的蚂蚁的力量值为区间 gcd。
用线段树记录区间 gcd 和 gcd 的个数即可。
#include<bits/stdc++.h>
#define lc i << 1
#define rc i << 1 | 1
using namespace std;
const int N = 1e5 + 5;
int n, t;
struct Node{
int x = 0, cnt = 0;
}tr[N * 4];
int s[N];
int gcd(int x, int y) {
if (!y) {
return x;
}
return gcd(y, x % y);
}
void push_up(int i) {
tr[i].x = gcd(tr[lc].x, tr[rc].x);
tr[i].cnt = 0;
if (tr[i].x == tr[lc].x) {
tr[i].cnt += tr[lc].cnt;
}
if (tr[i].x == tr[rc].x) {
tr[i].cnt += tr[rc].cnt;
}
}
void build(int i, int l, int r) {
if (l == r) {
tr[i].x = s[l];
tr[i].cnt = 1;
return ;
}
int mid = (l + r) >> 1;
build(lc, l, mid);
build(rc, mid + 1, r);
push_up(i);
}
Node query(int i, int l, int r, int x, int y) {
if (x <= l && r <= y) {
return tr[i];
}
int mid = (l + r) >> 1;
Node tmp1, tmp2;
if (mid >= x) {
tmp1 = query(lc, l, mid, x, y);
}
if (mid < y) {
tmp2 = query(rc, mid + 1, r, x, y);
}
if (mid >= x && mid < y) {
int g = gcd(tmp1.x, tmp2.x), ans = 0;
if (tmp1.x == g) {
ans += tmp1.cnt;
}
if (tmp2.x == g) {
ans += tmp2.cnt;
}
return {g, ans};
}
else if (mid >= y) {
return tmp1;
}
else if (mid < x) {
return tmp2;
}
else {
return {0, 0};
}
}
int main() {
ios::sync_with_stdio(false), cin.tie(0);
cin >> n;
for (int i = 1; i <= n; ++ i) {
cin >> s[i];
}
build(1, 1, n);
cin >> t;
while (t --) {
int l, r;
cin >> l >> r;
int len = r - l + 1;
cout << len - query(1, 1, n, l, r).cnt << "\n";
}
return 0;
}
AtCoder abc254_f
结论:
证明: 令
所以
即
本题中对于第
注:左上角要单独处理
#include<bits/stdc++.h>
#define lc i << 1
#define rc i << 1 | 1
using namespace std;
const int N = 2e5 + 5;
int n, q;
int a[N], b[N], da[N], db[N];
struct Node{
int l, r;
int data, datb;
}tr[N * 4];
int gcd(int x, int y) {
if (!y) {
return x;
}
return gcd(y, x % y);
}
void push_up(int i) {
tr[i].data = gcd(tr[lc].data, tr[rc].data);
tr[i].datb = gcd(tr[lc].datb, tr[rc].datb);
}
void build(int i, int l, int r) {
tr[i].l = l, tr[i].r = r;
if (l == r) {
tr[i].data = da[l];
tr[i].datb = db[l];
return ;
}
int mid = (l + r) >> 1;
build(lc, l, mid);
build(rc, mid + 1, r);
push_up(i);
}
int querya(int i, int x, int y) {
if (tr[i].l >= x && tr[i].r <= y) {
return tr[i].data;
}
int mid = (tr[i].l + tr[i].r) >> 1, res = 0;
if (mid >= x) {
res = gcd(querya(lc, x, y), res);
}
if (mid < y) {
res = gcd(querya(rc, x, y), res);
}
return res;
}
int queryb(int i, int x, int y) {
if (tr[i].l >= x && tr[i].r <= y) {
return tr[i].datb;
}
int mid = (tr[i].l + tr[i].r) >> 1, res = 0;
if (mid >= x) {
res = gcd(queryb(lc, x, y), res);
}
if (mid < y) {
res = gcd(queryb(rc, x, y), res);
}
return res;
}
int main() {
ios::sync_with_stdio(false), cin.tie(0);
cin >> n >> q;
for (int i = 1; i <= n; ++ i) {
cin >> a[i];
da[i] = a[i] - a[i - 1];
}
for (int i = 1; i <= n; ++ i) {
cin >> b[i];
db[i] = b[i] - b[i - 1];
}
build(1, 1, n);
while (q --) {
int h1, h2, w1, w2;
cin >> h1 >> h2 >> w1 >> w2;
int ans = a[h1] + b[w1];
ans = gcd(ans, querya(1, h1 + 1, h2));
ans = gcd(ans, queryb(1, w1 + 1, w2));
cout << abs(ans) << "\n";
}
return 0;
}
CodeForces 1701F
无解情况:
令最左端的点为
令
考虑将点
对本身的影响为:新增一段合法区间,即新增
删除的影响则相反,直接减去相差值即可。
那我们就用线段树记录:
- 点
x 是否还存在。 - 合法区间中每个点
i ,区间[i, i + d] 的点数。 - 点
x 的合法区间内的点数。
#include<bits/stdc++.h>
#define lc i << 1
#define rc i << 1 | 1
using namespace std;
using ll = long long;
const int N = 2e5 + 5, V = 2e5;
int q, d;
bool vis[N];
struct Node {
ll op = 0, cnt = 0, val = 0, lazy = 0;
}tr[N * 4];
void push_down(int i, ll l, ll r) {
if (!tr[i].lazy) {
return ;
}
tr[lc].lazy += tr[i].lazy;
tr[rc].lazy += tr[i].lazy;
int mid = (l + r) >> 1;
tr[lc].cnt += tr[lc].op * tr[i].lazy;
tr[rc].cnt += tr[rc].op * tr[i].lazy;
tr[lc].val += (mid - l + 1) * tr[i].lazy;
tr[rc].val += (r - mid) * tr[i].lazy;
tr[i].lazy = 0;
}
void push_up(int i) {
tr[i].val = tr[lc].val + tr[rc].val;
tr[i].cnt = tr[lc].cnt + tr[rc].cnt;
tr[i].op = tr[lc].op + tr[rc].op;
}
void updateop(int i, ll l, ll r, ll x, ll k) {
if (!x) {
return ;
}
if (l == r) {
tr[i].op += k;
if (!tr[i].op) {
tr[i].cnt = 0;
}
else {
tr[i].cnt = tr[i].val;
}
return ;
}
push_down(i, l, r);
ll mid = (l + r) >> 1;
if (mid >= x) {
updateop(lc, l, mid, x, k);
}
else {
updateop(rc, mid + 1, r, x, k);
}
push_up(i);
}
void updatecnt(int i, ll l, ll r, ll x, ll y, ll k) {
if (x > y) {
return ;
}
if (l >= x && r <= y) {
tr[i].val += (r - l + 1) * k;
tr[i].cnt += k * tr[i].op;
tr[i].lazy += k;
return ;
}
push_down(i, l, r);
ll mid = (l + r) >> 1;
if (mid >= x) {
updatecnt(lc, l, mid, x, y, k);
}
if (mid < y) {
updatecnt(rc, mid + 1, r, x, y, k);
}
push_up(i);
}
ll queryval(int i, ll l, ll r, ll x) {
if (!x) {
return 0;
}
if (l == r) {
return tr[i].val;
}
push_down(i, l, r);
ll mid = (l + r) >> 1, res = 0;
if (mid >= x) {
res += queryval(lc, l, mid, x);
}
else {
res += queryval(rc, mid + 1, r, x);
}
return res;
}
ll querycnt(int i, ll l, ll r, ll x, ll y) {
if (x > y) {
return 0;
}
if (l >= x && r <= y) {
return tr[i].cnt;
}
push_down(i, l, r);
ll mid = (l + r) >> 1, res = 0;
if (mid >= x) {
res += querycnt(lc, l, mid, x, y);
}
if (mid < y) {
res += querycnt(rc, mid + 1, r, x, y);
}
return res;
}
int main() {
ios::sync_with_stdio(false), cin.tie(0);
cin >> q >> d;
ll ans = 0;
while (q --) {
ll x;
cin >> x;
ll l = max(1ll, x - d), r = x - 1;
if (d == 1) {
cout << "0\n";
continue;
}
if (!vis[x]) {
ans += querycnt(1, 1, V, l, r);
updatecnt(1, 1, V, l, r, 1);
ll cnt = queryval(1, 1, V, x);
ans += (cnt * (cnt - 1) / 2);
updateop(1, 1, V, x, 1);
vis[x] = 1;
}
else {
updatecnt(1, 1, V, l, r, -1);
ans -= querycnt(1, 1, V, l, r);
ll cnt = queryval(1, 1, V, x);
ans -= (cnt * (cnt - 1) / 2);
updateop(1, 1, V, x, -1);
vis[x] = 0;
}
cout << ans << "\n";
}
return 0;
}
AtCoder abc285_e
假设某个连续工作日段的长度为
一个长度为
我们可以预先计算出所有可能的
- 若
L=0, ~ \operatorname{Cost}(0)=0 。 - 若
L>0 ,设m=\lfloor(L+1) / 2\rfloor 。 - 若
L 是奇数:\operatorname{Cost}(L)=2 \times\left(A_1+\cdots+A_{m-1}\right)+A_m - 若
L 是偶数:\operatorname{Cost}(L)=2 \times\left(A_1+\cdots+A_m\right)
现在,问题转化为:我们有
因此,问题等价于一个完全背包问题: \begin{itemize}
- 背包容量为
N (总天数)。 - 物品的重量为
L+1 (对应一段"假日+L 个工作日"),价值为\operatorname{Cost}(L) 。 - 每种物品(长度
L )可以选无限次。 - 求恰好装满容量为
N 的背包时的最大价值。
AtCoder abc287_g
单点修改、全局前
权值线段树的每个节点维护两个信息:当前分数区间内的卡片总数
对于查询操作,在线段树上二分:优先查右子树(高分数),如果右子树总数大于等于
#include <bits/stdc++.h>
#define lc i << 1
#define rc i << 1 | 1
using namespace std;
using ll = long long;
const int N = 4e5 + 5;
int n, Q;
int a[N], b[N];
int q_type[N], q_x[N], q_y[N];
int val[N], res;
ll cnt[N * 4], s[N * 4];
int get_id(int v) {
int pos = lower_bound(val + 1, val + res + 1, v) - val;
if (pos <= res && val[pos] == v) {
return pos;
}
return -1;
}
void update(int i, int l, int r, int x, ll k) {
if (l == r) {
cnt[i] += k;
s[i] += k * val[l];
return;
}
int mid = (l + r) / 2;
if (x <= mid) {
update(lc, l, mid, x, k);
}
else {
update(rc, mid + 1, r, x, k);
}
cnt[i] = cnt[lc] + cnt[rc];
s[i] = s[lc] + s[rc];
}
ll query(int i, int l, int r, ll k) {
if (k <= 0) {
return 0;
}
if (l == r) {
return k * val[l];
}
int mid = (l + r) / 2;
if (cnt[rc] >= k) {
return query(rc, mid + 1, r, k);
}
else {
return s[rc] + query(lc, l, mid, k - cnt[rc]);
}
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(0);
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> a[i] >> b[i];
val[++ res] = a[i];
}
cin >> Q;
for (int i = 1; i <= Q; ++i) {
cin >> q_type[i];
if (q_type[i] == 1) {
cin >> q_x[i] >> q_y[i];
val[++ res] = q_y[i];
}
else if (q_type[i] == 2) {
cin >> q_x[i] >> q_y[i];
}
else {
cin >> q_x[i];
}
}
sort(val + 1, val + res + 1);
res = unique(val + 1, val + res + 1) - (val + 1);
for (int i = 1; i <= n; ++i) {
update(1, 1, res, get_id(a[i]), b[i]);
}
for (int i = 1; i <= Q; ++i) {
if (q_type[i] == 1) {
int x = q_x[i], y = q_y[i];
update(1, 1, res, get_id(a[x]), -b[x]);
a[x] = y;
update(1, 1, res, get_id(a[x]), b[x]);
}
else if (q_type[i] == 2) {
int x = q_x[i], y = q_y[i];
update(1, 1, res, get_id(a[x]), -b[x]);
b[x] = y;
update(1, 1, res, get_id(a[x]), b[x]);
}
else {
ll x = q_x[i];
if (cnt[1] < x) {
cout << -1 << "\n";
}
else {
cout << query(1, 1, res, x) << "\n";
}
}
}
return 0;
}
CodeForces 1070C
将问题转化为:对于每一天,在当天所有可用的方案中,尽可能选择价格