Problem 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

For each test case you are to output a string on a single line that describes a sequence of operations. The string itself should consist only of the characters ‘1’ and ‘2’. This string should be such that if we start with the initial arrangement 1,2,3,...,n−1,n and successively apply rules 1 and 2 according to the order they appear in the output, then the resulting permutation is identical to the input permutation. The output string does not necessarily need to be the shortest such string, but it must be no longer than 2n2 characters. If it is
 

 Sample Input

3 2 1 3
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(贪心)的更多相关文章

  1. UVA-11925 Generating Permutations (逆向思维)

    题目大意:给出1~n的某个排列,问由升序变到这个排列最少需要几次操作.操作1:将头两个数交换:操作2:将头一个数移动最后一个位置. 题目分析:反过来考虑,将这个排列变为升序排列,那么这个变化过程实际上 ...

  2. uva11925 Generating Permutations

    逆序做,逆序输出 紫书上的描述有点问题 感觉很经典 ans.push_back(2); a.insert(a.begin(),a[n-1]); a.erase(a.end()-1); a.push_b ...

  3. CF722D. Generating Sets[贪心 STL]

    D. Generating Sets time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  4. hdu5338 ZZX and Permutations(贪心、线段树)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud ZZX and Permutations Time Limit: 6000/300 ...

  5. 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 ...

  6. hdu 5338 ZZX and Permutations (贪心+线段树+二分)

    ZZX and Permutations Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/O ...

  7. Generating Sets 贪心

    H - Generating Sets Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64 ...

  8. 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 ...

  9. UVa11925 Generating Premutations

    留坑(p.254) #include<cstdio> #include<cstring> #include<cstdlib> #include<algorit ...

  10. UVA 11925 - Generating Permutations

    题意: 给出一个1到n的排列,给出操作顺序,使升序排列能变为所给排列. 分析: 正常冒泡排序的想法.如果前两个数,前面的大于后面的,则换(特例是n,1不能换).否则,就用2的逆操作,把最后的数放前面. ...

随机推荐

  1. 月薪15k的测试员需要学习什么技术?

    想了很久,决定还是要写一篇这样的文章出来,月薪15k的测试员需要学习什么技术?我觉得测试想要月薪15k并不难,只要做到我说的这几点肯定是可以的! 克服懒惰 我觉得,越是聪明的人越是觉得自己“懒惰”.大 ...

  2. Netty实战十三之使用UDP广播事件

    1.UDP的基础知识 我们将会把重点放在一个无连接协议即用户数据报协议(UDP)上,它通常用在性能至关重要并且能够容忍一定的数据报丢失的情况下. 面向连接的传输(如TCP)管理了两个网络端点之间的连接 ...

  3. mybatis插件机制

    目录 mybatis插件机制 主要 类/接口 和 方法 mybatis插件机制实现 mybatis插件机制 mybatis的插件机制使用动态代理实现,不了解的朋友请先了解代理模式和动态代理:插件本质是 ...

  4. 实现响应式——CSS变量

    CSS 变量是 CSS 引入的一个新特性,目前绝大多数浏览器已经支持了,它可以帮助我们用更少的代码写出同样多的样式,大大提高了工作效率,本篇文章将教你如何使用 CSS 变量(css variable) ...

  5. 19 个常用的 JavaScript 简写方法

    来自:SangSir 链接:https://segmentfault.com/a/1190000012673854 原文:https://www.sitepoint.com/shorthand-jav ...

  6. CSS3动画属性:动画(animation)

    一:动画(animation)的参数详解 由于上面用到了animation动画,这里详细介绍下这个animation的参数. 简介 CSS动画(Animations)简单说就是在一段固定的动画时间内暗 ...

  7. Vue基础01vue的基本示例,vue的双向数据绑定,vue中常见的几种用法,vue相关常见指令

    自学vue框架,每天记录重要的知识点,与大家分享!有不足之处,希望大家指正. 本篇将讲述:vue的基本示例,vue的双向数据绑定,vue中常见的几种用法,vue相关常见指令 前期学习基础,使用vue. ...

  8. 使用SQL查看表字段和字段说明

    MySql: show full columns from tableName; Sql server: SELECT A.name AS table_name, B.name AS column_n ...

  9. python itchat 爬取微信好友信息

    原文链接:https://mp.weixin.qq.com/s/4EXgR4GkriTnAzVxluJxmg 「itchat」一个开源的微信个人接口,今天我们就用itchat爬取微信好友信息,无图言虚 ...

  10. Android为TV端助力 Canvas 和 Paint用法

    自定义view里面的onDraw方法,在这里我们可以绘制各种图形,onDraw里面有两个API我们需要了解清楚他们的用法:Canvas 和 Paint. Canvas翻译成中文就是画布的意思,Canv ...