QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#495327#3033. Harry Potter and the Palindromic RadiusPetroTarnavskyi0 0ms0kbC++202.2kb2024-07-27 20:03:552024-07-27 20:03:56

Judging History

This is the latest submission verdict.

  • [2024-07-27 20:03:56]
  • Judged
  • Verdict: 0
  • Time: 0ms
  • Memory: 0kb
  • [2024-07-27 20:03:55]
  • Submitted

answer

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define RFOR(i, a, b) for(int i = (a) - 1; i >= (b); i--)
#define SZ(a) int(a.size())
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair
#define F first
#define S second

typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef double db;

const int N = 1 << 20;

int col[N];
VI comps[6];
string ans;

int cntComps = 0;
bool OK = true;
void dfs(const vector<VI>& g, int v)
{
	comps[cntComps].PB(v);
	for (int tow : g[v])
	{
		if (col[tow / 2] == -1)
		{
			col[tow / 2] = col[v] ^ (tow & 1);
			dfs(g, tow / 2);
		}
		else
			OK &= (col[tow / 2] == (col[v] ^ (tow & 1)));
	}
}

void solve()
{
	int n;
	cin >> n;
	VI p(n);
	FOR(i, 0, n)
	{
		cin >> p[i];
		p[i]++;
	}
	FOR(i, 0, n)
	{
		if (i - p[i] + 1 < 0 || i + p[i] - 1 >= n)
		{
			cout << "0\n";
			return;
		}
	}
	vector<VI> g(n);
	FOR(i, 0, n)
		g[i].reserve(4);
		
	cntComps = 0;
	FOR(i, 0, 6)
		comps[i].clear();
	fill(col, col + n, -1);
	int l = -1, r = -1;
	FOR(i, 0, n)
	{
		int cur = 0;
		if (i <= r)
			cur = min(r - i + 1, p[l + (r - i)]);
		if (cur > p[i])
		{
			cout << "0\n";
			return;
		}
		while (cur < p[i])
		{
			if (cur > 0)
			{
				g[i - cur].PB(2 * (i + cur));
				g[i + cur].PB(2 * (i - cur));
			}
			cur++;
		}
		if (0 <= i - cur && i + cur < n)
		{
				g[i - cur].PB(2 * (i + cur) + 1);
				g[i + cur].PB(2 * (i - cur) + 1);
		}
		if (i + cur - 1 > r)
		{
			r = i + cur - 1;
			l = i - cur + 1;
		}
	}
	FOR(i, 0, n)
	{
		if (col[i] == -1)
		{
			if (cntComps == 6)
			{
				cout << "0\n";
				return;
			}
			col[i] = 0;
			OK = true;
			dfs(g, i);
			if (!OK)
			{
				cout << "0\n";
				return;
			}
			cntComps++;
		}
	}
	cout << (1 << cntComps) << "\n";
	ans.resize(n);
	FOR(mask, 0, 1 << cntComps)
	{
		FOR(i, 0, cntComps)
		{
			int b = (mask >> (cntComps - 1 - i)) & 1;
			for (int v : comps[i])
			{
				ans[v] = (col[v] ^ b) + '0';
			}
		}
		cout << ans << "\n";
	}
}

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	int t;
	cin >> t;
	while (t--)
		solve();
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Pretests


Final Tests

Test #1:

score: 0
Time Limit Exceeded

input:

131112
2
0 0
2
0 1
2
0 0
2
1 0
2
0 0
2
0 1
2
0 0
2
0 1
3
0 1 0
3
0 1 1
3
0 0 0
3
0 1 0
3
0 1 0
3
0 2 0
3
0 0 0
3
1 0 0
3
0 0 0
3
1 0 0
3
0 1 0
3
0 2 0
3
0 0 0
3
0 0 1
3
0 1 0
3
0 2 0
4
0 1 1 0
4
0 1 2 0
4
0 0 1 0
4
0 0 1 1
4
0 1 0 0
4
0 1 0 1
4
0 0 0 0
4
0 0 1 0
4
0 0 1 0
4
1 0 1 0
4
0 1 1 0
4
0 1 2...

output:

4
00
01
10
11
0
4
00
01
10
11
0
4
00
01
10
11
0
4
00
01
10
11
0
4
000
010
101
111
0
4
001
011
100
110
4
000
010
101
111
4
000
010
101
111
0
4
001
011
100
110
0
4
001
011
100
110
0
4
000
010
101
111
0
4
001
011
100
110
0
4
000
010
101
111
0
4
0000
0101
1010
1111
0
4
0010
0111
1000
1101
0
4
0001
0100
...

result: