QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#305243#5508. Job for a Hobbitushg8877WA 1ms3620kbC++142.7kb2024-01-14 23:19:062024-01-14 23:19:07

Judging History

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

  • [2024-01-14 23:19:07]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3620kb
  • [2024-01-14 23:19:06]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define MP make_pair
mt19937 rnd(time(0));
int n,k;
int a[55][15],top[55];
int b[55][15];// 目标序列 
void debug(){
	for(int i=0;i<=n+1;i++){
		cout<<"bang "<<i<<" : ";
		for(int j=1;j<=top[i];j++) cout<<a[i][j]<<' ';
		cout<<endl;
	}
}
void solve(){
	memset(a,0,sizeof(a));memset(top,0,sizeof(top));
	cin>>n>>k;
	map<int,int> mp; 
	for(int i=1;i<=n;i++){
		for(int j=1;j<=k;j++){
			cin>>a[i][j];
			mp[a[i][j]]++;
		} 
		top[i]=k;
	}
	if(k==1){cout<<"TAK"<<endl<<"0"<<endl;return;} 
	int cnt=0;
	for(auto i:mp) cnt+=(i.second+k-1)/k;
	if(cnt>n+2){cout<<"NIE"<<endl;return;} 
	int x=0,y=1;
	for(auto i:mp){
		while(i.second--){
			b[x][y++]=i.first;
			if(y==k+1) x++,y=1;
		}
	}
	vector<pair<int,int> > ans;
	auto op=[&](int x,int y){
		// operator: x->y
		if(!top[x]||top[y]==k||min(x,y)<0||max(x,y)>n+1||abs(x-y)!=1){
			cout<<"Operator Error!"<<endl;
			cout<<"Information: you want put"<<x<<" to "<<y<<"."<<endl;
			cout<<"Size of them is "<<top[x]<<" and "<<top[y]<<"."<<endl;
			exit(0);
		}
		ans.push_back(MP(x,y));
		a[y][++top[y]]=a[x][top[x]--];a[x][top[x]+1]=0;
	};
	for(int i=n;i>=1;i--){
		for(int j=1;j<=k;j++) op(i,i+1);
	}// 空出前两个 
//	debug();
	for(int gx=0;gx<n;gx++){
		for(int gy=1;gy<=k;gy++){
			// 目标:拿到 b[gx][gy]
			int x=0,y=0;
			for(int p=gx+2;p<=n+1;p++) for(int q=1;q<=k;q++)
				if(a[p][q]==b[gx][gy]){
					x=p,y=q;
					goto nxt;
				}
			nxt:; 
			for(int i=gx+1;i<x-1;i++){
				while(top[i+1]) op(i+1,i);
			}
			// 把空柱子挪到 x-1 
//			cout<<"Init: "<<x<<" "<<y<<endl;
			while(x!=gx+2){
				if(y!=1||top[x]!=k){
					// 此时可以直接挪
					while(top[x]+1!=y) op(x,x-1);
					y=top[--x];
					while(top[x-1]){
						op(x-1,x);
						if(top[x+1]!=k) op(x,x+1);
					}
					// 空出前面的柱子 
				}else{
					int pre=x-2;
					while(top[pre]==k) pre--;
					for(int i=pre;i<x-2;i++) op(i,i+1);
					op(x,x-1);op(x-1,x-2);// 挪一个到前面
					while(top[x]) op(x,x-1);
					y=top[--x];
					while(top[x-1]) op(x-1,x),op(x,x+1);
					for(int i=x-2;i>=pre;i--) op(i,i+1);
					while(top[x-1]) op(x-1,x);
				} 
			}
			while(top[x]>=y) op(x,x-1);
			op(x-1,x-2);
			while(top[x-1]) op(x-1,x); 
//			cout<<"After "<<gx<<" "<<gy<<" "<<x<<" "<<y<<endl;
//			debug();
		}
	} 
	int ept=n+1;
	for(int i=n-1;i>=0;i--){
		for(int j=k;j>=1;j--){
			if(top[ept]==0||a[i][j]==a[ept][1]){
				for(int t=i;t<ept;t++) op(t,t+1); 
			}else{
				ept--;
				for(int t=i;t<ept;t++) op(t,t+1); 
			}
		}
	}
	cout<<ans.size()<<endl;
	for(auto i:ans) cout<<i.first<<' '<<i.second<<endl; 
}
int main(){
	ios::sync_with_stdio(false);
	int _;cin>>_;
	while(_--) solve(); 
}

詳細信息

Test #1:

score: 0
Wrong Answer
time: 1ms
memory: 3620kb

input:

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

output:

30
2 3
2 3
1 2
1 2
2 1
1 0
2 1
3 2
2 1
3 2
1 2
2 3
1 2
2 3
2 1
1 0
3 2
3 2
2 1
2 3
3 2
2 1
1 2
2 3
1 2
2 3
0 1
1 2
0 1
1 2
NIE

result:

wrong answer Brak odpowiedzi TAK/NIE