记录 要炸了,所以写这来了。
2025.10.16
CF149D
显然区间 dp,令 () 表示 左右端点为 颜色的方案数。
转移分讨一下:
- 如果 匹配,那么 也是合法序列,我们直接从 转移过来。由于相邻括号不能染同色,所以要判掉。
- 如果不匹配,那么我们预处理 表示与 匹配的右括号位置,这样我们可以从 和 两个转移过来,同样把不满足限制的判掉即可。
因为比较懒所以直接写了记搜。
。
#include <bits/stdc++.h>
using namespace std;
template<class T> inline bool cmax(T &x, T y) { if (x < y) return x = y, true; return false; }
template<class T> inline bool cmin(T &x, T y) { if (x > y) return x = y, true; return false; }
#define int long long
constexpr int N = 709, md = 1e9 + 7;
int n, f[N][N][3][3], nxt[N], stk[N], tp;
string s;
int dfs(int l, int r, int p, int q) {
if (l + 1 == r) return (p > 0) + (q > 0) == 1;
if (~f[l][r][p][q]) return f[l][r][p][q];
auto &res = f[l][r][p][q] = 0;
if (nxt[l] == r) {
if ((p > 0) + (q > 0) != 1) return res;
for (int i = 0; i < 3; ++i)
for (int j = 0; j < 3; ++j) {
if (i > 0 && i == p) continue;
if (j > 0 && j == q) continue;
(res += dfs(l + 1, r - 1, i, j)) %= md;
}
return res;
}
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
if (i > 0 && i == j) continue;
(res += dfs(l, nxt[l], p, i) * dfs(nxt[l] + 1, r, j, q)) %= md;
}
}
return res;
}
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
cin >> s, n = s.size(), s = ' ' + s;
for (int i = 1; i <= n; ++i) {
if (tp && s[stk[tp]] == '(' && s[i] == ')') nxt[stk[tp--]] = i;
else stk[++tp] = i;
}
memset(f, -1, sizeof f);
int ans = 0;
for (int i = 0; i < 3; ++i)
for (int j = 0; j < 3; ++j) (ans += dfs(1, n, i, j)) %= md;
cout << ans;
}UVA1629
rmj 炸了,不知道过没过,vjudge 也交不了。
设 表示一个矩形的答案,然后每次枚举横切竖切即可。
#include <bits/stdc++.h>
using namespace std;
template<class T> inline bool cmax(T &x, T y) { if (x < y) return x = y, true; return false; }
template<class T> inline bool cmin(T &x, T y) { if (x > y) return x = y, true; return false; }
constexpr int N = 29;
int n, m, k, s[N][N], f[N][N][N][N];
int get(int lx, int ly, int rx, int ry) {
return s[rx][ry] - s[lx - 1][ry] - s[rx][ly - 1] + s[lx - 1][ly - 1];
}
int dfs(int lx, int ly, int rx, int ry) {
if (get(lx, ly, rx, ry) == 0) return 0x3f3f3f3f;
if (get(lx, ly, rx, ry) == 1) return 0;
auto &res = f[lx][ly][rx][ry];
if (res != 0x3f3f3f3f) return res;
for (int i = lx; i < rx; ++i) {
cmin(res, dfs(lx, ly, i, ry) + dfs(i + 1, ly, rx, ry) + ry - ly + 1);
}
for (int i = ly; i < ry; ++i) {
cmin(res, dfs(lx, ly, rx, i) + dfs(lx, i + 1, rx, ry) + rx - lx + 1);
}
return res;
}
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
int tt = 0;
while (cin >> n >> m >> k) {
memset(s, 0, sizeof s);
for (int i = 1; i <= k; ++i) {
int x, y;
cin >> x >> y;
++s[x][y];
}
memset(f, 0x3f, sizeof f);
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j) s[i][j] += s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1];
cout << "Case " << ++tt << ": " << dfs(1, 1, n, m) << '\n';
}
}P5851
区间 dp,令 表示 的答案,转移我们枚举 ,令本次的牛强制吃掉 ,也就是 ,这里 也就是能吃掉 的最重的牛。
同样可以转移 。
。
注意一下 转移的枚举顺序。
#include <bits/stdc++.h>
using namespace std;
#define int long long
template<class T> inline bool cmax(T &x, T y) { if (x < y) return x = y, true; return false; }
template<class T> inline bool cmin(T &x, T y) { if (x > y) return x = y, true; return false; }
constexpr int N = 309;
int n, m, w[N * N], l[N * N], r[N * N], f[N][N], g[N][N][N];
int dfs(int x, int y) {
if (~f[x][y]) return f[x][y];
if (x > y) return 0;
auto &res = f[x][y] = 0;
for (int j = x; j <= y; ++j) {
cmax(res, dfs(x, j - 1) + dfs(j + 1, y) + g[x][y][j]);
}
return res;
}
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n >> m;
for (int i = 1; i <= m; ++i) {
cin >> w[i] >> l[i] >> r[i];
for (int j = l[i]; j <= r[i]; ++j) cmax(g[l[i]][r[i]][j], w[i]);
}
for (int k = 1; k <= n; ++k) {
for (int x = k; x >= 1; --x) {
for (int y = k; y <= n; ++y) {
cmax(g[x][y][k], g[x + 1][y][k]);
cmax(g[x][y][k], g[x][y - 1][k]);
}
}
}
memset(f, -1, sizeof f);
cout << dfs(1, n);
}P2851
做两次背包即可,每次上界均为 。
当老板要找的钱多于 时,则其找的硬币数量必然 ,假设其每个硬币找了 个,对其求前缀和 ,由于序列长度 ,而取模结果只有 种,所以必然有 ,于是可以把这一段压成 个 ,显然更优,因此不会超过 。
同理,也不会付超过 。
#include <bits/stdc++.h>
using namespace std;
template<class T> inline bool cmax(T &x, T y) { if (x < y) return x = y, true; return false; }
template<class T> inline bool cmin(T &x, T y) { if (x > y) return x = y, true; return false; }
constexpr int N = 40009;
int n, t, v[N], c[N], f[N], g[N];
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n >> t;
for (int i = 1; i <= n; ++i) cin >> v[i];
for (int i = 1; i <= n; ++i) cin >> c[i];
int mxv = *max_element(v + 1, v + n + 1) + 4;
mxv *= mxv;
memset(f, 0x3f, sizeof f);
f[0] = 0;
for (int i = 1; i <= n; ++i) {
for (int j = 1, p = 1; j <= c[i]; j += p, p = min(c[i] - j + 1, p << 1)) {
for (int k = t + mxv; k >= p * v[i]; --k) cmin(f[k], f[k - v[i] * p] + p);
}
}
memset(g, 0x3f, sizeof g);
g[0] = 0;
for (int i = 1; i <= n; ++i)
for (int j = v[i]; j <= t + mxv; ++j) cmin(g[j], g[j - v[i]] + 1);
int ans = 0x3f3f3f3f;
for (int i = t; i <= t + mxv; ++i) {
cmin(ans, f[i] + g[i - t]);
}
if (ans == 0x3f3f3f3f) cout << -1;
else cout << ans;
}P2224
学到了!
考虑 dp,令 表示做完前 个,A 花了 的时间, 所需的最小时间。
转移很简单,,滚动掉 就可以了。
。
#include <bits/stdc++.h>
using namespace std;
template<class T> inline bool cmax(T &x, T y) { if (x < y) return x = y, true; return false; }
template<class T> inline bool cmin(T &x, T y) { if (x > y) return x = y, true; return false; }
constexpr int N = 3e4 + 9;
int n, t[3][N], f[N];
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n;
int up = 0;
for (int i = 1; i <= n; ++i) {
cin >> t[0][i] >> t[1][i] >> t[2][i];
up += max({t[0][i], t[1][i], t[2][i]});
}
memset(f, 0x3f, sizeof f);
f[0] = 0;
for (int i = 1; i <= n; ++i) {
for (int j = up; j >= 0; --j) {
if (t[1][i] == 0) f[j] = 0x3f3f3f3f;
else f[j] += t[1][i];
if (j >= t[0][i] && t[0][i] > 0) cmin(f[j], f[j - t[0][i]]);
if (j >= t[2][i] && t[2][i] > 0) cmin(f[j], f[j - t[2][i]] + t[2][i]);
}
}
int ans = 0x3f3f3f3f;
for (int i = 0; i <= up; ++i) cmin(ans, max(f[i], i));
cout << ans;
}P4170
dp,令 表示 的答案,显然有 。
显然,上面的转移并没有包含全部涂一个色的情况,所以我们再手动转移一下即可。
具体地,直接记录一下每个区间内是否有某个颜色,然后考虑这次操作的颜色是和左右端点相同,如果我要的颜色已经涂过了就不用 ,否则要。
。
#include <bits/stdc++.h>
using namespace std;
template<class T> inline bool cmax(T &x, T y) { if (x < y) return x = y, true; return false; }
template<class T> inline bool cmin(T &x, T y) { if (x > y) return x = y, true; return false; }
constexpr int N = 59;
int n, f[N][N], had[N][N][26];
string s;
int dfs(int l, int r) {
if (l > r) return 0;
if (l == r) return 1;
auto &res = f[l][r];
if (res < 0x3f3f3f3f) return f[l][r];
for (int i = l; i < r; ++i) cmin(res, dfs(l, i) + dfs(i + 1, r));
cmin(res, dfs(l + 1, r) + (had[l + 1][r][s[l] - 'a'] == 0));
cmin(res, dfs(l, r - 1) + (had[l][r - 1][s[r] - 'a'] == 0));
return res;
}
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
cin >> s, n = s.size(), s = ' ' + s;
for (int i = 1; i <= n; ++i)
for (int j = i; j <= n; ++j) {
for (int k = 0; k < 26; ++k) had[i][j][k] = had[i][j - 1][k];
++had[i][j][s[j] - 'a'];
}
memset(f, 0x3f, sizeof f);
cout << dfs(1, n);
}P4158
容易发现我们只关心每一行涂了几次,具体怎么涂的不重要。于是考虑先对每一行 dp,令 表示第 行第 列,涂了 次的答案,转移直接找一个 ,然后假设把 全涂了的贡献即可。
对于所有行,我们再设一个 dp,,表示涂完前 行,涂了 次的答案,每次转移枚举一下这一行涂了几次即可。
因为 与 同阶,所以是 。
#include <bits/stdc++.h>
using namespace std;
template<class T> inline bool cmax(T &x, T y) { if (x < y) return x = y, true; return false; }
template<class T> inline bool cmin(T &x, T y) { if (x > y) return x = y, true; return false; }
constexpr int N = 59;
int n, m, t, f[N][N][N], g[N][N * N];
int a[N][N];
int get(int l, int r, int p) { return max(a[p][r] - a[p][l - 1], r - l + 1 + a[p][l - 1] - a[p][r]); }
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n >> m >> t;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
char c;
cin >> c;
a[i][j] = c - '0' + a[i][j - 1];
}
}
for (int i = 1; i <= n; ++i) {
auto &dp = f[i];
memset(dp, -0x3f, sizeof dp);
dp[0][0] = 0;
for (int j = 1; j <= m; ++j) {
for (int k = 0; k <= m; ++k) {
cmax(dp[j][k], dp[j - 1][k]);
if (k == 0) continue;
for (int l = 0; l < j; ++l)
cmax(dp[j][k], dp[l][k - 1] + get(l + 1, j, i));
}
}
}
for (int i = 1; i <= n; ++i) {
for (int j = 0; j <= t; ++j) {
for (int k = 0; k <= j && k <= m; ++k)
cmax(g[i][j], g[i - 1][j - k] + f[i][m][k]);
}
}
cout << g[n][t];
}P4095
首先这个最优化的背包,它不支持撤销,所以考虑维护前后缀背包,由于已知最大体积,所以合并两个背包是 的。
时间复杂度 。
#include <bits/stdc++.h>
using namespace std;
template<class T> inline bool cmax(T &x, T y) { if (x < y) return x = y, true; return false; }
template<class T> inline bool cmin(T &x, T y) { if (x > y) return x = y, true; return false; }
constexpr int N = 1009;
int n, a[N], b[N], c[N], f[N][N], g[N][N];
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n;
for (int i = 1; i <= n; ++i)
cin >> a[i] >> b[i] >> c[i];
for (int i = 1; i <= n; ++i) {
memcpy(f[i], f[i - 1], sizeof f[i]);
for (int j = 1, p = 1; j <= c[i]; j += p, p = min(p << 1, c[i] - j + 1)) {
for (int k = N - 1; k >= p * a[i]; --k)
cmax(f[i][k], f[i][k - p * a[i]] + p * b[i]);
}
}
for (int i = n; i >= 1; --i) {
memcpy(g[i], g[i + 1], sizeof g[i]);
for (int j = 1, p = 1; j <= c[i]; j += p, p = min(p << 1, c[i] - j + 1)) {
for (int k = N - 1; k >= p * a[i]; --k)
cmax(g[i][k], g[i][k - p * a[i]] + p * b[i]);
}
}
int q;
cin >> q;
while (q--) {
int d, e;
cin >> d >> e, ++d;
int ans = 0;
for (int i = 0; i <= e; ++i)
cmax(ans, f[d - 1][i] + g[d + 1][e - i]);
cout << ans << '\n';
}
}P1772
点数很少,考虑状压。
首先,一共只有 种码头状态,我们用 的时间把所有情况下 最短路求出来,也就是这种状态下的答案 。
然后预处理出 天有哪些码头是可以用的,然后开始 dp。
表示第 天,码头状态为 的答案, 表示第 天的答案。显然 ,转移可以选择不改变,;也可以选择上一次的最小答案 ,这里不用管 是否和 一样,如果一样会被前一半转移到。
。
#include <bits/stdc++.h>
using namespace std;
template<class T> inline bool cmax(T &x, T y) { if (x < y) return x = y, true; return false; }
template<class T> inline bool cmin(T &x, T y) { if (x > y) return x = y, true; return false; }
constexpr int N = 109;
int n, m, k, e, been[N], dst[29], val[1 << 20], f[N], g[1 << 20], h[1 << 20];
vector<pair<int, int>> es[N];
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n >> m >> k >> e;
for (int i = 1; i <= e; ++i) {
int x, y, z;
cin >> x >> y >> z;
--x, --y;
es[x].emplace_back(y, z), es[y].emplace_back(x, z);
}
for (int i = 1; i <= n; ++i) been[i] = (1 << m) - 1;
int d;
cin >> d;
for (int i = 1; i <= d; ++i) {
int a, b, p;
cin >> p >> a >> b;
for (int j = a; j <= b; ++j) been[j] ^= 1 << (p - 1);
}
for (int i = 0; i < 1 << m; ++i) {
priority_queue<pair<int, int>> q;
memset(dst, 0x3f, sizeof dst);
dst[0] = 0;
q.push({0, 0});
while (!q.empty()) {
auto [d, x] = q.top();
d = -d, q.pop();
if (dst[x] != d || (i >> x & 1 ^ 1)) continue;
for (auto [y, z] : es[x]) {
if (dst[x] + z < dst[y]) {
dst[y] = dst[x] + z;
q.push({-dst[y], y});
}
}
}
val[i] = dst[m - 1];
}
memset(f, 0x3f, sizeof f);
memset(g, 0x3f, sizeof g);
memset(h, 0, sizeof h);
f[0] = 0;
for (int i = 1; i <= n; ++i) {
for (int j = been[i]; j; j = been[i] & (j - 1)) {
cmin(g[j], min(h[j], f[i - 1] + k) + val[j]);
cmin(f[i], g[j]);
}
memcpy(h, g, sizeof g);
memset(g, 0x3f, sizeof g);
}
cout << f[n];
}P2518
水题,直接 的出现次数压成状态,最多有 种,于是直接数位 dp。
#include <bits/stdc++.h>
using namespace std;
template<class T> inline bool cmax(T &x, T y) { if (x < y) return x = y, true; return false; }
template<class T> inline bool cmin(T &x, T y) { if (x > y) return x = y, true; return false; }
#define int long long
constexpr int N = 59;
int len;
array<int, 10> cnt;
string n;
map<array<int, 10>, int> dp[N];
int dfs(int p, bool top) {
if (p > len) return true;
if (!top && dp[p].count(cnt)) return dp[p][cnt];
int res = 0, up = top ? n[p] - '0' : 9;
for (int i = 0; i <= up; ++i) {
if (cnt[i] == 0) continue;
--cnt[i];
res += dfs(p + 1, top && i == up);
++cnt[i];
}
if (!top) dp[p][cnt] = res;
return res;
}
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n, len = n.size(), n = ' ' + n;
for (int i = 1; i <= len; ++i) ++cnt[n[i] - '0'];
cout << dfs(1, true) - 1;
}P1800
显然答案具有单调性,考虑二分答案,然后我们 dp,令 表示前 个人,软件 做了 个,做多可以做多少软件 。
。
#include <bits/stdc++.h>
using namespace std;
template<class T> inline bool cmax(T &x, T y) { if (x < y) return x = y, true; return false; }
template<class T> inline bool cmin(T &x, T y) { if (x > y) return x = y, true; return false; }
constexpr int N = 109;
int n, m, d1[N], d2[N], f[N];
bool chk(int x) {
memset(f, -0x3f, sizeof f);
f[0] = 0;
for (int i = 1; i <= n; ++i) {
for (int k = m; k >= 0; --k) {
for (int j = 0; j <= k && j * d1[i] <= x; ++j) {
cmax(f[k], f[k - j] + (x - j * d1[i]) / d2[i]);
}
}
}
return f[m] >= m;
}
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n >> m;
for (int i = 1; i <= n; ++i) cin >> d1[i] >> d2[i];
int l = 1, r = 1e5;
while (l < r) {
int mid = (l + r) >> 1;
if (chk(mid)) r = mid;
else l = mid + 1;
}
cout << l;
}CF1132F
就是 P4170 涂色。
UVA1437
P4170 加强。
把空串染色是好做的,而非空串我们肯定是一个一个区间的染。
于是可以 dp, 表示染了 的前 个,, 就是把 当空串的染色次数。
。
#include <bits/stdc++.h>
using namespace std;
template<class T> inline bool cmax(T &x, T y) { if (x < y) return x = y, true; return false; }
template<class T> inline bool cmin(T &x, T y) { if (x > y) return x = y, true; return false; }
constexpr int N = 109;
int f[N][N], g[N];
string s, t;
int dfs(int l, int r) {
if (l == r) return 1;
if (f[l][r] < 0x3f3f3f3f) return f[l][r];
auto &res = f[l][r];
for (int i = l; i < r; ++i)
cmin(res, dfs(l, i) + dfs(i + 1, r));
if (t[l] == t[r]) cmin(res, min(dfs(l + 1, r), dfs(l, r - 1)));
return res;
}
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
while (cin >> s >> t) {
int n = s.size();
s = ' ' + s, t = ' ' + t;
memset(f, 0x3f, sizeof f);
memset(g, 0x3f, sizeof g);
g[0] = 0;
for (int i = 1; i <= n; ++i) {
g[i] = g[i - 1] + (s[i] != t[i]);
for (int k = 0; k < i; ++k) {
cmin(g[i], g[k] + dfs(k + 1, i));
}
}
cout << g[n] << '\n';
}
}CF280C
期望考虑拆贡献。
首先每个点最多被选择一次,记 表示 是否被选,那么答案即为 ,而 即为 被选的概率。
考虑把所有选择构成一个排列 ,每次相当于从中选择第一个没有删去的。
那么显然 当且仅当其祖先晚于其被选择,即在 中排名大于 。
由于除此之外的点没有限制,所以我们只考虑 和其祖先共 个点,那么 排在第一个的概率即为 。
答案即为 。
#include <bits/stdc++.h>
using namespace std;
template<class T> inline bool cmax(T &x, T y) { if (x < y) return x = y, true; return false; }
template<class T> inline bool cmin(T &x, T y) { if (x > y) return x = y, true; return false; }
constexpr int N = 1e5 + 9;
#define d64 long double
d64 ans;
int n, dep[N];
vector<int> es[N];
void dfs(int x, int ff) {
dep[x] = dep[ff] + 1;
ans += 1. / dep[x];
for (int y : es[x])
if (y ^ ff) dfs(y, x);
}
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n;
for (int i = 1, x, y; i < n; ++i)
cin >> x >> y, es[x].push_back(y), es[y].push_back(x);
dfs(1, 0);
cout << fixed << setprecision(20) << ans;
}CF547C
模拟赛场切了。
显然我们可以每次加入或删除时维护这个数与其它数的贡献,于是问题就变成求一个集合中与某个数 互质的数。
然后这个东西显然可以容斥,我们考虑枚举 的质因子的子集。由于值域很小 ,所以本质不同的质因子个数 ,这就意味着我们可以 次枚举子集做容斥。因为我们没有管一个质因子的指数,所以要预处理每个数的倍数个数。
但是这个做法细节多,比较唐,我们发现 ,于是每次枚举 的因子,然后后面的用桶存一下即可。
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pc(x) __builtin_popcount(x)
constexpr int N = 2e5 + 9, V = 5e5 + 9;
int n, q, c, a[N], pr[N], mn[V], cnt[V], ans, num;
bool ins[N];
vector<int> p[N], e[N];
void add(int x) {
const int sz = p[x].size();
int res = num;
for (int i = 1; i < 1 << sz; ++i) {
int ret = 1;
for (int j = 0; j < sz; ++j) {
if (i >> j & 1) ret *= p[x][j];
}
res += (pc(i) & 1 ? -1 : 1) * cnt[ret];
}
++num, ans += res;
for (int i : e[x]) ++cnt[i];
}
void del(int x) {
const int sz = p[x].size();
for (int i : e[x]) --cnt[i];
int res = --num;
for (int i = 1; i < 1 << sz; ++i) {
int ret = 1;
for (int j = 0; j < sz; ++j) {
if (i >> j & 1) ret *= p[x][j];
}
res += (pc(i) & 1 ? -1 : 1) * cnt[ret];
}
ans -= res;
}
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n >> q;
for (int i = 2; i < V; ++i) {
if (!mn[i]) {
pr[++c] = mn[i] = i;
for (int j = i + i; j < V; j += i)
if (!mn[j]) mn[j] = i;
}
}
for (int i = 1; i <= n; ++i) {
cin >> a[i];
int u = a[i];
while (u > 1) {
int t = mn[u];
p[i].push_back(t);
while (u % t == 0) u /= t;
}
for (int j = 1; j * j <= a[i]; ++j) {
if (a[i] % j == 0) {
e[i].push_back(j);
if (j * j != a[i]) e[i].push_back(a[i] / j);
}
}
}
while (q--) {
int x;
cin >> x;
if (ins[x]) del(x);
else add(x);
ins[x] ^= 1;
cout << ans << '\n';
}
}P3008
模拟赛题,场切了。
由于保证了不会通过负权边形成环,所以将正权边连的所成一个点后,图是一个 DAG,这样我们可以拓扑排序求 DAG 最短路,每个点内部用 dijkstra 跑。
具体地,我们在拓扑排序过程中存一个点集 ,表示所有通过负权边可达的点,然后以这个点集为起点跑多源最短路,并且更新其所有出边的连通块的点集,细节比较多。
。
#include <bits/stdc++.h>
using namespace std;
#define int long long
constexpr int N = 2.5e4 + 9;
int n, r, p, s, m, dst[N], bel[N], ind[N], ans[N];
vector<int> blk[N];
vector<pair<int, int>> es[N];
vector<tuple<int, int, int, int>> e[N];
void dij(int bl, const vector<pair<int, int>> &st) {
priority_queue<pair<int, int>> q;
// for (int i : blk[bl]) dst[i] = 0x3f3f3f3f3f3f3f3f;
for (auto [x, y] : st) dst[x] = min(dst[x], y);
for (auto [x, y] : st)
if (dst[x] == y) q.emplace(-dst[x], x);
while (!q.empty()) {
auto [d, x] = q.top();
q.pop(), d = -d;
if (d != dst[x]) continue;
for (auto [y, z] : es[x])
if (dst[y] > dst[x] + z) {
dst[y] = dst[x] + z;
q.emplace(-dst[y], y);
}
}
}
void dfs(int x, int c) {
bel[x] = c, blk[c].push_back(x);
for (auto [y, z] : es[x])
if (!bel[y]) dfs(y, c);
}
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n >> r >> p >> s;
for (int i = 1; i <= r; ++i) {
int x, y, z;
cin >> x >> y >> z;
es[x].emplace_back(y, z);
es[y].emplace_back(x, z);
}
for (int i = 1; i <= n; ++i) {
if (!bel[i]) dfs(i, ++m);
// cerr << bel[i] << ' ';
}
// cerr << '\n';
for (int i = 1; i <= p; ++i) {
int x, y, z;
cin >> x >> y >> z;
e[bel[x]].emplace_back(bel[y], y, x, z);
++ind[bel[y]];
}
memset(dst, 0x3f, sizeof dst);
map<int, vector<pair<int, int>>> mp;
mp[bel[s]].emplace_back(s, 0);
queue<int> q;
for (int i = 1; i <= m; ++i)
if (!ind[i]) q.push(i);
while (!q.empty()) {
int x = q.front();
q.pop();
auto st = mp[x];
dij(x, st);
// cerr << x << ' ' << dst[1] << '\n';
for (auto [y1, y2, x1, z] : e[x]) {
mp[y1].emplace_back(y2, dst[x1] + z);
if (!--ind[y1]) q.push(y1);
}
}
for (int i = 1; i <= n; ++i) {
if (dst[i] > 2e9) cout << "NO PATH" << '\n';
else cout << dst[i] << '\n';
}
}P7098
被放在了普及组模拟赛 T4,不过场切了。
考虑状压,令 表示当前放第 层,已经放了 集合中的路线的最小花费。
需要一些比较麻烦的预处理,写的不太好有点卡常。
。
#include <bits/stdc++.h>
using namespace std;
template<class T> inline bool cmax(T &x, T y) { if (x < y) return x = y, true; return false; }
template<class T> inline bool cmin(T &x, T y) { if (x > y) return x = y, true; return false; }
#define pc(x) __builtin_popcount(x)
constexpr int N = 29, M = 1e5 + 9;
int n, m, a[N][M];
long long dp[N][1 << 14], val[1 << 14][N], Val[N][N];
bool ok[1 << 14], can[N][N];
vector<int> ru[N];
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n >> m;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) cin >> a[i][j];
}
for (int i = 0; i < n; ++i) {
int sz;
cin >> sz;
ru[i].resize(sz);
for (int j = 0; j < sz; ++j) {
cin >> ru[i][j], --ru[i][j];
for (int k = 0; k < n; ++k) {
Val[i][k] += a[k][ru[i][j]];
}
}
}
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j) can[i][j] = 1;
for (int i = 0; i < n; ++i) {
map<int, int> mp;
for (int k : ru[i]) mp[k] = 1;
for (int j = 0; j < n; ++j) {
if (i == j) continue;
for (int k : ru[j]) {
if (mp[k]) {
can[i][j] = 0;
break;
}
}
}
}
for (int i = 0; i < 1 << n; ++i) {
ok[i] = 1;
vector<int> vec;
for (int j = 0; j < n; ++j) {
if (i >> j & 1) {
vec.push_back(j);
for (int k = 0; k < n; ++k) val[i][k + 1] += Val[j][k];
}
}
// cerr << vec.size() << '\n';
for (auto j : vec) {
if (!ok[i]) break;
for (auto k : vec) {
// cerr << i << ' ' << k << ' ' << can[i][k] << '\n';
if (!can[j][k]) ok[i] = 0;
if (!ok[i]) break;
}
}
// cerr << i << ' ' << ok[i] << '\n';
}
// cerr << ok[1 << 1];
memset(dp, 0x3f, sizeof dp);
dp[0][0] = 0;
for (int i = 0; i < 1 << n; ++i) {
for (int j = 1; j <= n; ++j) {
if ()
cmin(dp[j][i], dp[j - 1][i]);
for (int k = i; k; k = (k - 1) & i) {
if (!ok[k]) continue;
cmin(dp[j][i], dp[j - 1][i ^ k] + val[k][j]);
}
}
}
long long ans = 0x3f3f3f3f3f3f3f3f;
for (int i = 0; i <= n; ++i) cmin(ans, dp[i][(1 << n) - 1]);
cout << ans;
}CF2150C
考虑 dp,令 表示已经选了 ,现在考虑 的最大价值。
首先考虑 能不能选,令 ,则 能选当且仅当 。
于是若 ,。
若 ,,也就是考虑上一次 选的是什么。这样可能 并没有都被 选了,不过这样没关系,因为我们取最大所以这种不优的情况最后会被舍掉。
若 ,。
于是直接线段树维护即可。
#include <bits/stdc++.h>
using namespace std;
#define int long long
constexpr int N = 2e5 + 9;
int n, v[N], a[N], b[N], pb[N], p[N], sgt[N << 2], tg[N << 2];
void build(int u, int l, int r) {
sgt[u] = tg[u] = 0;
if (l == r) return ;
int m = (l + r) >> 1;
build(u << 1, l, m), build(u << 1 | 1, m + 1, r);
}
void apply(int u, int z) { sgt[u] += z, tg[u] += z; }
void down(int u) {
if (tg[u]) apply(u << 1, tg[u]), apply(u << 1 | 1, tg[u]), tg[u] = 0;
}
void update1(int u, int l, int r, int x, int y, int z) {
if (x <= l && r <= y) return apply(u, z);
int m = (l + r) >> 1;
down(u);
if (m >= x) update1(u << 1, l, m, x, y, z);
if (m < y) update1(u << 1 | 1, m + 1, r, x, y, z);
sgt[u] = max(sgt[u << 1], sgt[u << 1 | 1]);
}
void update2(int u, int l, int r, int x, int y) {
if (l == r) return sgt[u] = y, void();
int m = (l + r) >> 1;
down(u);
if (m >= x) update2(u << 1, l, m, x, y);
else update2(u << 1 | 1, m + 1, r, x, y);
sgt[u] = max(sgt[u << 1], sgt[u << 1 | 1]);
}
int query(int u, int l, int r, int x, int y) {
if (x <= l && r <= y) return sgt[u];
int m = (l + r) >> 1;
down(u);
if (m >= x && m < y) return max(query(u << 1, l, m, x, y), query(u << 1 | 1, m + 1, r, x, y));
else if (m >= x) return query(u << 1, l, m, x, y);
else return query(u << 1 | 1, m + 1, r, x, y);
}
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
int tt;
cin >> tt;
while (tt--) {
cin >> n;
for (int i = 1; i <= n; ++i) cin >> v[i];
for (int i = 1; i <= n; ++i) cin >> a[i];
for (int i = 1; i <= n; ++i) cin >> b[i], pb[b[i]] = i;
for (int i = 1; i <= n; ++i) p[i] = pb[a[i]];
build(1, 0, n);
for (int i = 1; i <= n; ++i) {
update2(1, 0, n, p[i], query(1, 0, n, 0, p[i]));
update1(1, 0, n, 0, p[i] - 1, v[a[i]]);
}
cout << query(1, 0, n, 0, n) << '\n';
}
}CF1704E
首先不能直接拓扑排序,因为有的点一开始是 ,但是后来某个时刻可能又有值了,所以不能直接拓扑。
我们考虑这种点什么时候又有值了,显然对于两个点 ,如果 ,那么从 时刻开始, 中的值就到 了, 最大为 ,所以从 时刻开始,这种点就不存在了。
于是我们直接模拟前 个时刻,然后开始拓扑排序。
。
#include <bits/stdc++.h>
using namespace std;
constexpr int N = 1009, md = 998244353;
int n, m, a[N], d[N], o[N], c;
vector<int> es[N];
queue<int> q;
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
int tt;
cin >> tt;
while (tt--) {
cin >> n >> m, c = 0;
for (int i = 1; i <= n; ++i) cin >> a[i], d[i] = 0, es[i].clear();
for (int i = 1, x, y; i <= m; ++i) {
cin >> x >> y, es[x].push_back(y), ++d[y];
}
for (int i = 1; i <= n; ++i)
if (!d[i]) q.push(i);
while (!q.empty()) {
int x = q.front();
q.pop(), o[++c] = x;
for (int y : es[x])
if (!--d[y]) q.push(y);
}
bool ok = false;
for (int k = 0; k < n; ++k) {
if (count(a + 1, a + n + 1, 0) == n) {
cout << k << '\n';
ok = true;
break;
}
vector<int> p;
for (int i = 1; i <= n; ++i)
if (a[i] > 0) p.push_back(i);
for (int i : p) {
--a[i];
for (int y : es[i]) ++a[y];
}
}
for (int i = 1; i <= c; ++i) {
for (int y : es[o[i]]) (a[y] += a[o[i]]) %= md;
}
if (!ok) cout << (a[o[c]] + n) % md << '\n';
}
}CF1706D2
考虑枚举最小值,然后对于每个数,,而 。而 可以分为许多连续段,每段均相同,我们可以枚举每一段,也就是此时 的值域,然后用二分得到这一段的最大值。
。
#include <bits/stdc++.h>
using namespace std;
constexpr int N = 1e5 + 9;
int n, k, a[N];
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
int tt;
cin >> tt;
while (tt--) {
cin >> n >> k;
for (int i = 1; i <= n; ++i) cin >> a[i];
n = unique(a + 1, a + n + 1) - a - 1;
int mx = 0, mn = 0x3f3f3f3f;
for (int i = 1; i <= n; ++i) mx = max(mx, a[i] / k), mn = min(mn, a[i] / k);
int ans = mx - mn;
for (int i = 1; i <= a[1]; ++i) {
mx = a[n] / k;
for (int j = 1; j <= k && j <= a[n] / i; ++j) {
mx = max(mx, a[lower_bound(a + 1, a + n + 1, (j + 1) * i) - a - 1] / j);
}
ans = min(ans, mx - i);
}
cout << ans << '\n';
}
}