NYOJ 1057 Max search (3) (Greedy)

Source: Internet
Author: User

NYOJ 1057 Max search (3) (Greedy)

Description

 

Given an integer N, you can move the numbers on two adjacent digits at most K times to get a new integer.

Evaluate the maximum value of the new integer.

Enter multiple groups of test data.
Each group of test data occupies one row, with two numbers of N and K (1 ≤ limit N ≤ limit 10 ^ 18; 0 ≤ limit K ≤ limit 100 ). the output of each group of test data occupies one row, and the output moves to the maximum value of the new integer. Sample Input
1990 1100 09090000078001234 6
Sample output
91901009907000008001234

Question Analysis:

Greedy Algorithm, but greedy for each step, it should be in the range of k steps to find the maximum value, exchange forward in turn, if found the maximum value, but not to k step, then we will continue to look for the next kk step from the current optimum.

 

AC code:

 

# Include
   
    
# Include
    
     
# Include
     
      
# Include
      
       
Using namespace std; int main () {long n; int k; while (scanf (% lld % d, & n, & k )! = EOF) {char s [20]; sprintf (s, % lld, n); int len = strlen (s); int k1, k2, I, j; k1 = k2 = 0; while (k) {// cout <
       
        
S [k2]) {k2 = I ;}// cout <
        
          K1) {for (j = k2; j> k1; j --) {// the largest value found, swap (s [j], s [J-1]);} k-= (k2-k1); // number of remaining transformations} k1 ++; // modify the pointer to k2 = k1;} printf (% s, s);} return 0 ;}
        
       
      
     
    
   


 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.