比赛 2026.8.28 评测结果 AAAAAAAAAA
题目名称 一周一次买下同班同学的那些事 最终得分 100
用户昵称 RpUtl 运行时间 0.337 s
代码语言 C++ 内存使用 5.17 MiB
提交时间 2026-08-28 09:55:02
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 10;
int T, n, pos, now, mk[N];
ll a[N], s1[N], s2[N], maxx;
void work() {
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> a[i];
    for (int i = 1; i <= n; i++) {
        s1[i] = s1[i - 1] + a[i];
        s2[i] = s2[i - 1] + abs(a[i]); 
    }
    maxx = s1[n], pos = -1;
    for (int i = 1; i <= n; i++) {
        if (a[i] > 0) {
            ll val = s2[i - 1] + s1[n] - s1[i] - a[i];
            if (val > maxx) maxx = val, pos = i;
        }
    }
    if (pos == -1) {
        cout << "0" << '\n';
        cout << '\n';
    } else {
        int now = 1, sec = -1; 
        mk[pos] = 1;
        for (int i = pos - 1; i >= 1; i--) {
            if ((a[i] < 0) ^ now) {
                mk[i] = 1, now ^= 1;
                if (sec == -1) sec = i;
            }
        }
        vector<int> ans;
        if (sec == -1) {
            ans.push_back(pos);
        } else {
            ans.push_back(sec);
            for (int i = 1; i < sec; i++) {
                if (a[i] < 0 && mk[i]) ans.push_back(i);
            }
            for (int i = sec - 1; i >= 1; i--) {
                if (a[i] > 0 && mk[i]) ans.push_back(i);
            }
            ans.push_back(pos);
        }
        cout << ans.size() << '\n';
        for (auto u : ans) cout << u << " ";
        cout << '\n';
    }
    for (int i = 0; i <= n; i++) mk[i] = 0;
    return;
}
int main() {
    freopen("bought.in", "r", stdin);
    freopen("bought.out", "w", stdout);
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    cin >> T;
    while (T--) work();
    return 0;
}