Incremental illusion

Incremental illusion

Time limit: 1 Sec Memory Limit: 128 MB

Title Description

  As we all know, shallot students good at computing, especially good at computing the number of combinations, but the title and number of combinations does not matter. 

  Shallot students engrossed in a recent study of dynamic programming, a hundred years of hard study he has a firm grasp of the knowledge longest rising sequence. Shallot for this nondecreasing sequence fascinated, so he had an idea, wrote down a number x, shallot students now hope to find a less than equal to x number so that each digit of this number is nondecreasing. Find this number. 

Entry

  A row number x. 

Export

  His party on behalf of a number of answers. 

Sample input

  233996

Sample Output 

  233899

prompt

  For 40% of the data, x≤10 . 5 .
  For 60% of the data, x≤10 . 9 .
  For 80% of data, x≤10 18 is .
  To 100% of the data, 0≤x≤10 100000. .

answer

      If the maximum value from left to right before the i-th bit x (0 <= x <= 9), x p appears in the first position, the first bit i is less than x, then the first bit to p x -1 becomes full after 9 p.

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 using namespace std;
 5 const int N=101000;
 6 char c[N];
 7 int mx,p,n,l;
 8 int main()
 9 {
10     scanf("%s",c);
11     n=strlen(c);
12     mx=-1;
13     for(int i=0;i<n;i++)
14     {
15         int x=c[i]-'0';
16         if(x<mx)
17         {
18             c[p]-=1;
19             if(c[p]<'0')
20                 c[p-1]-=1,
21                 c[p]='9';
22             for(int j=p+1;j<n;j++)
23                 c[j]='9';
24             break;
25         }
26         if(x>mx)
27             mx=x,p=i;
28     }
29     while(c[l]=='0'&&l+1<n) l++;
30     for(int i=l;i<n;i++)
31         putchar(c[i]);
32     return 0;
33 }
View Code

 

Guess you like

Origin www.cnblogs.com/Jony-English/p/11875836.html