QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#77058#5508. Job for a HobbitHe_RenAC ✓82ms12276kbC++234.0kb2023-02-13 00:53:402023-02-13 00:53:42

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-02-13 00:53:42]
  • 评测
  • 测评结果:AC
  • 用时:82ms
  • 内存:12276kb
  • [2023-02-13 00:53:40]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int MAXN = 50 + 5;
const int MAXD = 10 + 5;
const int MAXP = MAXN * MAXD;

using Data = array<int,2>;

int n,d,pcnt;
vector<int> a[MAXN];
int c[MAXP];
Data pos[MAXP], need[MAXP];

vector<pii> ans, finans;

bool solve_finans(void)
{
	static vector<int> b[MAXN];
	for(int i=0; i<=n+1; ++i)
		b[i].clear();
	
	static int id[MAXP];
	iota(id+1, id+pcnt+1, 1);
	sort(id+1, id+pcnt+1, [&] (int x,int y){
		return c[x] < c[y];
	});
	
	int cur = 0;
	for(int i=1; i<=pcnt && cur<=n+1; ++i)
	{
		int u = id[i], v = id[i-1];
		if((i > 1 && c[u] != c[v]) || (int)b[cur].size() == d) ++cur;
		if(cur > n+1) break;
		b[cur].emplace_back(u);
	}
	if(cur > n+1)
		return 0;
	
	while(1)
	{
		int i = 1;
		while(i<=n+1 && ((int)b[i].size() == 0 || (int)b[i-1].size() == d)) ++i;
		if(i > n+1) break;
		finans.emplace_back(i, i-1);
		b[i-1].emplace_back(b[i].back());
		b[i].pop_back();
	}
	
	for(int i=n; i>=1; --i)
	{
		for(int j=1; j<=d; ++j)
		{
			finans.emplace_back(i-1, i);
			b[i].emplace_back(b[i-1].back());
			b[i-1].pop_back();
		}
	}
	
	for(int i=1; i<=n; ++i)
	{
		assert((int)b[i].size() == d);
		for(int j=0; j<d; ++j)
			need[b[i][j]] = {i,j};
	}
	
	return 1;
}

void OP(int i,int j)
{
	assert((int)a[i].size() && (int)a[j].size() < d);
	ans.emplace_back(i, j);
	int u = a[i].back(); a[i].pop_back();
	a[j].emplace_back(u);
	pos[u] = {j, (int)a[j].size() - 1};
}
void OP(int i,int j,int k)
{
	while(k--) OP(i, j);
}

void move_empty(int i,int j)
{
	if(i <= j)
	{
		for(int k=i; k<j; ++k)
			OP(k+1, k, d);
	}
	else
	{
		for(int k=i; k>j; --k)
			OP(k-1, k, d);
	}
}

void move_single(int x, int beg, int enn)// beg <= enn
{
	if(beg == enn) return;
	
	int t1 = d-1 - enn;
	int t2 = enn - beg;
	
	OP(x, x+1, t1);
	OP(x, x-1, t2);
	
	OP(x, x+1);
	
	OP(x-1, x, t2);
	OP(x+1, x, t1+1);
}

void move_special(int x)// d, 1, 0, d-1
{
	for(int i=1; i<=d; ++i)
		OP(x-2, x-1), OP(x-1, x);
	OP(x, x+1);
	OP(x-1, x-2);
}

void solve(void)
{
	scanf("%d%d",&n,&d);
	pcnt = n * d;
	for(int i=1; i<=n; ++i)
	{
		a[i].resize(d);
		for(int j=0; j<d; ++j)
		{
			int u = (i-1) * d + j + 1;
			scanf("%d",&c[u]);
			a[i][j] = u;
			pos[u] = {i, j};
		}
	}
	a[0].clear();
	a[n+1].clear();
	
	if(d == 1)
	{
		printf("TAK\n");
		printf("0\n");
		return;
	}
	
	ans.clear(); finans.clear();
	
	if(solve_finans() == 0)
	{
		printf("NIE\n");
		return;
	}
	
	for(int needx=1; needx<=n; ++needx)
		for(int needy=d-1; needy>=0; --needy)
		{
			int u = 1;
			while(u<=pcnt && need[u] != Data{needx, needy}) ++u;
			
			assert(u <= pcnt);
			
			int curx = pos[u][0], cury = pos[u][1];
			if(curx == needx && cury == needy) continue;
			
			move_empty(0, curx-1);
			move_empty(n+1, curx+1);
			
//			printf("ok1\n");
			
			if(curx == needx)
			{
				move_single(curx, cury, needy);
				move_empty(curx-1, 0);
				move_empty(curx+1, n+1);
//				printf("fin\n");
				continue;
			}
			
//			printf("ok2\n");
			
			for(int i=d-1; i>=0; --i)
				OP(curx, i == cury? curx-1: curx+1);
			
//			printf("ok3\n");
//			
//			printf("curx = %d, needx = %d\n",curx,needx);
			
			for(int i=curx; i>needx+1; --i)
				move_special(i);
			
//			printf("ok4\n");
			
//			for(int i=-1; i<=2; ++i)
//				printf("siz[%d] = %d\n",needx+i,(int)a[needx+i].size());
			
			OP(needx-1, needx);
			OP(needx, needx+1);
			OP(needx+1, needx+2);
			
			OP(needx, needx-1);
			move_empty(needx, needx-1);
			
//			printf("ok5\n");
			
			move_single(needx, 0, needy);
			
			move_empty(needx-1, 0);
			move_empty(needx+1, n+1);
		}
	
	reverse(finans.begin(), finans.end());
	for(auto t: finans)
		ans.emplace_back(t.second, t.first);
	
	printf("TAK\n");
	printf("%d\n",(int)ans.size());
	for(auto t: ans)
		printf("%d %d\n",t.first,t.second);
}

int main(void)
{
	int T;
	scanf("%d",&T);
	while(T--) solve();
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 3560kb

input:

2
2 2
1 2
2 1
1 4
1 2 3 4

output:

TAK
32
2 3
2 3
1 0
1 2
0 1
2 1
3 2
3 2
1 0
1 0
2 1
2 3
0 1
1 2
2 3
1 0
0 1
0 1
3 2
3 2
1 0
1 0
2 1
2 3
1 2
3 2
0 1
0 1
1 0
1 0
2 1
2 1
NIE

result:

ok Correct! Used 32 swaps

Test #2:

score: 0
Accepted
time: 1ms
memory: 3772kb

input:

25
2 7
4 1 2 2 3 2 4
4 4 6 4 2 2 5
2 5
6 5 5 3 3
1 6 5 2 5
2 8
1 4 2 1 4 1 2 1
1 3 4 4 2 2 1 2
2 4
1 1 5 2
1 5 2 2
2 10
3 5 4 5 5 2 1 3 5 2
5 2 2 1 5 1 3 3 4 2
2 8
2 4 3 3 4 2 1 2
5 1 4 1 2 6 3 4
2 9
4 3 4 3 4 2 4 1 2
2 4 4 2 2 3 3 3 4
2 4
4 1 3 1
4 2 4 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aps

Test #4:

score: 0
Accepted
time: 53ms
memory: 11612kb

input:

1
50 10
33 28 16 37 35 47 28 35 31 21
47 40 33 25 16 40 50 25 13 33
10 14 48 38 1 38 32 43 28 18
11 16 1 51 4 45 16 31 27 41
52 18 32 51 17 31 38 48 49 47
5 24 20 48 51 20 29 32 35 20
39 18 21 45 10 30 11 5 32 32
5 46 19 39 30 26 15 17 15 8
15 15 21 25 41 28 6 8 6 20
47 28 34 12 44 16 48 49 52 47
23...

output:

TAK
634657
1 0
1 0
1 0
1 0
1 0
1 0
1 0
1 0
1 0
1 0
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
3 2
3 2
3 2
3 2
3 2
3 2
3 2
3 2
3 2
3 2
4 3
4 3
4 3
4 3
4 3
4 3
4 3
4 3
4 3
4 3
5 4
5 4
5 4
5 4
5 4
5 4
5 4
5 4
5 4
5 4
6 5
6 5
6 5
6 5
6 5
6 5
6 5
6 5
6 5
6 5
7 6
7 6
7 6
7 6
7 6
7 6
7 6
7 6
7 6
7 6
8 7
8 7
8...

result:

ok Correct! Used 634657 swaps

Test #5:

score: 0
Accepted
time: 2ms
memory: 3700kb

input:

5
10 1
11
2
10
3
1
12
8
4
5
7
10 7
11 5 6 5 2 1 2
8 7 1 9 10 6 4
6 8 4 6 2 12 9
12 3 10 5 11 4 7
9 11 4 2 9 3 9
6 7 5 11 1 10 6
11 7 6 7 12 1 1
5 3 2 3 4 7 12
3 8 11 9 12 9 8
1 12 12 4 10 1 2
10 7
9 8 12 8 10 7 4
1 1 10 7 3 5 1
11 6 11 5 2 4 12
6 12 8 3 8 6 8
12 7 4 1 11 8 6
7 6 2 5 9 3 6
12 10 4 9 ...

output:

TAK
0
TAK
11827
1 0
1 0
1 0
1 0
1 0
1 0
1 0
2 1
2 1
2 1
2 1
2 1
2 1
2 1
3 2
3 2
3 2
3 2
3 2
3 2
3 2
4 3
4 3
4 3
4 3
4 3
4 3
4 3
5 4
5 4
5 4
5 4
5 4
5 4
5 4
6 5
6 5
6 5
6 5
6 5
6 5
6 5
10 11
10 11
10 11
10 11
10 11
10 11
10 11
9 10
9 10
9 10
9 10
9 10
9 10
9 10
8 9
8 9
8 9
8 9
8 9
8 9
8 9
7 8
7 6
7 8...

result:

ok Correct! Used 25829 swaps

Test #6:

score: 0
Accepted
time: 27ms
memory: 5088kb

input:

2
25 10
27 26 27 26 27 26 27 26 27 26
26 27 26 27 26 27 26 27 26 27
25 25 25 25 25 25 25 25 25 25
24 24 24 24 24 24 24 24 24 24
23 23 23 23 23 23 23 23 23 23
22 22 22 22 22 22 22 22 22 22
21 21 21 21 21 21 21 21 21 21
20 20 20 20 20 20 20 20 20 19
19 19 19 19 19 19 19 19 18 18
18 18 18 18 18 18 18 1...

output:

TAK
166076
1 0
1 0
1 0
1 0
1 0
1 0
1 0
1 0
1 0
1 0
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
3 2
3 2
3 2
3 2
3 2
3 2
3 2
3 2
3 2
3 2
4 3
4 3
4 3
4 3
4 3
4 3
4 3
4 3
4 3
4 3
5 4
5 4
5 4
5 4
5 4
5 4
5 4
5 4
5 4
5 4
6 5
6 5
6 5
6 5
6 5
6 5
6 5
6 5
6 5
6 5
7 6
7 6
7 6
7 6
7 6
7 6
7 6
7 6
7 6
7 6
8 7
8 7
8...

result:

ok Correct! Used 256618 swaps

Test #7:

score: 0
Accepted
time: 82ms
memory: 12276kb

input:

1
50 10
1 1 1 37 35 47 1 35 31 21
47 40 1 25 1 40 1 25 13 1
10 14 48 38 1 38 32 43 1 18
11 1 1 1 4 45 1 31 27 41
1 18 32 1 17 31 38 48 49 47
1 24 1 48 1 1 29 32 35 1
39 18 21 45 10 30 11 1 32 32
1 46 19 39 30 26 15 17 15 8
15 15 21 25 41 1 6 8 6 1
47 1 34 12 1 1 48 49 1 47
23 18 1 1 37 1 41 1 2 30
4...

output:

TAK
630295
1 0
1 0
1 0
1 0
1 0
1 0
1 0
1 0
1 0
1 0
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
2 1
3 2
3 2
3 2
3 2
3 2
3 2
3 2
3 2
3 2
3 2
4 3
4 3
4 3
4 3
4 3
4 3
4 3
4 3
4 3
4 3
5 4
5 4
5 4
5 4
5 4
5 4
5 4
5 4
5 4
5 4
6 5
6 5
6 5
6 5
6 5
6 5
6 5
6 5
6 5
6 5
7 6
7 6
7 6
7 6
7 6
7 6
7 6
7 6
7 6
7 6
8 7
8 7
8...

result:

ok Correct! Used 630295 swaps

Test #8:

score: 0
Accepted
time: 2ms
memory: 3524kb

input:

1
50 10
15 52 42 32 47 29 31 51 12 48
43 19 14 2 28 39 51 10 43 36
33 6 29 11 4 18 12 41 15 34
24 2 48 30 25 23 34 17 9 28
24 8 17 4 21 14 42 19 48 30
23 16 30 9 33 25 33 36 21 12
36 18 46 35 31 13 44 34 15 50
34 11 33 38 9 23 9 42 4 3
37 12 2 41 7 34 23 16 10 27
24 8 38 16 24 11 47 29 3 50
34 52 47...

output:

NIE

result:

ok Correct! Used 0 swaps