跳到正文

题解

题解:P12074 [OOI 2025] The arithmetic exercise

用户 1674785约 2 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 2 分钟
题目
P12074

版本

  1. v1当前

和几位大佬方法大同小异

C++
#include <bits/stdc++.h>
#define ll long long
using namespace std;

const int MAXM = 300000 + 5;
int x[MAXM];

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int t;
    cin >> t;
    while(t--){
        int n, m;
        cin >> n >> m;
        for(int i = 0; i < m; i++){
            cin >> x[i];
        }
        ll W = 0;
        for(int i = 0; i < m; i++) W -= x[i];
        multiset<ll, greater<ll>> s;
        int p = 0;
        for(int i = 0; i < m; i++){
            ll v = (ll)x[m - 1 - i] * 2;
            s.insert(v);
            int L = (i + 2) / 2;
            int R = (i + n + 1) / 2;
            while(p < L){
                W += *s.begin();
                s.erase(s.begin());
                p++;
            }
            while(p + (int)s.size() > R){
                auto it = prev(s.end());
                s.erase(it);
            }
        }
        for(ll v : s){
            if(v > 0) W += v;
        }
        cout << W << "\n";
    }
    return 0;
}
原文