codeforces(C++ Summation Game)

题目: 

codeforces(C++ Summation Game)_第1张图片

codeforces(C++ Summation Game)_第2张图片 

翻译: 

codeforces(C++ Summation Game)_第3张图片

 思路:

1、将数据从大到小排序

2、用前缀和

3、每次用总和减去2倍的乘-1的数,求最大值

代码:

#include 
#include
using namespace std;

void solve() {
    int n, k, x;
    cin >> n >> k >> x;

    int A[200020] = {};
    for (int i = 1; i <= n; i++)
        cin >> A[i];

    sort(A + 1, A + n + 1, greater());//降序排列

    for (int i = 1; i <= n; i++)
        A[i] += A[i - 1];

    int ans = -1e9;
    for (int i = 0; i <= k; i++)
        ans = max(ans, (A[n] - 2 * A[min(i + x, n)] + A[i]));

    cout << ans << "\n";
}

int main() 
{
    int tc;
    cin >> tc;

    while (tc--)
        solve();
}

你可能感兴趣的:(codeforces,c++,开发语言)