QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#305259 | #5508. Job for a Hobbit | ushg8877 | WA | 1ms | 3812kb | C++14 | 2.9kb | 2024-01-14 23:42:16 | 2024-01-14 23:42:16 |
Judging History
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;}
cout<<"TAK"<<endl;
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++){
while(top[gx+1]){
for(int j=n;j>=gx+1;j--) while(top[j]&&top[j+1]+1<=k) op(j,j+1);
}
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;
}
// cout<<"Want "<<b[gx][gy]<<endl;
nxt:;
for(int i=gx+1;i<x-1;i++){
while(top[i+1]) op(i+1,i);
}
// 把空柱子挪到 x-1
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();cout<<endl;
}
}
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();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3812kb
input:
2 2 2 1 2 2 1 1 4 1 2 3 4
output:
TAK 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:
ok Correct! Used 30 swaps
Test #2:
score: 0
Accepted
time: 0ms
memory: 3624kb
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 3 2 4 5 1 2 2 2 4 3 2 2 7 1 2 4 5 2 5 2 4 5 5 ...
output:
NIE NIE TAK 169 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 1 0 1 2 1 2 1 2 1 2 1 2 1 2 1 2 2 1 2 1 2 1 2 1 2 1 2 1 1 0 1 2 1 2 1 2 1 2 1 2 2 1 2 1 2 1 2 1 1 0 1 2 1 2 1 2 2 1 1 0 2 1 2 1 2 1 2 1 3 2 3 2 3 2 3 2 3 2 3 2 3 2 1 2 2 3 1 2 2 3 1 2 2 3 ...
result:
ok Correct! Used 1487 swaps
Test #3:
score: -100
Wrong Answer
time: 0ms
memory: 3640kb
input:
1 50 10 2 2 2 2 2 1 2 1 1 2 2 1 2 1 1 2 2 2 2 2 2 1 1 2 2 2 2 1 1 1 2 2 1 2 2 2 1 1 1 2 2 1 1 2 1 2 2 1 2 1 2 1 2 1 1 1 2 1 2 2 1 2 1 1 2 2 1 1 2 1 2 2 1 1 2 2 2 1 1 2 1 2 2 2 2 1 1 2 1 1 2 2 2 1 2 1 1 2 1 1 2 2 1 2 2 1 1 1 1 1 1 2 2 1 2 2 2 1 1 1 2 2 2 1 2 2 1 1 2 2 1 2 1 2 1 1 1 1 2 2 1 2 1 1 2 2 ...
output:
TAK Operator Error! Information: you want put1 to 2. Size of them is 4 and 10.
result:
wrong output format Expected integer, but "Operator" found