QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#115943#5305. Oscar is All You NeedPetroTarnavskyi#RE 27ms5084kbC++172.8kb2023-06-27 19:29:112023-06-27 19:29:12

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-06-27 19:29:12]
  • 评测
  • 测评结果:RE
  • 用时:27ms
  • 内存:5084kb
  • [2023-06-27 19:29:11]
  • 提交

answer

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

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

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

map<VI, int> m;
map<VI, PII> op;

void brute(int n)
{
	VI v(n);
	iota(ALL(v), 1);
	m[v] = 0;
	queue<VI> q;
	q.push(v);
	while (!q.empty())
	{
		auto b = q.front();
		q.pop();
		int a = m[b];
		FOR (x, 1, n - 2)
		{
			FOR (y, 1, n - x)
			{
				VI vec;
				FOR (i, 0, y) vec.PB(b[n - y + i]);
				FOR (i, x, n - y) vec.PB(b[i]);
				FOR (i, 0, x) vec.PB(b[i]);
				if (!m.count(vec))
				{
					m[vec] = a + 1;
					op[vec] = {y, x};
					q.push(vec);
				}
			}
		}
	}
}

vector<PII> oper;

void zrobyty(VI& a, VI& sz, PII p)
{
	int n = SZ(a);
	int x = 0, y = 0;
	FOR (i, 0, p.first) x += sz[i];
	FOR (i, 0, p.second) y += sz[SZ(a) - 1 - i];
	oper.PB({x, y});
	
	VI na, nsz;
	FOR (i, 0, p.second)
	{
		na.PB(a[n - p.second + i]);
		nsz.PB(sz[n - p.second + i]);
	}
	
	FOR (i, p.first, n - p.second)
	{ 
		na.PB(a[i]);
		nsz.PB(sz[i]);
	}
	FOR (i, 0, p.first)
	{
		na.PB(a[i]);
		nsz.PB(sz[i]);
	}
	a = na;
	sz = nsz;
}

void comp(VI& a, VI& sz, int k)
{
	int n = SZ(a);
	VI na;
	VI nsz;
	FOR (i, 0, n)
	{ 
		if (a[i] != k) 
		{
			na.PB(a[i] < k ? a[i] : a[i] - 1);
			nsz.PB(sz[i]);
		}
		else
		{
			assert(na.back() == k - 1);
			nsz.back() += sz[i];
		}
	}
	a = na;
	sz = nsz;
}

void solve(VI a, VI sz)
{
	int n = SZ(a);
	if (SZ(a) <= 7)
	{
		while (m[a] != 0)
		{
			PII p = op[a];
			zrobyty(a, sz, p);
		}
		return;
	}
	RFOR (i, n - 2, n - 7)
	{
		if (i < 0) break;
		if (a[i] == n) continue;
		FOR (j, 0, i)
		{
			if (a[j] == a[i] + 1)
			{
				int k = a[j];
				zrobyty(a, sz, {i + 1, 1});
				zrobyty(a, sz, {n - 1 - (i - j), 1});
				
				comp(a, sz, k);
				
				solve(a, sz);
				
				return;
			}
		}
	}
	
	FOR (i, n - 1, 0)
	{
		FOR (j, 0, i)
		{
			if (a[i] == a[j] + 1)
			{
				int k = a[i];
				zrobyty(a, sz, {j + 1, 1});
				zrobyty(a, sz, {i - j, 1});
	
				comp(a, sz, k);
				
				solve(a, sz);
				
				return;
			}
		}
	}
	assert(0);
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	for (int i = 4; i <= 7; i++) brute(i);
	
	int t;
	cin >> t;
	while (t--)
	{
		int n;
		cin >> n;
		VI a(n);
		FOR (i, 0, n) cin >> a[i];
		if (n == 3)
		{
			if (a[2] < a[0])
				cout << "1\n1 1\n";
			else
				cout << "0\n";
			continue;
		}
		VI sz(n, 1);
		solve(a, sz);
		cout << SZ(oper) << '\n';
		FOR (i, 0, SZ(oper))
		{
			cout << oper[i].first << ' ' << oper[i].second << '\n';
		}
	}
	
	return 0;
}


Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 27ms
memory: 5084kb

input:

2
3
1 3 2
5
4 1 2 3 5

output:

0
2
2 1
1 1

result:

ok OK in maximum 2 operations

Test #2:

score: -100
Dangerous Syscalls

input:

120
3
1 3 2
3
3 2 1
3
2 3 1
5
1 2 3 4 5
12
11 9 2 8 3 10 6 1 4 7 5 12
36
24 9 7 3 31 15 13 1 4 33 11 29 16 23 2 25 35 21 32 14 6 18 17 26 28 8 27 22 20 36 10 19 34 12 30 5
4
4 2 3 1
5
3 5 2 1 4
4
1 2 4 3
10
5 7 4 9 6 8 1 3 10 2
5
3 1 5 2 4
5
3 5 1 2 4
3
3 1 2
13
3 1 2 11 12 13 8 6 5 4 10 9 7
16
12 8...

output:


result: