2 solutions

  • -4
    @ 2024-2-6 19:32:51
    #include<iostream>
    #include<cstdio>
    using namespace std;
    const int N = 1010, M = 2e6 + 10;
    int n, A[N];
    int T, B[M];
    int fst, lst, pre[N], nex[N], r;
    int getlst(){
    	if(r == 0) return lst;
    	return fst;
    }
    int getfst(){
    	if(r == 0) return fst;
    	return lst;
    }
    int getsec(){
    	if(r == 0) return nex[fst];
    	return pre[lst];
    }
    int getnex(int x){
    	if(r == 0) return nex[x];
    	return pre[x];
    }
    void opt1(){
    	B[++T] = 1;
    	r ^= 1;
    }
    void opt2(){
    	B[++T] = 2;
    	int x = getsec(), l = getlst(), f = getfst(), y = getnex(x);
    	if(r == 0){
    		pre[x] = l;
    		nex[l] = x;
    		pre[y] = f;
    		nex[f] = y;
    		lst = x;
    	}else{
    		nex[x] = l;
    		pre[l] = x;
    		nex[y] = f;
    		pre[f] = y;
    		fst = x;
    	}
    }
    void opt3(){
    	B[++T] = 3;
    	int x = getsec(), l = getlst(), f = getfst();
    	if(r == 0){
    		pre[f] = l;
    		nex[l] = f;
    		pre[x] = 0;
    		nex[f] = 0;
    		lst = f;
    		fst = x;
    	}else{
    		nex[f] = l;
    		pre[l] = f;
    		nex[x] = 0;
    		pre[f] = 0;
    		lst = x;
    		fst = f;
    	}
    }
    void print(){
    	printf("\n");
    	int u = getfst();
    	while(u != getlst()){
    		printf("%d ",u);
    		u = getnex(u);
    	}
    	printf("%d\n",getlst());
    }
    void init(){
    	for(int i = 1; i <= n; i += 1){
    		pre[i] = i - 1;
    		if(i != n) nex[i] = i + 1;
    	}
    	fst = 1, lst = n;
    }
    int main(){
    	scanf("%d",&n);
    	for(int i = 1; i <= n; i += 1)
    		scanf("%d",&A[i]);
    	init();
    	if(A[1] == 1) opt1();
    	else while(getlst() != A[1]) opt2();
    	for(int i = 2; i < n - 1; i += 1){
    		if(getfst() == A[i]){
    			opt2();
    			opt1();
    			while(getlst() != A[1]) opt2();
    			if(i == n - 2){
    				opt1();
    				break;
    			}else{
    				opt2();
    				opt1();
    				while(getlst() != A[i]) opt2();
    			}
    		}else{
    			while(getlst() != A[i]) opt2();
    			opt1();
    			while(getsec() != A[i - 1]) opt2();
    			opt1();
    		}
    		//print();
    	}
    	if(getfst() == A[n]){
    		opt2();
    		opt3();
    	}else if(getfst() != A[1]){
    		opt2();
    		opt1();
    		while(getlst() != A[1]) opt2();
    		opt1();
    	}
    	printf("%d\n",T);
    	for(int i = 1; i <= T; i += 1)
    		printf("%d ",B[i]);
    	//print();
    	return 0;
    }
    

    Information

    ID
    310
    Time
    1000ms
    Memory
    256MiB
    Difficulty
    10
    Tags
    (None)
    # Submissions
    25
    Accepted
    0
    Uploaded By