Codeforces 1579G DP / 二分 + bitset

题意

传送门 Codeforces 1579G Minimal Coverage

题解
DP

d p [ i + 1 ] [ j ] dp[i+1][j] dp[i+1][j] 代表 0 ⋯ i 0\cdots i 0⋯i 次移动后所在位置与覆盖区域最左侧位置相差 j j j 时,覆盖区域的最小值。枚举左右方向递推即可。总时间复杂度 O ( n ⋅ max ⁡ { a i } ) O(n\cdot\max\{a_i\}) O(n⋅max{ai})。

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
constexpr int M = 2E3, INF = 1e9;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int tt;
    cin >> tt;
    while (tt--) {
        int n;
        cin >> n;
        vector<int> a(n);
        for (int i = 0; i < n; ++i) {
            cin >> a[i];
        }
        auto _min = [](int &x, int y) {
            x = min(x, y);
        };
        vector<vector<int>> dp(n + 1, vector<int>(M, INF));
        dp[0][0] = 0;
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < M; ++j) {
                if (dp[i][j] == INF) {
                    continue;
                }
                if (j + a[i] < M) {
                    _min(dp[i + 1][j + a[i]], max(dp[i][j], j + a[i]));
                }
                if (j - a[i] >= 0) {
                    _min(dp[i + 1][j - a[i]], dp[i][j]);
                } else {
                    _min(dp[i + 1][0], dp[i][j] + a[i] - j);
                }
            }
        }
        int res = *min_element(dp[n].begin(), dp[n].end());
        cout << res << '\n';
    }

    return 0;
}
二分 + bitset

二分覆盖区域的大小 d d d。用 std::bitset 维护当前的可能位置,初始位置可能位于 [ 0 , d ) [0,d) [0,d) 中的任一个位置,递推即可。总时间复杂度 O ( n ⋅ max ⁡ { a i } ⋅ log ⁡ n / 32 ) O(n\cdot\max\{a_i\}\cdot\log n/32) O(n⋅max{ai}⋅logn/32)。

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
constexpr int N = 2E3;
using bt = bitset<N>;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int tt;
    cin >> tt;
    while (tt--) {
        int n;
        cin >> n;
        vector<int> a(n);
        for (int i = 0; i < n; ++i) {
            cin >> a[i];
        }
        auto judge = [&](int d) {
            bt x, mask;
            for (int i = 0; i < d; ++i) {
                x[i] = mask[i] = 1;
            }
            for (int i = 0; i < n; ++i) {
                x = ((x << a[i]) | (x >> a[i])) & mask;
            }
            return x.any();
        };
        int lb = 0, ub = N;
        while (ub - lb > 1) {
            int mid = (lb + ub) / 2;
            if (judge(mid)) {
                ub = mid;
            } else {
                lb = mid;
            }
        }
        cout << ub - 1 << '\n';
    }

    return 0;
}
相关推荐
studytosky11 分钟前
深度学习理论与实战:反向传播、参数初始化与优化算法全解析
人工智能·python·深度学习·算法·分类·matplotlib
WolfGang00732112 分钟前
代码随想录算法训练营Day48 | 108.冗余连接、109.冗余连接II
数据结构·c++·算法
努力学算法的蒟蒻1 小时前
day35(12.16)——leetcode面试经典150
算法·leetcode·面试
cccc来财1 小时前
角点检测算法:Harris 和 FAST 方法
算法·计算机视觉·特征提取
风中月隐1 小时前
C语言中以坐标的方式图解“字母金字塔”的绘制
c语言·开发语言·算法·字母金子塔·坐标图解法
q_30238195561 小时前
告别“笨重”检测!VA-YOLO算法让疲劳驾驶识别更轻更快更准
算法·yolo
松涛和鸣1 小时前
DAY32 Linux Thread Programming
linux·运维·数据库·算法·list
LYFlied1 小时前
【每日算法】LeetCode 234. 回文链表详解
算法·leetcode·链表
NeDon2 小时前
[OJ]数据结构:移除链表元素
c语言·数据结构·算法·链表
刃神太酷啦2 小时前
C++ list 容器全解析:从构造到模拟实现的深度探索----《Hello C++ Wrold!》(16)--(C/C++)
java·c语言·c++·qt·算法·leetcode·list