Codeforces Round #750 (Div. 2) - E. Pchelyonok and Segments (dp)

简介: 笔记

E. Pchelyonok and Segments


题意

31.png

思路

可以从右往左考虑,即从 n 开始 选择长度为 1、2、3 … k 的不相交子段 使得这些子段内元素的和严格递减,求最大的 k。


设dp[i][j] 表示以第i 个数结尾(从右往左看)长度为j 的子段中,元素和的最大值。


那么转移时,会有两种情况:32.png


为什么 dp[i][j] 要维护的是最大值呢?因为从右往左 子段内的元素和要递减,右边的子段和越大,左边符合条件的情况越多。


具体细节见代码。


代码

// Author:zzqwtcc
// Problem: E. Pchelyonok and Segments
// Contest: Codeforces - Codeforces Round #750 (Div. 2)
// Time:2021-10-25 13:52:59
// URL: https://codeforces.com/contest/1582/problem/E
// Memory Limit: 512 MB
// Time Limit: 2000 ms
#include<bits/stdc++.h>
#include<unordered_map>
#define int long long 
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3f
#define mod 1000000007
#define MOD 998244353
#define rep(i, st, ed) for (int (i) = (st); (i) <= (ed);++(i))
#define pre(i, ed, st) for (int (i) = (ed); (i) >= (st);--(i))
// #define debug(x,y) cerr << (x) << " == " << (y) << endl;
#define endl '\n'
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
template<typename T> inline T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template<typename T> inline T lowbit(T x) { return x & -x; }
template<typename S,typename T>void debug(S s, T t){cerr << s << " == " << t << endl;}
template<typename T>void debug(T t){cerr << t << endl;}
template<typename T>void debug(T t[],int st,int ed){for(int i = st; i <=ed;++i){cerr << t[i] << " ";}cerr << endl;}
template<typename T>void debug(const vector<T>&t){for(int i =0 ; i < t.size();++i)cerr << t[i] << " ";cerr << endl;}
// template<typename T> T qmi(T a, T b = mod - 2, T p = mod) { T res = 1; b %= (p - 1 == 0 ? p : p - 1); while (b) { if (b & 1) { res = (LL)res * a % p; }b >>= 1; a = (LL)a * a % p; }return res % mod; }
const int N = 1e5 + 10;
int n;
int a[N];
int dp[N][505];
int sum[N];
void solve() {
    cin >> n;
  // 初始化
    for(int i = 1; i <= n;++i){
      for(int j = 1; j <= 500;++j){
        dp[i][j] = 0;
      }
    }
    // 预处理前缀和
    for(int i = 1; i <= n;++i)scanf("%lld",&a[i]),sum[i] = sum[i - 1] + a[i];
    // 从右往左开始
    dp[n][1] = a[n];
    // 无论如何 k都可以为1
    int res = 1;
    for(int i = n - 1;i >= 1;--i){
        // 更新长度为 1 的子段的最大值
      dp[i][1] = max(dp[i + 1][1], a[i]);
      for(int j = 2; j <= 500;++j){
            // 不选当前这个数
        dp[i][j] = dp[i + 1][j];
        // 选当前这个数
        if(i + j <= n && dp[i + j][j - 1] && sum[i + j - 1] - sum[i - 1] < dp[i + j][j - 1]){
                // 更新dp的最大值
          dp[i][j] = max(dp[i][j],sum[i + j - 1] - sum[i - 1]);
                // 如果满足条件 更新答案
          res = max(res,j);
        }
      }
    }
    cout << res << endl;
}
signed main() {
    int _; cin >> _;
    while (_--)
        solve();
    return 0;
}




目录
相关文章
|
9月前
|
机器学习/深度学习 人工智能 移动开发
.Codeforces Round 883 (Div. 3)
Codeforces Round 883 (Div. 3)
|
7月前
Codeforces Round #192 (Div. 2) (329A)C.Purification
Codeforces Round #192 (Div. 2) (329A)C.Purification
22 0
|
7月前
|
人工智能 算法 BI
Codeforces Round #179 (Div. 2)A、B、C、D
我们每次加进来的点相当于k,首先需要进行一个双重循环找到k点和所有点之间的最短路径;然后就以k点位判断节点更新之前的k-1个点,时间复杂度降到O(n^3),而暴力解法每次都要进行floyd,时间复杂度为O(n^4);相比之下前述解法考虑到了floyd算法的性质,更好了运用了算法的内质。
34 0
Codeforces Round #675 (Div. 2) A~D
Codeforces Round #675 (Div. 2) A~D
102 0
Codeforces Round #640 (Div. 4)
Codeforces Round #640 (Div. 4)
68 0
Equidistant Vertices-树型dp-Codeforces Round #734 (Div. 3)
Description A tree is an undirected connected graph without cycles. You are given a tree of n vertices. Find the number of ways to choose exactly k vertices in this tree (i. e. a k-element subset of vertices) so that all pairwise distances between the selected vertices are equal (in other words,
119 0
Codeforces Round #723 (Div. 2)B. I Hate 1111
Description You are given an integer x. Can you make x by summing up some number of 11,111,1111,11111,…? (You can use any number among them any number of times). For instance, 33=11+11+11 144=111+11+11+11
141 0
Codeforces Round #723 (Div. 2)B. I Hate 1111
|
机器学习/深度学习 算法 C++