UVa11925 Generating Premutations
2016-01-03 21:16
260 查看
留坑(p.254)
View Code
#include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<iostream> using namespace std; void setIO(const string& s) { freopen((s + ".in").c_str(), "r", stdin); freopen((s + ".out").c_str(), "w", stdout); } template<typename Q> Q read(Q& x) { static char c, f; for(f = 0; c = getchar(), !isdigit(c); ) if(c == '-') f = 1; for(x = 0; isdigit(c); c = getchar()) x = x * 10 + c - '0'; if(f) x = -x; return x; } template<typename Q> Q read() { static Q x; read(x); return x; } const int N = 300 + 10; #include<vector> vector<int> a, b; //#define dprintf(s, arg...) fprintf(stderr, s, ##arg) void p1() { swap(a[0], a[1]); putchar('1'); // dprintf("1:"); // for(unsigned i = 0; i < a.size(); i++) { // dprintf("%d ", a[i]); // } // dprintf("\n"); } void p2() { int t = a[0]; a.erase(a.begin()); a.push_back(t); putchar('2'); // dprintf("2:"); // for(unsigned i = 0; i < a.size(); i++) { // dprintf("%d ", a[i]); // } // dprintf("\n"); } bool ok() { for(unsigned i = 0; i < a.size(); i++) { if(a[i] ^ b[i]) return 0; } return 1; } int main() { #ifdef DEBUG freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); #endif int n; while(read(n)) { a.clear(), b.clear(); for(int i = 1; i <= n; i++) { a.push_back(i); b.push_back(read<int>()); } if(a[1] == b[0]) p1(); for(int i = 0; i < n; i++) {//½«µÚi¸öÒƵ½×îºó while(a[0] != b[i]) p2(); if(ok()) break; if(i != n-1) while(a[1] != b[i+1]) p1(), p2(); p2(); } putchar('\n'); // dprintf("\n"); } return 0; }
View Code
相关文章推荐
- Storm实时计算框架的编程模式
- vector内存机制和性能分析
- Thrift连接池实现
- 网卡重启后,如何恢复MHA的虚IP
- CSAPP里面对浮点型的描述
- java19.IO操作------使用字符流读写数据
- netty5 NioEventLoopGroup线程池简析
- Thrift连接池实现
- yum更新源优先级设置
- SmartUpload乱码问题
- vim+ycm+eclim+javacomplete解决冲突
- IOS 3D touch的使用
- 为什么别人不感激你的付出?
- 翻开新的篇章 - 启程
- 折半插入排序(Binary Insertion Sort)的C语言实现
- 错位重排
- mysql的一个基础查询
- mysql cluster (mysql 集群)安装配置方案(转)
- yum局域网软件源搭建
- 优秀的架构师应该具备哪些能力