跳到正文

题解

题解:CF2229C1 We Be Flipping (Easy Version)

aaa_Pigeon约 1 分钟
原文
文章信息与版本

文章信息

发布
2026年5月24日
收录
2026年7月12日
更新
2026年11月11日
篇幅
约 1 分钟

版本

  1. v1当前
hint 1

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

hint 2

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

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

C++
#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;
}
``
原文