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;
}
相关推荐
@––––––3 小时前
力扣hot100—系列9—图论
算法·leetcode·图论
pp起床3 小时前
图论 | part01
算法·深度优先·图论
luckycoding3 小时前
3676. 碗子数组的数目
算法·游戏·深度优先
I_LPL3 小时前
day50 代码随想录算法训练营 图论专题3
java·算法·深度优先·图论·求职面试
Flying pigs~~3 小时前
我的leetcode hot100之行(持续更新)
数据结构·算法·leetcode
滴滴答滴答答3 小时前
机考刷题之 7 LeetCode 240 搜索二位矩阵Ⅱ
java·算法·leetcode
武帝为此3 小时前
【HMAC加密算法介绍】
算法·密码学
进击的荆棘3 小时前
优选算法——模拟
java·开发语言·算法·模拟
仰泳的熊猫3 小时前
题目2086:蓝桥杯算法提高VIP-最长公共子序列
数据结构·c++·算法·蓝桥杯·动态规划
请你喝好果汁6413 小时前
ML-线性回归(Linear Regression)
算法·回归·线性回归