Greedy topic - delete a number for the minimum

Find the smallest number.

Enter a high precision positive integer N (not more than 5000), wherein after removing any remaining k digit numbers to order the original left and right to form a new positive integer.
For a given N and k, find the minimum of a solution so that the remaining digits of the new number.

Input formats:

The first line of a given no more than five thousand positive integer n. The second line does not exceed a given bit number n of the non-negative integer k.

Output formats:

The last remaining minimum number.

Sample input:

175438 4

Sample output:

13

Ideas:
Under the premise of a fixed number of bits of an integer, so that a high number as small as possible, the value of the integer is small

eg 175438
delete 1: remove 7--15438 (not remove 8--17543)
Delete 2: Remove 7,5--1438
Delete 3: 754--138 removed (not remove 785--143)

High to low according to the order of search, if you increment the number, then delete the last digit
, if diminishing, then delete the first (as far as possible high) decreasing the interval the first character
and then back to the first string, delete the next number by the above rules.

Code:

#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<vector>
#include<iomanip>
#include<vector>
#include<string>
#define M 5010
#define INF 0x3f3f3f3f
typedef long long ll;
using namespace std;
vector<int>v;
string s;
int main(){
    cin >> s;
    for(int i=0; s[i]; i++)
        v.push_back(s[i]-'0');
    v.push_back(-1);
    
    int k;
    cin >> k;
    while(k--){         //删除k个
        int t=0;
        while(v[t]<=v[t+1] && v[t]!=-1)
            t++;
        v.erase(v.begin()+t);
    }
    
    bool flag = 0;      //输出
    for(int i=0; v[i]!=-1; i++){
        if(v[i]!=0) flag = 1;
        if(flag) cout << v[i];
    }
    if(!flag) cout << 0;
    cout << endl;
    
    return 0;
}

Published 62 original articles · won praise 0 · Views 1740

Guess you like

Origin blog.csdn.net/jhckii/article/details/104475423