5.4提高组模拟赛
__DragonFly · · 个人记录
空调 (Air)
dfs 选和不选,选择这个冰箱或不选。
正确代码
#include <bits/stdc++.h>
using namespace std;
const int L = 101;
const int M = 10;
const int inf = 1e9;
struct node {
int l, r, p, m;
} a[M];
int req[L], cool[L];
int n, m, ans = inf;
void dfs(int idx, int cost) {
if (idx == m) {
bool ok = true;
for (int i = 1; i <= 100; ++i) {
if (cool[i] < req[i]) {
ok = false;
break;
}
}
if (ok) ans = min(ans, cost);
return;
}
dfs(idx + 1, cost);
for (int j = a[idx].l; j <= a[idx].r; ++j) cool[j] += a[idx].p;
dfs(idx + 1, cost + a[idx].m);
for (int j = a[idx].l; j <= a[idx].r; ++j) cool[j] -= a[idx].p;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
for (int i = 0; i < n; ++i) {
int l, r, c;
cin >> l >> r >> c;
for (int j = l; j <= r; ++j) req[j] = max(req[j], c);
}
for (int i = 0; i < m; ++i) {
cin >> a[i].l >> a[i].r >> a[i].p >> a[i].m;
}
dfs(0, 0);
cout << ans << '\n';
return 0;
}
P6146 [USACO20FEB] Help Yourself G
题意
相交的线段为一个联通块,求联通块的数量。
思路
将每一线段按照左端点按照从小到大排序。
设已选的线段中,右端点小于当前线段的左端点的有
所以,当前线段的贡献是
以上的数量关系可以用树状数组维护。
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 7;
const int mod = 1e9 + 7;
struct node {
int l, r;
}a[N];
int n, maxm;
int tr[N << 1];
long long pow_2[N];
bool cmp(node q, node h) {
return q.l < h.l;
}
int lowbit(int x) {
return x & (-x);
}
void add(int x, int v) {
while (x <= maxm) {
tr[x] += v;
x += lowbit(x);
}
return ;
}
int sum(int x) {
int res = 0;
while (x) {
res += tr[x];
x -= lowbit(x);
}
return res;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> a[i].l >> a[i].r;
maxm = max(maxm, a[i].r);
}
sort(a + 1, a + 1 + n, cmp);
pow_2[0] = 1;
for (int i = 1; i <= n; ++i) pow_2[i] = pow_2[i - 1] * 2 % mod;
long long ans = 0;
for (int i = 1; i <= n; ++i) {
int t = (sum(maxm) - sum(a[i].l - 1)) + 1;
ans = (ans + pow_2[n - t]) % mod;
add(a[i].r, 1);
}
cout << ans << "\n";
return 0;
}
P6279 [USACO20OPEN] Favorite Colors G
题意
- 给定
n 对关系(a,b) 表示b 仰慕a - 若
a ,b 有共同的仰慕对象,那么他们的最喜欢的颜色必须相同 - 需要给每个分配一个颜色
c(c \le n) ,满足条件的最小字典序的序列。
思路
维护一个扩展并查集:
- 节点
x(1 \le x \le n) 代表牛x 。 - 节点
x + n 代表喜欢颜色x 的仰慕者集合。
对于每一个 merge(b, a + n) 将
当他们都是需要仰慕别人时,即节点
最后,在同一个集合中的人配相同的色。按照牛的编号配色,保证字典序最小。
正确代码
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 7;
int fa[N];
int vis[N], color[N];
int n, m, tot;
int find(int u) {
if (fa[u] == u) return u;
return fa[u] = find(fa[u]);
}
void merge(int x, int y) {
x = find(x), y = find(y);
if (x == y) return ; // 同一个集合
// 保证用较小的根更新
if (x <= n) swap(x, y);
fa[x] = y;
// 二个都是普通人,他们必须同色,同时它们的仰慕者集合也要合并
if (x <= n && y <= n) {
merge(x + n, y + n);
}
return ;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= 2 * n; ++i) fa[i] = i;
for (int i = 1; i <= m; ++i) {
int a, b;
cin >> a >> b;
merge(b, a + n);
}
for (int i = 1; i <= n; ++i) {
int id = find(i);
if (!vis[id]) { // 尚未被染色
vis[id] = ++tot;
}
color[i] = vis[id];
}
for (int i = 1; i <= n; ++i) {
cout << color[i] << "\n";
}
return 0;
}
P5839 [USACO19DEC] Moortal Cowmbat G
题意
给定长度为 i->k->j)。
思路
-
1.任意两两字符替换的最小代价
由于可以通过媒介,可以用 floyd 求出 f[i][j] : 将字符
i 改为j 的最小代价。 -
2.前缀和预处理出代价和
处理
sum[i][c]前i 个字符改为c 的代价和。转移:
sum[i][c]=sum[i-1][c]+f[si][c]利用
sumO(1) 求出任意区间的代价和 -
3.动态规划
-
初始化:
ans[0]=0,dp[c]=1e9 -
对于
i=j到N,递推:1.延续之前的
c :dp_c = dp_c+f_{s_i,c} 2.新开一段全为c 的长度为j 的一段:dp_c = min(dp_c + f_{s_i,c},ans_{i-k}+sum_{ic}-sum_{i-k,c})
-
正确代码
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 7, M = 30;
int f[M][M], sum[N][M], dp[M], ans[N];
char s[N + 1];
int n, m, k;
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> m >> k;
cin >> (s + 1);
for (int i = 0; i < m; ++i) {
for (int j = 0; j < m; ++j) {
cin >> f[i][j];
}
}
// floyd求两两配对的最小代价
for (int k1 = 0; k1 < m; ++k1) {
for (int i = 0; i < m; ++i) {
for (int j = 0; j < m; ++j) {
if (f[i][j] > f[i][k1] + f[k1][j]) f[i][j] = f[i][k1] + f[k1][j];
}
}
}
// 前缀和预处理
for (int i = 1; i <= n; ++i) {
int cur = s[i] - 'a';
for (int c = 0; c < m; ++c) {
sum[i][c] = sum[i - 1][c] + f[cur][c];
}
}
// 初始化
memset(dp, 0x3f, sizeof dp);
memset(ans, 0x3f, sizeof ans);
ans[0] = 0;
for (int i = k; i <= n; ++i) {
int cur = s[i] - 'a';
for (int c = 0; c < m; ++c) {
dp[c] = min(dp[c] + f[cur][c], ans[i - k] + sum[i][c] - sum[i - k][c]);
// 延续c结尾
// 新开一段
ans[i] = min(ans[i], dp[c]); // 更新前i个的最小值
}
}
cout << ans[n];
return 0;
}
P6006 [USACO20JAN] Farmer John Solves 3SUM G
题意
给定长度为
思路
区间
设
那么若
正确代码
#include <bits/stdc++.h>
using namespace std;
const int N = 5005, M = 1e6;
long long f[N][N];
int a[N], cnt[4000005];
int n, q;
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> q;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
for (int i = 1; i <= n; ++i) {
for (int j = i + 1; j <= n; ++j) {
f[i][j] = cnt[-a[i] - a[j] + 2 * M];
cnt[a[j] + 2 * M]++;
}
for (int j = i + 1; j <= n; ++j) {
cnt[a[j] + 2 * M] = 0;
}
}
for (int len = 3; len <= n; ++len) {
for (int l = 1; len - 1 + l <= n; ++l) {
int r = len - 1 + l;
f[l][r] += f[l + 1][r] + f[l][r - 1] - f[l + 1][r - 1];
}
}
while (q--) {
int l, r;
cin >> l >> r;
cout << f[l][r] << "\n";
}
return 0;
}