UVA11925-Generating Permutations(贪心)
Accept: 214 Submit: 1429
Time Limit: 1000 mSec
Problem Description
A permutation on the integers from 1 to n is, simply put, a particular rearrangement of these integers. Your task is to generate a given permutation from the initial arrangement 1,2,3,...,n using only two simple operations.
• Operation 1: You may swap the first two numbers. For example, this would change the arrangement 3,2,4,5,1 to 2,3,4,5,1.
• Operation 2: You may move the first number to the end of the arrangement. For example, this would change the arrangement 3,2,4,5,1 to 2,4,5,1,3.
Input
The input consists of a number of test cases. Each test case begins with a single integer n between 1 and 300. On the same line, a permutation of integers 1 through n is given where consecutive integers are separated by a single space. Input is terminated by a line containing ‘0’ which should not be processed.
Output
Sample Input
3 2 3 1
4 4 2 3 1
0
Sample Output
1
2
12122
题解:这个题首先应该转换一下思维,考虑将给定串排成升序而不是将排好序的串变成给定串,这样会好想很多,注意如果这样思考的话,1操作就变成把最后一个数移到最前面,2操作不受影响。排序就是一个消除逆序对的过程,所以如果前面两个数是满足第一个数大于第二个数,那就要通过交换来消除这个逆序对(这样操作次数少),这里有个特殊情况就是第一个数是n并且第二个数是1,这时虽然构成逆序,但是是有可能通过把后面的数移到前面而使序列有序的,所以这时不要交换。
#include <bits/stdc++.h> using namespace std; int n;
deque<int> seq;
string ans; bool check() {
for (int i = ; i < n; i++) {
if (seq[i] != i + ) return false;
}
return true;
} int main()
{
//freopen("input.txt", "r", stdin);
while (~scanf("%d", &n) && n) {
seq.clear();
ans = "";
int x;
for (int i = ; i < n; i++) {
scanf("%d", &x);
seq.push_back(x);
} while (true) {
if (seq[] == && check()) {
break;
}
if (seq[] < seq[] || seq[] == n && seq[] == ) {
seq.push_front(seq[n - ]);
seq.pop_back();
ans += '';
}
else {
swap(seq[], seq[]);
ans += '';
}
}
reverse(ans.begin(), ans.end());
cout << ans << endl;
}
return ;
}
UVA11925-Generating Permutations(贪心)的更多相关文章
- UVA-11925 Generating Permutations (逆向思维)
题目大意:给出1~n的某个排列,问由升序变到这个排列最少需要几次操作.操作1:将头两个数交换:操作2:将头一个数移动最后一个位置. 题目分析:反过来考虑,将这个排列变为升序排列,那么这个变化过程实际上 ...
- uva11925 Generating Permutations
逆序做,逆序输出 紫书上的描述有点问题 感觉很经典 ans.push_back(2); a.insert(a.begin(),a[n-1]); a.erase(a.end()-1); a.push_b ...
- CF722D. Generating Sets[贪心 STL]
D. Generating Sets time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- hdu5338 ZZX and Permutations(贪心、线段树)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud ZZX and Permutations Time Limit: 6000/300 ...
- Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) D. Generating Sets 贪心
D. Generating Sets 题目连接: http://codeforces.com/contest/722/problem/D Description You are given a set ...
- hdu 5338 ZZX and Permutations (贪心+线段树+二分)
ZZX and Permutations Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/O ...
- Generating Sets 贪心
H - Generating Sets Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64 ...
- Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) D. Generating Sets 贪心+优先队列
D. Generating Sets time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- UVa11925 Generating Premutations
留坑(p.254) #include<cstdio> #include<cstring> #include<cstdlib> #include<algorit ...
- UVA 11925 - Generating Permutations
题意: 给出一个1到n的排列,给出操作顺序,使升序排列能变为所给排列. 分析: 正常冒泡排序的想法.如果前两个数,前面的大于后面的,则换(特例是n,1不能换).否则,就用2的逆操作,把最后的数放前面. ...
随机推荐
- nginx 转将http跳转到https
#websoceket 使用map map $http_upgrade $connection_upgrade { default upgrade; '' close; } upstream abc. ...
- 洛谷P4704 太极剑(乱搞)
题意 题目链接 Sol 不会正解 写了发暴力过了,貌似跑的还挺快?.. // luogu-judger-enable-o2 // luogu-judger-enable-o2 #include< ...
- 洛谷P4063 [JXOI2017]数列(dp)
题意 题目链接 Sol 这题想还是不难想的,就是写起来很麻烦,然后去看了一下loj的最短代码表示只能Orz 首先不难发现一条性质:能够选择的区间一定是不断收缩的,而且新的可选区间一定是旧区间的某个位置 ...
- git 入门教程之版本控制
版本控制 我们知道 git 是分布式版本控制系统,所以称被控制对象是版本本身没错,但是从git 命令中发现,并没有版本这个名词,有的只是commit,所以前几节我一直称其为提交. 为了避免后续教程引发 ...
- (后端)Mybatis实现批量删除操作(转)
原文地址:https://blog.csdn.net/javaee_sunny/article/details/52511842 一. 这里主要考虑两种参数类型:数组或者集合. 而这点区别主要体现在E ...
- codeforces 2B The least round way(DP+数学)
The least round way 题目链接:http://codeforces.com/contest/2/problem/B ——每天在线,欢迎留言谈论.PS.本题有什么想法.建议.疑问 欢迎 ...
- MongoDB 安装与配置
MongoDB下载 官方下载链接:https://www.mongodb.com/download-center/community MongoDB安装 简单,按提示安装即可.安装方式: 1. Com ...
- Spark性能优化【OOM】
一.异常情况 Spark on yarn模式下,当yarn为client的模式时没有OOM而cluster模式下出现OOM 二.异常分析 由于client模型没有出现OOM而cluster模式出现OO ...
- 转自:stuff字符串拼接方法
下文讲述数据表中将多列合并到一列的方法分享 转自:http://www.maomao365.com/?p=6796
- python第一百三十天 ---简单的BBS论坛
简单的BBS论坛 实现功能 git仓库地址:https://github.com/uge3/BBS 1.整体参考“抽屉新热榜” + “博客园” 2.实现不同论坛版块 3.帖子列表展示 4.个人博客主页 ...