Generating Permutations UVA
来源:互联网 发布:nba2konline作弊软件 编辑:程序博客网 时间:2024/05/23 01:59
题目传送门
题意:这个题意紫书描述出了一些问题,题意是给你两种操作,让你从原来的严格升序列变成现在的序列。
操作一:交换最前面的两个数字。
操作二:把最后一个数字放到最前面。
思路:这个题要逆向思维,然后就可以用冒泡的思想。但是一开始这个样子一直都是死循环,后来发现要把首位是n的情况特判。
#include <algorithm>#include <cmath>#include <cstdio>#include <cstring>#include <ctime>#include <fstream>#include <iostream>#include <list>#include <map>#include <queue>#include <set>#include <sstream>#include <stack>#include <string>#include <vector>#define MAXN 1010#define MAXE 210#define INF 10000000#define MOD 1000000007#define LL long long#define PI acos(-1.0)using namespace std;int main() { std::ios::sync_with_stdio(false); int n; while (cin >> n && n) { deque<int> que; int arr[MAXN]; for (int i = 0; i < n; ++i) { cin >> arr[i]; } for (int i = n - 1; i >= 0; --i) { que.insert(que.begin(), arr[i]); } if (n == 1) { cout << endl; } else { vector<int> vec; while (true) { bool flag = true; for (int i = 0; i < n; ++i) { if (que[i] != i + 1) { flag = false; break; } } if (flag) { break; } if (que[0] == n) { int temp = que[n - 1]; que.pop_back(); que.push_front(temp); vec.push_back(2); } else { if (que[0] > que[1]) { swap(que[0], que[1]); vec.push_back(1); } else { int temp = que[n - 1]; que.pop_back(); que.push_front(temp); vec.push_back(2); } } } for (int i = vec.size() - 1; i >= 0; --i) { cout << vec[i]; } cout << endl; } } return 0;}/*3 2 1 33 2 3 14 4 2 3 10*/
阅读全文
0 0
- UVa:11925 Generating Permutations
- UVa 11925 - Generating Permutations
- UVa 11925 Generating Permutations
- UVa - 11925 - Generating Permutations
- UVA 11925 Generating Permutations
- UVA 11925 - Generating Permutations
- Generating Permutations UVA
- UVA - 11925 Generating Permutations 推理
- UVA 11925(p254)----Generating Permutations
- UVa #11925 Generating Permutations (习题8-7)
- UVa 11925 - Generating Permutations(构造法)
- UVa 11925:Generating Permutations(构造)
- uva 11925——Generating Permutations
- UVA 11925 Generating Permutations 逆向思维
- 11925 - Generating Permutations
- 生成排列 Generating Permutations
- 习题8-7 UVA - 11925 Generating Permutations 生成排列(构造)
- UVa 10098 Generating Fast
- 使用maven命令将第三方jar包导入maven库
- android之控件的可视性
- 游戏常用到的sdk及接口方法文档
- 处理sql server的死锁 [kill spid]
- Cheapest Palindrome POJ
- Generating Permutations UVA
- REM手机屏幕适配
- 论文阅读实践
- memcpy与memset
- Javaweb核心之session&cookie
- Irrlicht 源码学习笔记 【irrstring.h】
- vim的ex模式用法
- 《数学之美》—— 读后总结
- 一切从现在开始