QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#115946#5305. Oscar is All You NeedPetroTarnavskyiWA 31ms5136kbC++172.9kb2023-06-27 19:34:352023-06-27 19:34:35

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:34:35]
  • 评测
  • 测评结果:WA
  • 用时:31ms
  • 内存:5136kb
  • [2023-06-27 19:34:35]
  • 提交

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;
	}
	VI pos(n);
	FOR(i, 0, n)
		pos[a[i] - 1] = i;
		
	FOR(it,	1, n){
		int i = pos[it - 1];
		int j = pos[it];
		if(j > i || i >= n - 2) continue;
	
			int k = a[j];
			zrobyty(a, sz, {i + 1, 1});
			zrobyty(a, sz, {n - 1 - (i - j), 1});
			
			comp(a, sz, k);
			assert(SZ(a) == n - 1);
			
			solve(a, sz);
			
			return;
	}
	
	FOR(it,	1, n){
		int i = pos[it];
		int j = pos[it - 1];
		if(j > i || i >= n - 1) continue;

		int k = a[i];
		zrobyty(a, sz, {j + 1, 1});
		zrobyty(a, sz, {i - j, 1});
	
		comp(a, sz, k);
		assert(SZ(a) == n - 1);
		solve(a, sz);
				
		return;
	}
}

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: 5136kb

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
Wrong Answer
time: 31ms
memory: 5108kb

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:

0
1
1 1
1
1 1
0
14
8 1
6 1
10 1
7 1
4 1
8 1
7 1
9 1
7 1
8 2
5 3
4 2
8 2
6 3
77
8 1
6 1
10 1
7 1
4 1
8 1
7 1
9 1
7 1
8 2
5 3
4 2
8 2
6 3
15 1
24 1
6 1
30 1
20 1
22 1
33 1
5 3
21 2
30 1
31 1
11 1
20 1
16 4
33 1
14 2
32 1
4 7
21 1
27 1
34 1
2 10
24 1
28 1
16 1
34 1
29 1
7 1
25 1
11 2
25 1
11 1
23 1
13 ...

result:

wrong answer Integer parameter [name=operations] equals to 77, violates the range [0, 73]