比赛 2026.8.28 评测结果 WWWWWWWWWW
题目名称 一周一次买下同班同学的那些事 最终得分 0
用户昵称 hsl_beat 运行时间 0.861 s
代码语言 C++ 内存使用 4.82 MiB
提交时间 2026-08-28 12:38:18
显示代码纯文本
#include<bits/stdc++.h>
using namespace std;
#define int long long
void solve()
{
    int n;
    cin >> n;
    vector<int> a(n + 1);
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    vector<int> ans;
    function<void(int, int, int)> add = [&](int p, int l, int r) -> void {
        if (p - 1 > r) {
            ans.push_back(p - 1);
            ans.push_back(r);
            if (l - 1 > 0) {
                ans.push_back(l - 1);
            }
            ans.push_back(p);
        } else {
            if (l - 1 > 0) {
                ans.push_back(l - 1);
            }
            ans.push_back(p);
        }
        a[p] = -a[p];
        for (int j = l; j <= r; j++) {
            a[j] = -a[j];
        }
    };
    int rr = n + 1, cnt = 0;
    priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
    for (int i = n; i >= 1; i--) {
        if (a[i] > 0) {
            if (i < n && a[i + 1] < 0) {
                if (pq.size() && cnt > pq.top().first) {
//                    cout << pq.top().first << ' ' << pq.top().second << '\n';
                    add(pq.top().second, i + 1, rr);
                    pq.pop();
                }
            }
            pq.push({a[i], i});
            cnt = 0;
        } else {
            if (cnt == 0) {
                rr = i;
            }
            cnt += -a[i];
        }
    }
    cout << ans.size() << '\n';
    for (int i = 0; i < ans.size(); i++) {
        cout << ans[i] << ' ';
    }
    cout << '\n';
}
signed main()
{
    freopen("bought.in", "r", stdin);
    freopen("bought.out", "w", stdout);
    int T;
    cin >> T;
    while (T--) {
        solve();
    }
    return 0;
}

/*
int l = pq.top().second.first, r = pq.top().second.second;
//                cout << i << ' ' << l << ' ' << r << '\n';
pq.pop();
if (p - 1 > r) {
ans.push_back(i - 1);
ans.push_back(r);
if (l - 1 > 0) {
ans.push_back(l - 1);
}
ans.push_back(p);
} else {
if (l - 1 > 0) {
ans.push_back(l - 1);
}
ans.push_back(p);
}
a[p] = -a[p];
cnt = -a[p];
ls = i;
for (j = l; j <= r; j++) {
a[j] = -a[j];
}
*/