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

     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 }
  • 相关阅读:
    3月1日中国观点股评级:将新浪上调至买入评级
    详讯:新浪第四季度净盈余1亿美元
    陈诉称必应1月全球市场份额首超雅虎
    Facebook收买群组动静办事供应商Beluga
    EMC宣布混杂云战略 年内新开6家中国分公司
    动静称微软最快在3月内推出IE9 RTW版
    Unity3D ShaderLab 混合两张贴图(Blend)
    C# 对排序的认识( Comparison ) ,使用匿名方法实现比较器
    C# 优雅的解决 多线程中访问 UI 的问题
    C# 为SharpDevelop4.1 做了一个深色的配色
  • 原文地址:https://www.cnblogs.com/npugen/p/9685888.html
Copyright © 2011-2022 走看看