Problem A. Sasha vs. Kate
During the regular Mars’s World Finals Subregional Programming
Contest a boy Sasha lost N “Mars” bars of chocolate to a girl Kate.
But for two years already Sasha does not hurry to pay his debt. And
now Sasha and Kate decided that Sasha will give Kate P chocolate
bars, where number P can be obtained from the number N by removing
exactly K decimal digits. Sasha generously let Kate to choose
digits to be removed. Your task is to find out how many bars Sasha
will give Kate. Of course Kate will choose K digits from the number
N in such a way that the resulting number P would be maximal.
Input
The first line of the input file contains two integer numbers N and
K (1 ≤ N ≤ 101000; 0 ≤ K ≤ 999). Number K is strictly less than
the number of digits in N. N will not have any leading zeros.
Output
Output the unknown P.
Examples
standard input standard output
1992 2 99
1000 2 10
就每次比较两位,如果这一位比下一位小就删掉,如果都大,删掉最后一个。
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define ff(i,a,b) for(int i = a; i <= b; i++)
#define f(i,a,b) for(int i = a; i < b; i++)
typedef pair<int,int> P;
#define ll long long
int main()
{
ios::sync_with_stdio(false);
string s; int n;
cin >> s >> n;
int k = n;
int num = 0;
for(int i = 1; n && i < s.length(); i++)
{
if(s[i - 1] - '0' < s[i] - '0')
{
f(j,i - 1,s.length())
s[j] = s[j + 1];
n--;
i -= 2;
i = max(i,0);
}
}
s.erase(s.length() - k, k);
cout << s << endl;
return 0;
}