题意:将序列1,2,3,……,n,用不超过2n^2次操作,通过下列操作变成给定序列。(1<=n<=300)
1、交换前两个元素
2、将第一个元素移到最后
分析:因为将序列变成升序更容易操作,所以倒着输出解,进行如下操作:
1、交换前两个元素
2、将最后一个元素移到第一个(当a[0] < a[1] || (a[0] == n && a[1] == 1))
#pragma comment(linker, "/STACK:102400000, 102400000") #include<cstdio> #include<cstring> #include<cstdlib> #include<cctype> #include<cmath> #include<iostream> #include<sstream> #include<iterator> #include<algorithm> #include<string> #include<vector> #include<set> #include<map> #include<stack> #include<deque> #include<queue> #include<list> #define Min(a, b) ((a < b) ? a : b) #define Max(a, b) ((a < b) ? b : a) typedef long long LL; typedef unsigned long long ULL; const int INT_INF = 0x3f3f3f3f; const int INT_M_INF = 0x7f7f7f7f; const LL LL_INF = 0x3f3f3f3f3f3f3f3f; const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f; const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1}; const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1}; const int MOD = 1e9 + 7; const double pi = acos(-1.0); const double eps = 1e-8; const int MAXN = 300 + 10; const int MAXT = 10000 + 10; using namespace std; vector<int> a, ans; int n; bool judge(){ for(int i = 0; i < n; ++i){ if(a[i] != i + 1) return false; } return true; } int main(){ while(scanf("%d", &n) == 1){ if(!n) return 0; a.clear(); ans.clear(); for(int i = 0; i < n; ++i){ int x; scanf("%d", &x); a.push_back(x); } while(1){ if(judge()) break; if(a[0] < a[1] || (a[0] == n && a[1] == 1)){ ans.push_back(2); a.insert(a.begin(), a[n - 1]); a.erase(a.end() - 1); } else{ ans.push_back(1); swap(a[0], a[1]); } } int len = ans.size(); for(int i = len - 1; i >= 0; --i) printf("%d", ans[i]); printf("\n"); } return 0; }