답안 #722763

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
722763 2023-04-12T20:07:21 Z NemanjaSo2005 Xor Sort (eJOI20_xorsort) C++14
65 / 100
1000 ms 4280 KB
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll N,S,niz[1005],pniz[205],niz2[205];
bool pncmp(int a,int b){
   return niz[a]<niz[b];
}
struct slog{
   int x,y;
}pp;
struct elem{
   bool bit[205];
   elem operator ^ (const elem &a){
      elem r;
      for(int i=1;i<=N;i++)
         r.bit[i]=a.bit[i]^bit[i];
      return r;
   }
} sniz[205],zniz[205];
queue<slog> Q;
void dodaj(int a,int b){
   pp.x=a;
   pp.y=b;
   Q.push(pp);
}
void ispis(){
   cout<<Q.size()<<endl;
   while(Q.size()){
      pp=Q.front();
      Q.pop();
      cout<<pp.x<<" "<<pp.y<<endl;
   }
   exit(0);
}
void uradi(int a,int b){
   sniz[a]=sniz[a]^sniz[b];
   dodaj(a,b);
   bool sortiran=true;
   for(int i=2;i<=N;i++)
      if(niz[i-1]>=niz[i])
         sortiran=false;
   if(sortiran and Q.size()<=40000)
      ispis();
}
void stavi1(int gde,elem sta){
   bool isti=true;
   for(int i=1;i<=N;i++)
      if(sniz[gde].bit[i]!=sta.bit[i])
         isti=false;
   if(isti)
      return;
   if(sta.bit[gde-1]==sniz[gde].bit[gde-1])
      uradi(gde,gde-1);

   isti=true;
   for(int i=1;i<=N;i++)
      if(sniz[gde].bit[i]!=sta.bit[i])
         isti=false;
   if(isti)
      return;

   elem pret;

   for(int i=1;i<gde;i++)
      pret.bit[i]=sta.bit[i]^sniz[gde].bit[i];
   for(int i=gde;i<=N;i++)
      pret.bit[i]=0;
   stavi1(gde-1,pret);
   uradi(gde,gde-1);
   return;
}
void stavi2(int gde,elem sta){
   bool isti=true;
   for(int i=1;i<=N;i++)
      if(sniz[gde].bit[i]!=sta.bit[i])
         isti=false;
   if(isti)
      return;
   if(sta.bit[gde+1]==sniz[gde].bit[gde+1])
      uradi(gde,gde+1);

   isti=true;
   for(int i=1;i<=N;i++)
      if(sniz[gde].bit[i]!=sta.bit[i])
         isti=false;
   if(isti)
      return;

   elem pret;

   for(int i=gde+1;i<=N;i++)
      pret.bit[i]=sta.bit[i]^sniz[gde].bit[i];
   for(int i=gde;i>=1;i--)
      pret.bit[i]=0;
   stavi2(gde+1,pret);
   uradi(gde,gde+1);
   return;
}
void pisibit(){
   for(int i=N;i>=1;i--){
      for(int j=1;j<=N;j++)
         cout<<sniz[j].bit[i]<<" ";
      cout<<endl;
   }
}
int main(){
 //  freopen("Input.txt","r",stdin);
   cin>>N>>S;
   for(int i=1;i<=N;i++)
      cin>>niz[i];
   ll MaxO=40000;
   if(S==1){
      for(int i=1;i<=N;i++)
         niz2[i]=niz[i];
      for(int i=1;i<=N;i++)
         for(int j=1;j<N;j++)
            if(niz2[j]>niz2[j+1]){
               swap(niz2[j+1],niz2[j]);
               dodaj(j,j+1);
               dodaj(j+1,j);
               dodaj(j,j+1);
            }
      if(Q.size()<=MaxO)
         ispis();
      while(Q.size())
         Q.pop();
      for(int mbit=0;mbit<=N;mbit++){
         for(int i=1;i<=N;i++)
            pniz[i]=i;
         sort(pniz+1,pniz+1+N,pncmp);
         for(int i=1;i<=N;i++){
            sniz[pniz[i]].bit[i]=1;
            zniz[i].bit[i]=1;
         }
         for(int i=1;i<=N;i++)
            niz2[pniz[i]]=i;
         for(int i=1;i<=N;i++)
            for(int j=i;j<=N;j++){
               if(niz2[j]!=i)
                  continue;
               for(int k=j;k>i;k--){
                  swap(niz2[k],niz2[k-1]);
                  uradi(k,k-1);
                  uradi(k-1,k);
                  if(i<=mbit)
                     uradi(k,k-1);
               }
               break;
            }
         pisibit();
         for(int i=N;i>=2;i--)
            stavi1(i,zniz[i]);

         if(Q.size()<=MaxO)
            ispis();
         while(Q.size())
            Q.pop();
      }
      for(int mbit=1;mbit<=N+1;mbit++){
         for(int i=1;i<=N;i++)
            pniz[i]=i;
         sort(pniz+1,pniz+1+N,pncmp);
         for(int i=1;i<=N;i++){
            sniz[pniz[i]].bit[i]=1;
            zniz[i].bit[i]=1;
         }
         for(int i=1;i<=N;i++)
            niz2[pniz[i]]=i;
         for(int i=1;i<=N;i++)
            for(int j=i;j<=N;j++){
               if(niz2[j]!=i)
                  continue;
               for(int k=j;k>i;k--){
                  swap(niz2[k],niz2[k-1]);
                  uradi(k,k-1);
                  uradi(k-1,k);
                  if(i>=mbit)
                     uradi(k,k-1);
               }
               break;
            }
         pisibit();
         for(int i=N;i>=2;i--)
            stavi1(i,zniz[i]);

         if(Q.size()<=MaxO)
            ispis();
         while(Q.size())
            Q.pop();
      }


      for(int mbit=0;mbit<=N;mbit++){
         for(int i=1;i<=N;i++)
            pniz[i]=i;
         sort(pniz+1,pniz+1+N,pncmp);
         for(int i=1;i<=N;i++){
            sniz[pniz[i]].bit[i]=1;
            zniz[i].bit[i]=1;
         }
         for(int i=1;i<=N;i++)
            niz2[pniz[i]]=i;
         for(int i=1;i<=N;i++)
            for(int j=i;j<=N;j++){
               if(niz2[j]!=i)
                  continue;
               for(int k=j;k>i;k--){
                  swap(niz2[k],niz2[k-1]);
                  uradi(k-1,k);
                  uradi(k,k-1);
                  if(i<=mbit)
                     uradi(k-1,k);
               }
               break;
            }

         //pisibit();

         for(int i=1;i<=N;i++)
            stavi2(i,zniz[i]);

        // pisibit();
         if(Q.size()<=MaxO)
            ispis();
      }

      for(int mbit=1;mbit<=N+1;mbit++){
         for(int i=1;i<=N;i++)
            pniz[i]=i;
         sort(pniz+1,pniz+1+N,pncmp);
         for(int i=1;i<=N;i++){
            sniz[pniz[i]].bit[i]=1;
            zniz[i].bit[i]=1;
         }
         for(int i=1;i<=N;i++)
            niz2[pniz[i]]=i;
         for(int i=1;i<=N;i++)
            for(int j=i;j<=N;j++){
               if(niz2[j]!=i)
                  continue;
               for(int k=j;k>i;k--){
                  swap(niz2[k],niz2[k-1]);
                  uradi(k-1,k);
                  uradi(k,k-1);
                  if(i>=mbit)
                     uradi(k-1,k);
               }
               break;
            }

         //pisibit();

         for(int i=1;i<=N;i++)
            stavi2(i,zniz[i]);

        // pisibit();
         if(Q.size()<=MaxO)
            ispis();
      }
      return -1;
   }
   for(int step=(1<<19);step>=1;step/=2){
      bool nemoj=true;
      for(int i=1;i<=N;i++)
         if(niz[i]&step)
            nemoj=false;
      if(nemoj)
         continue;
      for(int i=1;i<N;i++)
         if((niz[i]&step)>0 and (niz[i+1]&step)==0){
            niz[i+1]^=niz[i];
            dodaj(i+1,i);
         }
      for(int i=1;i<N;i++){
         if(niz[i]&step){
            niz[i]^=niz[i+1];
            dodaj(i,i+1);
         }
      }
      N--;
   }
   ispis();
   return 0;
}

Compilation message

xorsort.cpp: In function 'int main()':
xorsort.cpp:123:18: warning: comparison of integer expressions of different signedness: 'std::queue<slog>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
  123 |       if(Q.size()<=MaxO)
      |          ~~~~~~~~^~~~~~
xorsort.cpp:154:21: warning: comparison of integer expressions of different signedness: 'std::queue<slog>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
  154 |          if(Q.size()<=MaxO)
      |             ~~~~~~~~^~~~~~
xorsort.cpp:186:21: warning: comparison of integer expressions of different signedness: 'std::queue<slog>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
  186 |          if(Q.size()<=MaxO)
      |             ~~~~~~~~^~~~~~
xorsort.cpp:223:21: warning: comparison of integer expressions of different signedness: 'std::queue<slog>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
  223 |          if(Q.size()<=MaxO)
      |             ~~~~~~~~^~~~~~
xorsort.cpp:257:21: warning: comparison of integer expressions of different signedness: 'std::queue<slog>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
  257 |          if(Q.size()<=MaxO)
      |             ~~~~~~~~^~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 4 ms 340 KB Output is correct
4 Correct 19 ms 480 KB Output is correct
5 Correct 24 ms 596 KB Output is correct
6 Correct 23 ms 504 KB Output is correct
7 Correct 22 ms 468 KB Output is correct
8 Correct 22 ms 536 KB Output is correct
9 Correct 24 ms 560 KB Output is correct
10 Correct 23 ms 508 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 43 ms 716 KB Output is correct
13 Correct 43 ms 716 KB Output is correct
14 Correct 42 ms 716 KB Output is correct
15 Correct 47 ms 756 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 4 ms 340 KB Output is correct
4 Correct 19 ms 480 KB Output is correct
5 Correct 24 ms 596 KB Output is correct
6 Correct 23 ms 504 KB Output is correct
7 Correct 22 ms 468 KB Output is correct
8 Correct 22 ms 536 KB Output is correct
9 Correct 24 ms 560 KB Output is correct
10 Correct 23 ms 508 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 43 ms 716 KB Output is correct
13 Correct 43 ms 716 KB Output is correct
14 Correct 42 ms 716 KB Output is correct
15 Correct 47 ms 756 KB Output is correct
16 Correct 1 ms 308 KB Output is correct
17 Correct 24 ms 524 KB Output is correct
18 Correct 39 ms 696 KB Output is correct
19 Correct 39 ms 716 KB Output is correct
20 Correct 44 ms 732 KB Output is correct
21 Correct 38 ms 724 KB Output is correct
22 Correct 40 ms 716 KB Output is correct
23 Correct 39 ms 732 KB Output is correct
24 Correct 36 ms 740 KB Output is correct
25 Correct 37 ms 740 KB Output is correct
26 Execution timed out 1092 ms 4280 KB Time limit exceeded
27 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 2 ms 212 KB Output is correct
4 Correct 11 ms 340 KB Output is correct
5 Correct 41 ms 740 KB Output is correct
6 Correct 41 ms 784 KB Output is correct
7 Correct 47 ms 772 KB Output is correct
8 Correct 39 ms 700 KB Output is correct
9 Correct 39 ms 776 KB Output is correct
10 Correct 38 ms 716 KB Output is correct
11 Correct 39 ms 776 KB Output is correct
12 Correct 41 ms 784 KB Output is correct
13 Correct 41 ms 760 KB Output is correct
14 Correct 43 ms 816 KB Output is correct
15 Correct 51 ms 892 KB Output is correct