QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#149259 | #5508. Job for a Hobbit | Flamire | WA | 9ms | 6296kb | C++14 | 2.9kb | 2023-08-24 11:14:18 | 2023-08-24 11:14:21 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
int t,n,k,a[55][15],cnt[55],mv[1000011][2],nmv;map<int,int> mp;
bool nd[55][15],mk[55][15];
void move(int x,int y)
{
a[y][++cnt[y]]=a[x][cnt[x]--];
mk[y][cnt[y]]=mk[x][cnt[x]+1];mk[x][cnt[x]+1]=0;
a[x][cnt[x]+1]=0;
++nmv;mv[nmv][0]=x;mv[nmv][1]=y;
}
void MoveL(int x)
{
if(cnt[x-1]==k)MoveL(x-1);
move(x,x-1);
}
void MoveR(int x)
{
if(cnt[x+1]==k)MoveR(x+1);
move(x,x+1);
}
void SingleCleanse(int x)
{
for(int i=2;i<=x;++i)
{
while(cnt[i])
{
if(nd[i][cnt[i]])move(i,i-1);
else move(i,i-1),move(i-1,i-2);
}
while(cnt[i-1])move(i-1,i);
}
}
void FullCleanse(int x)
{
while(cnt[0])MoveR(0);
while(cnt[1])MoveR(1);
int cur=0,all=0;
for(int i=2;i<=x;++i)
{
for(int _=1;_<=cnt[i];++_)if(mk[i][_]&&cur<k-1)++cur,nd[i][_]=1;else nd[i][_]=0;
for(int _=1;_<=cnt[i];++_)if(mk[i][_])++all;
}
SingleCleanse(x);
if(all>=k)
{
while(cnt[0])MoveR(0);
while(cnt[1]>1)MoveR(1);
bool flg=0;
for(int i=2;i<x;++i)
{
for(int _=1;_<=cnt[i];++_)if(mk[i][_]&&!flg)nd[i][_]=flg=1;else nd[i][_]=0;
}
SingleCleanse(x-1);
if(mk[x-1][cnt[x-1]])move(x-1,x);
else
{
while(cnt[x-2]==k)MoveL(x-2);
move(x-1,x-2);move(x-1,x);
}
}
while(cnt[0])MoveR(0);
while(cnt[1])MoveR(1);
memset(mk,0,sizeof(mk));
}
void Sort(int x)
{
if(!cnt[x])return;
int mx=0;
for(int i=1;i<=cnt[x];++i)mx=max(mx,a[x][i]);
while(cnt[x])
{
if(a[x][cnt[x]]==mx)move(x,x-1),move(x-1,x-2);
else move(x,x-1);
}
while(cnt[x-1])move(x-1,x);
Sort(x);
}
int main()
{
scanf("%d",&t);while(t--)
{
scanf("%d%d",&n,&k);mp.clear();cnt[0]=cnt[n+1]=0;
for(int i=1;i<=n;++i)for(int j=1;j<=k;++j)scanf("%d",a[i]+j),++mp[a[i][j]];
if(k==1){printf("TAK\n0\n");continue;}
int ned=0;
for(auto x:mp)ned+=(x.second+k-1)/k;
if(ned>n+2){printf("NIE\n");continue;}
nmv=0;
for(int i=1;i<=n;++i)cnt[i]=k;
int ed=n+1;
for(auto x:mp)
{
for(int _=1;_<=x.second/k;++_)
{
int cur=0;
for(int i=0;i<=ed;++i)
{
for(int j=1;j<=cnt[i];++j)
{
if(cur<n&&a[i][j]==x.first)mk[i][j]=1;else mk[i][j]=0;
}
}
FullCleanse(ed--);
}
}
int ted=ed,cur=0;
for(auto x:mp)
{
bool flg=1;
while(flg)
{
flg=0;
for(int i=0;i<=ed;++i)
{
for(int j=1;j<=cnt[i];++j)
{
if(a[i][j]==x.first)
{
++cur;mk[i][j]=1;
if(cur==k){flg=1;cur=0;FullCleanse(ed--);break;}
}
}
if(flg)break;
}
}
}
if(cur)FullCleanse(ed--);
++ed;
for(int i=2;i<=ted;++i)Sort(i);
int lst=-1,lstp=ted+1;
for(int i=ted-2;~i;--i)
{
while(cnt[i]&&lstp!=i)
{
while(a[i][cnt[i]]==lst)for(int _=i;_<lstp;++_)move(_,_+1);
if(cnt[i])lst=a[i][cnt[i]],--lstp;
}
}
printf("TAK\n%d\n",nmv);
for(int i=1;i<=nmv;++i)printf("%d %d\n",mv[i][0],mv[i][1]);
}return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3896kb
input:
2 2 2 1 2 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 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 99587 50 51 49 50 48 49 47 48 46 47 45 46 44 45 43 44 42 43 41 42 40 41 39 40 38 39 37 38 36 37 35 36 34 35 33 34 32 33 31 32 30 31 29 30 28 29 27 28 26 27 25 26 24 25 23 24 22 23 21 22 20 21 19 20 18 19 17 18 16 17 15 16 14 15 13 14 12 13 11 12 10 11 9 10 8 9 7 8 6 7 5 6 4 5 3 4 2 3 1 2 50 51 4...
result:
ok Correct! Used 99587 swaps
Test #4:
score: 0
Accepted
time: 9ms
memory: 4744kb
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 100123 50 51 49 50 48 49 47 48 46 47 45 46 44 45 43 44 42 43 41 42 40 41 39 40 38 39 37 38 36 37 35 36 34 35 33 34 32 33 31 32 30 31 29 30 28 29 27 28 26 27 25 26 24 25 23 24 22 23 21 22 20 21 19 20 18 19 17 18 16 17 15 16 14 15 13 14 12 13 11 12 10 11 9 10 8 9 7 8 6 7 5 6 4 5 3 4 2 3 1 2 50 51 ...
result:
ok Correct! Used 100123 swaps
Test #5:
score: -100
Wrong Answer
time: 2ms
memory: 3880kb
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 2907 10 11 9 10 8 9 7 8 6 7 5 6 4 5 3 4 2 3 1 2 10 11 9 10 8 9 7 8 6 7 5 6 4 5 3 4 2 3 1 2 10 11 9 10 8 9 7 8 6 7 5 6 4 5 3 4 2 3 1 2 10 11 9 10 8 9 7 8 6 7 5 6 4 5 3 4 2 3 1 2 10 11 9 10 8 9 7 8 6 7 5 6 4 5 3 4 2 3 1 2 10 11 9 10 8 9 7 8 6 7 5 6 4 5 3 4 2 3 1 2 10 11 9 10 8 9 7 8 6 7 5 6 ...
result:
wrong answer Trying to perform move: 104, (5 -> 4), destination pillar full