SRM 437 TheSwap ( Permutation + Memory Search )

来源:互联网 发布:手机淘宝店简介怎么写 编辑:程序博客网 时间:2024/05/29 17:40

My idea:

 

    At first I solve this problem with the greedy approach (keeping the number maximal for every iteration) and all the test cases given is right . Later on I find some cases from the discussion that my code gives the wrong answer .

    For example : 35766 3

    the correct result is that 35766->75366->76356->76653

    but the greedy approach works like this : 35766->75366->76365->76635

    Here I will use the make permutation and memory search to solve this problem , and I have learn much about STL in C++ this time .

 

Problem Statement

    

There is nothing more beautiful than just an integer number.

You are given an integer n. Write down n in decimal notation with no leading zeroes, and let M be the number of written digits. Perform the following operation exactly k times:

  • Choose two different 1-based positions, i and j, such that 1 <= i < j <= M. Swap the digits at positions i and j. This swap must not cause the resulting number to have a leading zero, i.e., if the digit at position j is zero, then i must be strictly greater than 1.

 

Return the maximal possible number you can get at the end of this procedure. If it's not possible to perform k operations, return -1 instead.

 

Definition

    Class:TheSwapMethod:findMaxParameters:int, intReturns:intMethod signature:int findMax(int n, int k)(be sure your method is public)      

Constraints

-n will be between 1 and 1,000,000, inclusive.-

k will be between 1 and 10, inclusive.

 

原创粉丝点击