Chapter Four Hands report (issue number deleted)

Analysis program storage problems. content include:

  1. Practice topic

  2. Problem Description

  3. Algorithm Description (state your greedy strategy, and reference venue arrangements, required to prove that the use of greedy choice and optimal substructure property)

  4. Time and space complexity algorithm analysis (analysis process have)

  5. Feelings and experiences (for this practice of harvesting and doubts summarize)

 

4-2 puncturing logarithm problem (110 min)
 

Given n-bit positive integer a, which is removed after any k≤n digits, the remaining numbers arranged in the original order to form a new positive integer. For a given n-bit positive integer and a positive integer k, an algorithm designed to delete the minimum number of new programs to find the number of remaining digits.

Input formats:

Line 1 is a positive integer a. The second row is a positive integer k.

Output formats:

The minimum number of output.

Sample input:

Here we are given a set of inputs. E.g:

178543 
4 

Sample output:

Given here corresponding output. E.g:

13

Greedy strategy: the larger the high number, the greater the number, so we look from a high, relatively high and the next, from high to low to see if the high is greater than the low, then delete the high number, if it is incremented the number, then delete the last digit

178543: 1 <7, by incrementing; 7 <8, is incremented by; 8> 5, 8 decreasing deleted;

17543: 1 <7, by incrementing; 7> 5, 7 decremented deleted;

1543: 1 <5, by increments; 5> 4, 5 decremented deleted;

143: 1 <4, by incrementing; 4> 3, 4 decremented deleted;

Too: 13

 

#include<iostream>
using namespace std;
int main()
{
    int k,n,i,j;
    bool b[100000];
    string a;
    while(cin>>a>>k)
    {
        n=a.length();
        for(i=0;i<n;i++)
            b[i]=true;
        while(k>0)
        {
            for(i=0;i<n;i++)
                if(b[i])
                {
                    for(j=i+1;j<n;j++)
                        if(b[j])break;
                    if(j<n&&a[i]>a[j])break;
                    i=j-1;
                }
            if(i==n)break;
            for(i=0;i<n&&k>0;i++)
                if(b[i])
                {
                    for(j=i+1;j<n;j++)
                        if(b[j])break;
                    if(j<n&&a[i]>a[j])
                    {
                        b[i]=false;
                        k--;
                        break;
                    }
                    i=j-1;
                }
        }
        for(i=n-1;i>=0&&k>0;i--)
            if(b[i])
            {
                b[i]=false;
                k--;
            }
        for(i=0;i<n;i++)
            if(a[i]!='0'&&b[i])break;
        for(;i<n;i++)
            if(b[i])cout<<a[i];
        cout<<'\n';
    }
    return 0;
}

 

time complexity:

while (k> 0) is a transform operation cycle k k--

Therein for loop is O (n)

I.e., calculation complexity is O (n ^ 2)

 

Experience:

A better understanding of the idea of ​​greedy algorithm

Guess you like

Origin www.cnblogs.com/MTstory/p/11894819.html