CF1736D

Solution#

sp=sq,即满足 s 中可以划分成若干段连续序列,这些序列左半部分和右半部分相等。

【无解】#

显然当 0/1 的个数不是偶数时无解。

其他情况有解。

【证明】#

证明中的函数名请见下面【证明中的函数意义】。

我们可以构造一组解:

把所有 ina2ia2i+12i 加入集合 S

ina2ia2i+1 时。必然为 01 或者 10

则下标 bi 可以构造为 one(S1),zero(S2),one(S3),

那么我们知道只有 size(S)mod2=0 时成以上 bi

此题当有解时一定时 size(S)mod2=0

证明:

ina2i=a2i+1 时,0/1 都是 2 的倍数,不需考虑。

ina2ia2i+1 时。必然为 01 或者 10

size(S)mod20 时,则 ina2ia2i+1 必然出现奇数次,即 0/1 都是奇数次。

与 【无解】 情况矛盾。

最后 p 即为 1,3,5,,2n1

时间复杂度为 Θ(n)

【证明中的函数意义】#

one(i) 表示 aiai+11 的位置。

zero(i) 表示 aiai+10 的位置。

size(S) 表示集合 S 的大小。

Code#

#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <cmath>
#include <sstream>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>

#define x first
#define y second
#define IOS ios::sync_with_stdio(false)
#define cit cin.tie(0)
#define cot cout.tie(0)

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;

const int N = 100010, M = 100010, MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const LL LLINF = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-8;

void solve()
{
	int n;
	string str;
	cin >> n >> str;
	
	int sum = 0;
	for (auto x: str) sum += x == '0';
	if (sum & 1) cout << -1 << endl;
	else
	{
		vector<int> res;
		for (int i = 0; i < n * 2; i += 2)
			if (str[i] != str[i + 1])
			{
				if (str[i] - '0' == res.size() % 2) res.push_back(i + 1);
				else res.push_back(i + 2);
			}
		cout << res.size() << ' ';
		for (auto x: res) cout << x << ' ';
		cout << endl;
		
		for (int i = 1; i <= n * 2; i += 2) cout << i << ' ';
		cout << endl;
	}
}

int main()
{
	IOS;
	cit, cot;
	int T = 1;
	cin >> T;
	while (T -- ) solve();
	return 0;
}
posted @   hcywoi  阅读(18)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
点击右上角即可分享
微信分享提示
主题色彩