UVA11925-Generating Permutations(贪心)

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 ?rst 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 ?rst 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,这时虽然构成逆序,但是是有可能通过把后面的数移到前面而使序列有序的,所以这时不要交换。

 1 #include <bits/stdc++.h>
 2
 3 using namespace std;
 4
 5 int n;
 6 deque<int> seq;
 7 string ans;
 8
 9 bool check() {
10     for (int i = 0; i < n; i++) {
11         if (seq[i] != i + 1) return false;
12     }
13     return true;
14 }
15
16 int main()
17 {
18     //freopen("input.txt", "r", stdin);
19     while (~scanf("%d", &n) && n) {
20         seq.clear();
21         ans = "";
22         int x;
23         for (int i = 0; i < n; i++) {
24             scanf("%d", &x);
25             seq.push_back(x);
26         }
27
28         while (true) {
29             if (seq[0] == 1 && check()) {
30                 break;
31             }
32             if (seq[0] < seq[1] || seq[0] == n && seq[1] == 1) {
33                 seq.push_front(seq[n - 1]);
34                 seq.pop_back();
35                 ans += ‘2‘;
36             }
37             else {
38                 swap(seq[0], seq[1]);
39                 ans += ‘1‘;
40             }
41         }
42         reverse(ans.begin(), ans.end());
43         cout << ans << endl;
44     }
45     return 0;
46 }

原文地址:https://www.cnblogs.com/npugen/p/9685888.html

时间: 2024-08-30 12:34:02

UVA11925-Generating Permutations(贪心)的相关文章

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

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud ZZX and Permutations Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 181    Accepted Submission(s): 38 Problem Description ZZX likes

Generating Sets 贪心

H - Generating Sets Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Description You are given a set Y of ndistinct positive integers y1,?y2,?...,?yn. Set X of ndistinct positive integers x1,?x2,?...,?xn is

uva 11925 Generating Permutations

题意: 给定一到n的序列,按照下列规则,将序列排为升序列 1.交换前两个数 2.将最后一个数放在最前面(紫书错了,害惨我了) 数据量为300,刘汝佳提示是最多2*n*n次操作,所以我选择了数组模拟,正常数组无法将最后一个放到前面,所以我将数组倒置 因为没有要求最优解,只要能得到想要的结果就行了,所以采取了构造法 只要在第一个比第二个小,那么就把最后一个放到最前面,否则就交换前两个,这样就可以把大的慢慢往后放,如果n 1的情况,那么就要把最后一个放到最后面,不然就会陷入循环 例如4 2 3 1  

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&

UVA 11925 Generating Permutations 生成排列

题意:要用一个有序的序列生成给定序列,操作有两种,一是交换前两个元素,二是把第一个元素移动到最后去. 思路有两种: 1.映射,把给定序列映射成有序的序列,然后按照同样的替换规则把有序的序列映射掉,然后就可以排序啦. 具体解释可以看SRM 664的C题 2.逆向思考,把给定序列变成有序,操作相应变化一下,最后逆序输出操作. 至于排序的问题,把序列看成一个环,第二种操作相当改变了可交换元素的位置,然后就可以等效为冒泡排序啦... 第二种思路需要注意的一点是,是因为是环状的,和冒泡排序有所区别,最大的

UVa 11925 Generating Permutations (构造法)

题意:给定一个序列,让你从一个升序列变成该序列,并且只有两种操作,操作1:交换前两个元素,操作2:把第一个元素移动到最后. 析:一开始的时候吧,不会,还是看的题解,首先是要逆序来做,这样可能好做一点,那么操作1不变,操作2变成把最后一个元素放到最前面. 就像是冒泡排序一样,如果第一个元素大于第二个,交换顺序,否则就把最后一个元素移动到最前面,但第三个样例就死循环了,我也算过,这样会一直重复某几个状态, 所以我们要维护第一个值,如果是最大的元素,那么就不让他们交换了. 代码如下: #include

UVA - 11925 Generating Permutations 推理

题目大意:这题的题目是错的,他的第二个操作是把最后一个调到第一个,然后输出时要逆序输出 解题思路:类似冒泡排序法.先如果第一个是n的话,就调后面的到前面来,然后进行比较,取最大的放前面,这下下一次n到前面的时候,n-1就在n的后面了. #include<cstdio> #include<vector> #include<deque> #define maxn 310 using namespace std; int num[maxn], n; deque<int&

编程题目分类(剪辑)

1. 编程入门 2. 数据结构 3. 字符串 4. 排序 5. 图遍历 6. 图算法 7. 搜索:剪枝,启发式搜索 8. 动态规划/递推 9. 分治/递归 10. 贪心 11. 模拟 12. 算术与代数 13. 组合问题 14. 数论 15. 网格,几何,计算几何 [编程入门] PC 110101, uva 100, The 3n+1 problem, 难度 1 PC 110102, uva 10189, Minesweeper, 难度 1 PC 110103, uva 10137, The T

【转】编程词汇

很实用的编程英语词库,共收录一千五百余条词汇. 第一部分: application 应用程式 应用.应用程序 application framework 应用程式框架.应用框架 应用程序框架 architecture 架构.系统架构 体系结构 argument 引数(传给函式的值).叁见 parameter 叁数.实质叁数.实叁.自变量 array 阵列 数组 arrow operator arrow(箭头)运算子 箭头操作符 assembly 装配件 assembly language 组合语

【转】Java 专业词汇

原址:http://blog.csdn.net/xiaojunjuns1/article/details/52729861 abstract (关键字)             抽象 ['.bstr.kt] access                            vt.访问,存取 ['.kses]'(n.入口,使用权) algorithm                     n.算法 ['.lg.riem] annotation                     [Java