栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

Codeforces Educational Round 123 C Increase Subarray Sums

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Codeforces Educational Round 123 C Increase Subarray Sums

#include 
using namespace std;
const int INF = 0x3f3f3f3f;
int n, x;
const int N = 5010;
int a[N];
int maxseg[N];  // maxseg[i]表示长度为`i`的序列的最大和
int main() {
  int tc;
  cin >> tc;
  while (tc--) {
    cin >> n >> x;
    for (int i = 0; i < n; ++i) cin >> a[i];
    maxseg[0] = 0;
    for (int i = 1; i <= n; ++i) maxseg[i] = -INF;

    // Iterate over length `l`, find the max value within the segment
    for (int l = 0; l < n; ++l) {
      int sum = 0;
      for (int r = l; r < n; ++r) {
        sum += a[r];
        maxseg[r - l + 1] = max(maxseg[r - l + 1], sum);
      }
    }
    // If we want to increase `k` numbers, it optimal to increase number within
    // the segment if possible, i.e, if(len <= k), the sum will be increased by
    // k*x; else, the sum will be increased by len*x
    //
    // So the problem is effectively turned into a problem to find the maximal
    // sum of subsequence of length ranging from 1 to n
    for (int k = 0; k <= n; ++k) {
      int best = 0;
      for (int len = 0; len <= n; ++len)
        best = max(best, maxseg[len] + min(k, len) * x);
      cout << best << ' ';
    }
    cout << endl;
  }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/744539.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号