Codeforces TypeDB Forces 2023 C. Remove the Bracket【上下界DP】

C. Remove the Bracket

F

题意

给定一个长度为 n n n 的整数数组 a a a 和一个非负整数 s s s

  • 要求 ∀ i ∈ [ 2 , n − 1 ] , 选定两个整数 x i , y i ,满足 x i + y i = s 且 ( x i − s ) ( y i − s ) ≥ 0 \forall i \in [2,n - 1],选定两个整数 x_i, y_i,满足 x_i + y_i = s 且 (x_i - s)(y_i - s) \geq 0 i[2,n1],选定两个整数xi,yi,满足xi+yi=s(xis)(yis)0

定义花费 F = a 1 ⋅ x 1 + y 1 ⋅ x 2 + y 2 ⋅ x 3 + y 3 ⋅ x 4 + . . . + y n − 2 ⋅ x n − 1 + y n − 1 ⋅ a n F = a_1 \cdot x_1 + y_1 \cdot x_2 + y_2 \cdot x_3 + y_3 \cdot x_4 + ... + y_{n - 2} \cdot x_{n - 1} + y_{n -1 } \cdot a_n F=a1x1+y1x2+y2x3+y3x4+...+yn2xn1+yn1an

求出满足限制的最小花费

思路

( x i − s ) ( y i − s ) ≥ 0 (x_i - s)(y_i - s) \geq 0 (xis)(yis)0 意味着: m i n ( x i , y i ) ≥ s min(x_i, y_i) \geq s min(xi,yi)s m a x ( x i , y i ) ≤ s max(x_i, y_i) \leq s max(xi,yi)s,而 x i + y i = s x_i + y_i = s xi+yi=s
所以 x i x_i xi 的取值是一段连续的区间

如果我们记录位置 i i i 分裂出来的 y i y_i yi 对应的最小值,可能有 n × s n \times s n×s 种状态,考虑优化

我们注意到:对于当前的 i i i,如果我们分裂成了 x i x_i xi y i y_i yi,那么它们会影响的位置只有:
y i − 1 ⋅ x i + y i ⋅ x i + 1 y_{i - 1} \cdot x_i + y_i \cdot x_{i + 1} yi1xi+yixi+1,我们假设 y i − 1 < x i + 1 y_{i -1} < x_{i + 1} yi1<xi+1,当 x i x_i xi 1 1 1时, y i y_i yi 对应减 1 1 1,此时 F F F 的变化量为: δ = y i − 1 − x i + 1 < 0 \delta = y_{i - 1} - x_{i + 1} < 0 δ=yi1xi+1<0,总体 F F F 是在减小的!所以这种情况下我们要尽可能为 x i x_i xi 取到最大值
另外一种情况则 x i x_i xi 尽可能取到最小值 x i − 1 = y i + 1 x_{i -1} = y_{i + 1} xi1=yi+1 时, x i x_i xi 取最大最小都无所谓

那么我们可以得出结论:每次 a i a_i ai 分裂都会往两种最值之一分裂,这就是典型的上下界 D P DP DP 求解最值问题

我们只需要在当前位置 i i i 记录两种取法的最小值即可, d p [ i ] [ 0 ] dp[i][0] dp[i][0] 表示这个位置取的是 x i x_i xi y i y_i yi 留到后面; d p [ i ] [ 1 ] dp[i][1] dp[i][1] 则相反

时间复杂度: O ( 4 n ) O(4n) O(4n)

#include<bits/stdc++.h>
#define fore(i,l,r)	for(int i=(int)(l);i<(int)(r);++i)
#define fi first
#define se second
#define endl '\n'
#define ull unsigned long long
#define ALL(v) v.begin(), v.end()
#define Debug(x, ed) std::cerr << #x << " = " << x << ed;

const int INF=0x3f3f3f3f;
const long long INFLL=1e18;

typedef long long ll;

int main(){
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::cout.tie(nullptr);
    int t;
    std::cin >> t;
    while(t--){
        int n, s;
        std::cin >> n >> s;
        std::vector<ll> a(n + 1), x(n + 1), y(n + 1);
        std::vector<std::array<ll, 2>> dp(n + 1);
        fore(i, 1, n + 1){
            std::cin >> a[i];
            if(i == 1 || i == n) x[i] = y[i] = a[i];
            else if(s >= a[i]) x[i] = 0, y[i] = a[i];
            else x[i] = s, y[i] = a[i] - s; //这里x 和 y 没有固定大小关系,但一定是上下界
        }

        dp[1][0] = dp[1][1] = 0;
        fore(i, 2, n + 1){
            dp[i][0] = std::min(dp[i - 1][0] + y[i - 1] * x[i], dp[i - 1][1] + x[i - 1] * x[i]);
            dp[i][1] = std::min(dp[i - 1][0] + y[i - 1] * y[i], dp[i - 1][1] + x[i - 1] * y[i]);
        }

        std::cout << dp[n][0] << endl;
    }
    return 0;
}
  • 14
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,Codeforces Round 511 (Div. 1)是一个比赛的名称。然而,引用内容中没有提供与这个比赛相关的具体信息或问题。因此,我无法回答关于Codeforces Round 511 (Div. 1)的问题。如果您有关于这个比赛的具体问题,请提供更多的信息,我将尽力回答。 #### 引用[.reference_title] - *1* [Codeforces Round 860 (Div. 2)题解](https://blog.csdn.net/qq_60653991/article/details/129802687)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Codeforces Round 867 (Div. 3)(A题到E题)](https://blog.csdn.net/wdgkd/article/details/130370975)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Codeforces Round 872 (Div. 2)(前三道](https://blog.csdn.net/qq_68286180/article/details/130570952)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值