23.7.25 杭电暑期多校3部分题解

1005 - Out of Control

题目大意

你有一个长度为 n n n 的序列,你每次可以向一个栈内放一个数,如果栈不为空并且栈顶的数大于你放的数,那么你放的数会变成栈顶的数再放入,问当栈的大小为 1 1 1 到 n n n 时分别有几种情况

解题思路

考虑dp, f i , j f_{i,j} fi,j 表示放了 i i i 个数,最大的数为 j j j 的方案数

通过离散化处理,记录原数的大小 a i a_i ai 和 数量 b i b_i bi,这样 j j j 只要枚举下标即可

初始化显然 f 1 , j = 1 f_{1,j}=1 f1,j=1

对于 f i , j f_{i,j} fi,j,它可以从 f i − 1 , 1 f_{i-1,1} fi−1,1 到 f i − 1 , j f_{i-1,j} fi−1,j 转移而来

f i , j = ∑ k = 1 j f i , k f_{i,j}=\sum_{k=1}^jf_{i,k} fi,j=∑k=1jfi,k,前提条件是存在可填的数,即 ∑ k = 1 j b [ k ] ≥ i \sum_{k=1}^jb[k]\ge i ∑k=1jb[k]≥i

只要顺带处理一下前缀和就可以啦

code

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
const int N = 3e3 + 9;
const int MOD = 1e9 + 7;
int t, n, x[N], a[N], b[N], num, lst;
long long f[N][N];
int main() {
    scanf("%d", &t);
    while (t --) {
        scanf("%d", &n);
        for (int i = 1; i <= n; ++ i) scanf("%d", &x[i]);
        sort(x + 1, x + n + 1);
        num = lst = 0;
        for (int i = 1; i <= n; ++ i)
            if (x[i] != x[i - 1])
                b[num] = i - lst, lst = i, a[++ num] = x[i];
        b[num] = n - lst + 1;
        for (int i = 1; i <= num; ++ i) b[i] += b[i - 1];
        for (int i = 1; i <= n; ++ i) {
            for (int j = 1; j <= num; ++ j) {
                if (i == 1) f[i][j] = 1;
                else if (b[j] > i) f[i][j] = f[i - 1][j];
            }
            for (int j = 1; j <= num; ++ j) {
                f[i][j] = (f[i][j] + f[i][j - 1]) % MOD;
            }
        }
        for (int i = 1; i <= n; ++ i) {
            printf("%lld\n", f[i][num]);
        }
        for (int i = 1; i <= n; ++ i)
            for (int j = 1; j <= num; ++ j)
                f[i][j] = 0;
    }
    return 0;
}

1009 - Operation Hope

题意、思路待补

code

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 9;
struct lol {int x, id;} e[3][N * 2];
int t, n, a[3][N * 2], hd[3], tl[3], vis[N * 2], q[N * 2], num, f[N * 2], ans;
bool cmp(lol a, lol b) {return a.x < b.x;}
int getx(int x, int p, int k) {
    if (hd[k] <= tl[k]) {
        int y = e[k][hd[k]].x, id = e[k][hd[k]].id;
        if (y < x - p) {++ hd[k]; return id;}
        y = e[k][tl[k]].x; id = e[k][tl[k]].id;
        if (y > x + p) {-- tl[k]; return id;}
    }
    return 0;
}
void dfs(int x, int p) {
    vis[x] = 1;
    for (int k = 0; k < 3; ++ k) while (1) {
        int y = getx(a[k][x], p, k);
        if (!y) break;
        if (y <= n) if (!vis[y + n]) dfs(y + n, p); else;
        else if (!vis[y - n]) dfs(y - n, p); else;
    }
    q[++ num] = x;
}
void dfs1(int x, int p) {
    vis[x] = 0; f[x] = ans;
    for (int k = 0; k < 3; ++ k) while (1) {
        int y = getx(a[k][x <= n ? x + n : x - n], p, k);
        if (!y) break;
        if (vis[y]) dfs1(y, p);
    }
}
int chk(int p) {
    num = ans = 0;
    for (int i = 1; i <= 2 * n; ++ i) vis[i] = 0;
    for (int k = 0; k < 3; ++ k) hd[k] = 1, tl[k] = 2 * n;
    for (int k = 0; k < 3; ++ k)
        for (int i = 1; i <= 2 * n; ++ i)
            if (!vis[i]) dfs(i, p);
    for (int k = 0; k < 3; ++ k) hd[k] = 1, tl[k] = 2 * n;
    for (int k = 0; k < 3; ++ k)
        for (int i = num; i >= 1; -- i)
            if (vis[q[i]]) ++ ans, dfs1(q[i], p);
    for (int i = 1; i <= n; ++ i) if (f[i] == f[i + n]) return 0;
    return 1;
}
int main() {
    scanf("%d", &t);
    while (t --) {
        scanf("%d", &n);
        for (int i = 1; i <= n; ++ i) {
            for (int j = 0; j < 3; ++ j)
                scanf("%d", &a[j][i]),
                e[j][i].x = a[j][i],
                e[j][i].id = i;
            for (int j = 0; j < 3; ++ j)
                scanf("%d", &a[j][i + n]),
                e[j][i + n].x = a[j][i + n],
                e[j][i + n].id = i + n;
        }
        for (int k = 0; k < 3; ++ k)
            sort(e[k] + 1, e[k] + 2 * n + 1, cmp);
        int l = 0, r = 1e9;
        while (l <= r) {
            int mid = (l + r) >> 1;
            if (chk(mid)) r = mid - 1;
            else l = mid + 1;
        }
        printf("%d\n", l);
    }
    return 0;
 }
相关推荐
lunch( ̄︶ ̄)5 天前
B3628 机器猫斗恶龙
数据结构·c++·算法·二分
王老师青少年编程13 天前
CSP/信奥赛C++刷题训练:经典二分例题(2):洛谷P1678:烦恼的高考志愿
c++·算法·青少年编程·二分·csp·信奥赛
dengqingrui1231 个月前
【树形DP】AT_dp_p Independent Set 题解
c++·学习·算法·深度优先·图论·dp
Tisfy1 个月前
LeetCode 2187.完成旅途的最少时间:二分查找
算法·leetcode·二分查找·题解·二分
疯狂的小强呀1 个月前
超详细讲解:DP和DDP的区别以及使用方法
算法·dp·ddp
Jcqsunny1 个月前
[dp] 小信走迷宫
算法·前缀和·动态规划·dp
源代码•宸2 个月前
Leetcode—322. 零钱兑换【中等】(memset(dp,0x3f, sizeof(dp))
c++·算法·leetcode·职场和发展·dp
mikey棒棒棒2 个月前
算法练习题18——leetcode240搜索二维矩阵||(二分)
java·线性代数·算法·leetcode·矩阵·二分
Qres8212 个月前
[SCOI2014] 方伯伯的玉米田(dp+树状数组维护行列)
数据结构·dp·ds
源代码•宸2 个月前
Leetcode—72. 编辑距离【中等】
c++·算法·leetcode·dp