Nyoj 1057 Find maximum number (iii) (knowledge of the greedy algorithm)

Source: Internet
Author: User

Before doing greedy topics are the first step to the data from the large to the small sort, most of the greedy problem of the purpose of the treatment method is also the case. But Nyoj's 1057 questions, in solving the problem

do not need to sort, not realize at a time is greedy problem. After reading the discussion area, I realized that we should use greedy thoughts to solve the problem. It is realized that the greedy algorithm is not the mechanical operation after sorting, but the idea of seeking the optimal solution from the local.

The original question is as follows:


Find the maximum number (c) time limit: +Ms | Memory Limit:65535KB Difficulty:2
Describe

Given an integer n, you can move a number on 2 adjacent digits at a time, moving up to K times, and getting a new integer.

What is the maximum value for this new integer.

Input
multiple sets of test data.
One row for each set of test data, with two numbers n and K per line (1?≤? n≤?10^18; 0?≤? K?≤?100).
Output
The
output of each set of test data takes one row, and the output moves to get the maximum value of the new integer.
Sample input
1990 1100 09090000078001234 6
Sample output
91901009907000008001234

The code is as follows:

#include <stdio.h> #include <stdlib.h> #include <string.h>int main () {int N,t,i,k,len;char max,str[ 100];while (scanf ("%s%d", str,&k)!=eof) {Len=strlen (str); t=0;//t mark the first position of each exchange start while (k>0) {if (T==len)  break;n=t;//the first character for the interchange portion with n marks at the beginning of each interchange for (i=t;i<=t+k;i++) {if (I>=len)  break;if (Str[i]>str[n])  n=i ;//When you encounter a character with a large number of characters, mark it with N. }if (n!=t) {max=str[n];for (i=n;i>t;i--)  str[i]=str[i-1];str[t]=max;k-=n-t;//equals k=k-(N-T);} t++;} printf ("%s\n", str);} return 0;}




Nyoj 1057 Find maximum number (iii) (knowledge of the greedy algorithm)

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.