当前位置: 代码迷 >> 综合 >> Codeforces Educational Round 123 C Increase Subarray Sums
  详细解决方案

Codeforces Educational Round 123 C Increase Subarray Sums

热度:39   发布时间:2023-12-05 08:28:28.0
#include <bits/stdc++.h>
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 segmentfor (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 nfor (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;}
}
  相关解决方案