前言
今天做的是是2010年提高组和NOI的题目,做过几道原题,但是还是爆炸了,我真的太弱了。
t1-乌龟棋
https://www.luogu.org/problemnew/show/P1541
这道题目还是比较简单的,差不多是三年做过的题目。
简单的DP,定义状态是\(f[i][j][k][l]\)表示i个1的卡牌,j个2的卡牌,k个3的卡牌,l个4的卡牌时的最大分数。
O(n^4)的暴力转移
\[f[i][j][k][l]=max(f[i-1][j][k][l]+a[tmp])\]
\[f[i][j][k][l]=max(f[i][j-1][k][l]+a[tmp])\]
\[f[i][j][k][l]=max(f[i][j][k-1][l]+a[tmp])\]
\[f[i][j][k][l]=max(f[i][j][k][l-1]+a[tmp])\]
其中tmp表示的是当前的位置,因为我们已知用了各个卡牌有多少张,那么就可以直接推出当前的位置。
#include <bits/stdc++.h>
#define ll long long
#define ms(a, b) memset(a, b, sizeof(a))
#define inf 0x3f3f3f3f
#define N
using namespace std;
template <typename T>
inline void read(T &x) {
x = 0; T fl = 1;
char ch = 0;
while (ch < '0' || ch > '9') {
if (ch == '-') fl = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 1) + (x << 3) + (ch ^ 48);
ch = getchar();
}
x *= fl;
}
int f[45][45][45][45];
int n, m;
int a[355], b[10];
int main() {
read(n); read(m);
memset(f, 0, sizeof(f));
memset(b, 0, sizeof(b));
for (int i = 1; i <= n; i ++) read(a[i]);
for (int i = 1; i <= m; i ++) {
int x; read(x);
b[x] ++;
}
f[0][0][0][0] = a[1];
for (int i = 0; i <= b[1]; i ++) {
for (int j = 0; j <= b[2]; j ++) {
for (int k = 0; k <= b[3]; k ++) {
for (int l = 0; l <= b[4]; l ++) {
int tmp = i + j * 2 + k * 3 + l * 4 + 1;
if (i) f[i][j][k][l] = max(f[i][j][k][l], f[i - 1][j][k][l] + a[tmp]);
if (j) f[i][j][k][l] = max(f[i][j][k][l], f[i][j - 1][k][l] + a[tmp]);
if (k) f[i][j][k][l] = max(f[i][j][k][l], f[i][j][k - 1][l] + a[tmp]);
if (l) f[i][j][k][l] = max(f[i][j][k][l], f[i][j][k][l - 1] + a[tmp]);
}
}
}
}
printf("%d\n", f[b[1]][b[2]][b[3]][b[4]]);
return 0;
}
t2-关押罪犯
https://www.luogu.org/problemnew/show/P1525
并查集裸题,我们设立两个集合分别表示当前的罪犯和及其补集,也就是判断他们能不能在一起。
考试的时候差一点就写炸了,因为在结构体里面重定义运算符好像会炸,应该是我太弱了,所以就打了一个不怎么美观的cmp。 ̄□ ̄||。但是还是只有90分,因为我电脑死机,重启之后重构代码,结尾忘掉输出0了。
#include <bits/stdc++.h>
#define ll long long
#define ms(a, b) memset(a, b, sizeof(a))
#define inf 0x3f3f3f3f
#define N 100005
using namespace std;
template <typename T>
inline void read(T &x) {
x = 0; T fl = 1;
char ch = 0;
while (ch < '0' || ch > '9') {
if (ch == '-') fl = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 1) + (x << 3) + (ch ^ 48);
ch = getchar();
}
x *= fl;
}
struct edge {
int u, v, dis;
}a[N];
int fa[N];
int n, m;
bool cmp(edge a, edge b) {
return a.dis > b.dis;
}
int gf(int x) {
return fa[x] == x ? fa[x] : fa[x] = gf(fa[x]);
}
int main() {
freopen("*.in","r",stdin);
freopen("*.out","w",stdout);
read(n); read(m);
for (int i = 1; i <= m; i ++) read(a[i].u), read(a[i].v), read(a[i].dis);
for (int i = 1; i <= 2 * n; i ++) fa[i] = i;
sort(a + 1, a + 1 + m, cmp);
for (int i = 1; i <= m; i ++) {
int p1 = gf(a[i].u), p2 = gf(a[i].v);
if (p1 == p2) {
printf("%d\n", a[i].dis);
return 0;
}
fa[p1] = gf(a[i].v + n);
fa[p2] = gf(a[i].u + n);
}
return 0;
}
t4-引水入城
https://www.luogu.org/problemnew/show/P1514
一道简单的搜索题+DP,但是我忘记输出了不能有的城市的个数,直接爆炸成70分。
我们在搜索的时候,记录最后一行线段的端点,然后就做一些线段覆盖的DP就好了。
#include <bits/stdc++.h>
#define ll long long
#define ms(a, b) memset(a, b, sizeof(a))
#define inf 0x3f3f3f3f
using namespace std;
template <typename T>
inline void read(T &x) {
x = 0; T fl = 1;
char ch = 0;
while (ch < '0' || ch > '9') {
if (ch == '-') fl = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 1) + (x << 3) + (ch ^ 48);
ch = getchar();
}
x *= fl;
}
struct data {
int l, r;
bool operator <(const data &rhs) const {
return l < rhs.l;
}
}a[505];
int n, m, mp[505][505], f[505], cnt = 0;
bool vis[505][505] = {0}, ans[505] = {0};
void dfs(int x, int y, ll kkk) {
vis[x][y] = 1;
if (x == m) {
ans[y] = 1;
a[kkk].l = min(a[kkk].l, y);
a[kkk].r = max(a[kkk].r, y);
}
if (mp[x + 1][y] < mp[x][y] && x != m && !vis[x + 1][y]) dfs(x + 1, y, kkk);
if (mp[x - 1][y] < mp[x][y] && x != 1 && !vis[x - 1][y]) dfs(x - 1, y, kkk);
if (mp[x][y + 1] < mp[x][y] && y != n && !vis[x][y + 1]) dfs(x, y + 1, kkk);
if (mp[x][y - 1] < mp[x][y] && y != 1 && !vis[x][y - 1]) dfs(x, y - 1, kkk);
}
int main() {
read(m); read(n);
for (int i = 1; i <= n; i ++) a[i].l = f[i] = inf;
f[0] = 0;
for (int i = 1; i <= m; i ++) {
for (int j = 1; j <= n; j ++)
read(mp[i][j]);
}
for (int i = 1; i <= n; i ++) {
dfs(1, i, i);
memset(vis, 0, sizeof(vis));
}
for (int i = 1; i <= n; i ++) {
if (!ans[i]) cnt ++;
}
if (cnt) {
printf("0\n%d\n", cnt);
return 0;
}
else {
printf("1\n");
for (int i = 1; i <= n; i ++) {
for (int j = 1; j <= n; j ++) {
if (i >= a[j].l && i <= a[j].r) f[i] = min(f[i], f[a[j].l - 1] + 1);
}
}
printf("%d\n", f[n]);
}
return 0;
}
t4-超级钢琴
这道题目之前做过但是没有A掉,考场上面自己YY乱搞记得正解怎么搞,也不知道怎么回事就做出来了???
简要讲一下
先用前缀和处理数据,用st表预处理区间最小值,然后构建一个大根堆,队里存储左边界的范围(-1),右边界,以此为右边界的最大答案,最大答案时的左边界(-1),每次取出都把左边界的范围拆分成2份,维护一下就好了。
#include <bits/stdc++.h>
#define ll long long
#define ms(a, b) memset(a, b, sizeof(a))
#define inf 0x3f3f3f3f
#define pii pair<int, int>
#define Mp(a,b,c,d) make_pair(make_pair(a,b),make_pair(c,d))
#define N 500005
using namespace std;
template <typename T>
inline void read(T &x) {
x = 0; T fl = 1;
char ch = 0;
while (ch < '0' || ch > '9') {
if (ch == '-') fl = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 1) + (x << 3) + (ch ^ 48);
ch = getchar();
}
x *= fl;
}
struct ST {
int n;
int f[N][24], Log[N], a[N];
ST() {
memset(f, 0, sizeof(f));
}
void init(int M, int *A) {
n = M;
for (int i = 1; i <= n; i ++) a[i] = A[i];
}
int mn(int l, int r) {
return a[l] < a[r] ? l : r;
}
void make_ST() {
for (int i = 2; i <= n; i ++) Log[i] = Log[i >> 1] + 1;
for (int i = 1; i <= n; i ++) f[i][0] = i;
for (int j = 1; (1 << j) <= n; j ++) {
for (int i = 0; i + (1 << j) - 1 <= n; i ++) {
f[i][j] = mn(f[i][j - 1], f[i + (1 << j - 1)][j - 1]);
}
}
}
int query(int l, int r) {
if (l > r) return -1;
int k = Log[r - l + 1];
return mn(f[l][k], f[r - (1 << k) +1][k]);
}
}ST;
priority_queue<pair<pii,pii> >q;
int n, m, L, R;
ll ans = 0;
int x, y, a, b, c, d;
int v[N], sum[N];
int main() {
freopen("piano.in","r",stdin);
freopen("piano.out","w",stdout);
read(n); read(m); read(L); read(R);
for (int i = 1; i <= n; i ++) {
read(v[i]);
sum[i] = sum[i - 1] + v[i];
}
ST.init(n, sum);
ST.make_ST();
for (int i = L; i <= n; i ++) {
q.push(Mp(sum[i] - sum[ST.query(max(i - R, 0), i - L)], i, max(i - R, 0), i - L));
}
for (int i = 1; i <= m; i ++) {
pii t1 = q.top().first, t2 = q.top().second;
ans += t1.first, x = t1.second, a = t2.first, b = t2.second, y = ST.query(a, b);
q.pop();
c = ST.query(a, y - 1); d = ST.query(y + 1, b);
if (c != -1) q.push(Mp(sum[x] - sum[c], x, a, y - 1));
if (d != -1) q.push(Mp(sum[x] - sum[d], x, y + 1, b));
}
printf("%lld\n", ans);
return 0;
}