题解:CF2229C1 We Be Flipping (Easy Version)

· · 题解

:::info[hint 1]

一次操作会反转一个前缀,而后缀不会改变。

:::

:::info[hint 2]

考虑从后往前操作:如果当前 a_i > 0 则反转,否则不变。

不难发现最终所有数都非正。

:::


#include <bits/stdc++.h>
using namespace std;

const int N = 200000;

int n;
long long a[N + 10];

void work() {
    cin >> n;
    for (int i = 1; i <= n; ++i) cin >> a[i];

    long long mul = 1LL;
    vector<int> ans;

    for (int i = n; i >= 1; --i) {
        if (a[i] * mul > 0) {
            ans.push_back(i), mul *= -1LL;
        }
    }

    cout << ans.size() << '\n';
    for (int i = 0; i < ans.size(); ++i) cout << ans[i] << ' ';
    cout << '\n';
}

int main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    int T; cin >> T;
    while (T--) work();

    return 0;
}
``