답안 #285820

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
285820 2020-08-29T16:02:54 Z stefanbalaz2 Shift (POI11_prz) C++14
0 / 100
175 ms 20516 KB
/*

    idea:

*/
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define ff first
#define ss second
#define ll long long
typedef pair<int,int> pii;
typedef pair<int,char> pic;
const int maxn=2010;
vector<pic>rez;
int n,cp,a[maxn],pos[maxn];
void rotation3(int x){

   /// printf("%d ROTATE\n",x);

    int id[3]={x%n,(x+1)%n,(x+2)%n};

    int pom=a[id[0]];
    int p0=a[id[0]];
    int p1=a[id[1]];
    int p2=a[id[2]];

    a[id[0]]=p2;
    pos[p2]=id[0];

    a[id[2]]=p1;
    pos[p1]=id[2];

    a[id[1]]=p0;
    pos[p0]=id[1];

}
int dist(int x,int y){
    if(x<y)return y-x;
    else return n-x+y;
}
void rot3(int x,int val){
    if(cp!=x){
        rez.pb({dist(cp,x),'a'});
        cp=x;
    }
    rez.pb({val,'b'});
    for(int i=1;i<=val;i++)rotation3(x);
}
int inversions(){

    int ret=0;

    for(int i=0;i<n-1;i++)
        for(int j=i+1;j<n;j++)
            if(a[i]>a[j])ret++;

    return ret;
}
bool isNO(){

    if(n%2==0)return false;

    if(inversions()%2==1)return true;

    return false;
}
int find_num(int x){
    return pos[x];
}
void ispis(){
    for(int i=0;i<n;i++)printf("%d %d |",a[i],pos[a[i]]);
    printf("   >>\n\n");
}
void go(){

    for(int i=2;i<=n;i++){
        ///printf("%d III\n",i);
        ///ispis();
        if(find_num(i)==(find_num(i-1)+1)%n)continue;

        ///printf("%d III\n",i);
        ///ispis();

        if(i<=n-2){

            while((pos[i-1]+1)%n!=pos[i] && (pos[i-1]+2)%n!=pos[i])
                rot3((pos[i]-2+n)%n,1);

            if((pos[i-1]+1)%n==pos[i])continue;
            else rot3((pos[i]-1+n)%n,2);
        }
        else{

            if(n%2==0){

                while((pos[i-1]+1)%n!=pos[i]){
                    ///printf("%d %d AAA\n",(pos[i-1]+1)%n,pos[i]);
                    ///ispis();
                    rot3((pos[i]-2+n)%n,1);
                }

            }
            else return;
        }

        ///printf("%d III\n",i);
        ///ispis();
    }

}
int main(){

    ///freopen("test.txt","r",stdin);
    ///freopen("out.txt","w",stdout);

    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d",&a[i]);
        pos[a[i]]=i;
    }

    if(n==1){
        printf("%d\n",0);
        return 0;
    }
    if(n==2){
        if(a[0]==1)printf("%d\n",0);
        else printf("1\n1a\n");
        return 0;
    }

    if(isNO()){
        printf("NIE DA SIE\n");
        return 0;
    }

    go();

    if(cp!=pos[1])rez.pb({dist(cp,pos[1]),'a'});

    vector<pic>rez2;
    if(rez.size()>0)rez2.pb(rez[0]);
    for(int i=1;i<rez.size();i++)
        if(rez[i].ss==rez2.back().ss)rez2[rez2.size()-1].ff+=rez[i].ff;
        else rez2.pb(rez[i]);

    printf("%d\n",rez.size());
    for(int i=0;i<rez.size();i++){
        printf("%d%c ",rez[i].ff,rez[i].ss);
    }

    return 0;
}

Compilation message

prz.cpp: In function 'void rotation3(int)':
prz.cpp:23:9: warning: unused variable 'pom' [-Wunused-variable]
   23 |     int pom=a[id[0]];
      |         ^~~
prz.cpp: In function 'int main()':
prz.cpp:144:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, char> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  144 |     for(int i=1;i<rez.size();i++)
      |                 ~^~~~~~~~~~~
prz.cpp:148:14: warning: format '%d' expects argument of type 'int', but argument 2 has type 'std::vector<std::pair<int, char> >::size_type' {aka 'long unsigned int'} [-Wformat=]
  148 |     printf("%d\n",rez.size());
      |             ~^    ~~~~~~~~~~
      |              |            |
      |              int          std::vector<std::pair<int, char> >::size_type {aka long unsigned int}
      |             %ld
prz.cpp:149:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, char> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  149 |     for(int i=0;i<rez.size();i++){
      |                 ~^~~~~~~~~~~
prz.cpp:117:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  117 |     scanf("%d",&n);
      |     ~~~~~^~~~~~~~~
prz.cpp:119:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  119 |         scanf("%d",&a[i]);
      |         ~~~~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 256 KB Output is correct
2 Incorrect 0 ms 384 KB Litery stowarzyszone z liczbami musza wystepowac naprzemiennie
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 256 KB Liczby nie zostaly posortowane
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 384 KB Litery stowarzyszone z liczbami musza wystepowac naprzemiennie
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 640 KB Liczby nie zostaly posortowane
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 12 ms 1784 KB Liczby nie zostaly posortowane
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 124 ms 17640 KB Liczby nie zostaly posortowane
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 144 ms 18904 KB Litery stowarzyszone z liczbami musza wystepowac naprzemiennie
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 175 ms 20516 KB Liczby nie zostaly posortowane
2 Halted 0 ms 0 KB -