UVa11925 Generating Premutations

留坑(p.254)

 

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<cstdlib>
 4 #include<algorithm>
 5 #include<iostream>
 6 
 7 using namespace std;
 8 
 9 void setIO(const string& s) {
10     freopen((s + ".in").c_str(), "r", stdin);
11     freopen((s + ".out").c_str(), "w", stdout);
12 }
13 template<typename Q> Q read(Q& x) {
14     static char c, f;
15     for(f = 0; c = getchar(), !isdigit(c); ) if(c == '-') f = 1;
16     for(x = 0; isdigit(c); c = getchar()) x = x * 10 + c - '0';
17     if(f) x = -x;
18     return x;
19 }
20 template<typename Q> Q read() {
21     static Q x; read(x); return x;
22 }
23 
24 const int N = 300 + 10;
25 
26 #include<vector>
27 vector<int> a, b;
28 
29 //#define dprintf(s, arg...) fprintf(stderr, s, ##arg)
30 
31 void p1() {
32     swap(a[0], a[1]);
33     putchar('1');
34 //    dprintf("1:");
35 //    for(unsigned i = 0; i < a.size(); i++) {
36 //        dprintf("%d ", a[i]);
37 //    }
38 //    dprintf("\n");
39 }
40 
41 void p2() {
42     int t = a[0];
43     a.erase(a.begin());
44     a.push_back(t);
45     putchar('2');
46 //    dprintf("2:");
47 //    for(unsigned i = 0; i < a.size(); i++) {
48 //        dprintf("%d ", a[i]);
49 //    }
50 //    dprintf("\n");
51 }
52 
53 bool ok() {
54     for(unsigned i = 0; i < a.size(); i++) {
55         if(a[i] ^ b[i]) return 0;
56     }
57     return 1;
58 }
59 
60 int main() {
61 #ifdef DEBUG
62     freopen("in.txt", "r", stdin);
63     freopen("out.txt", "w", stdout);
64 #endif
65     
66     int n;
67     while(read(n)) {
68         a.clear(), b.clear();
69         for(int i = 1; i <= n; i++) {
70             a.push_back(i);
71             b.push_back(read<int>());
72         }
73         if(a[1] == b[0]) p1();
74         for(int i = 0; i < n; i++) {//½«µÚi¸öÒƵ½×îºó 
75             while(a[0] != b[i]) p2();
76             if(ok()) break;
77             if(i != n-1) while(a[1] != b[i+1]) p1(), p2();
78             p2();
79         }
80         putchar('\n');
81 //        dprintf("\n");
82     }
83     
84     return 0;
85 }
View Code

 

posted @ 2016-01-03 21:16  Showson  阅读(131)  评论(0编辑  收藏  举报