如标题所述,任务是:
给定数字N,消除K个数字以获得可能的最大数字。这些数字必须保留在它们的位置。
例如:n = 12345,k = 3,max = 45(消除前三个数字,数字不能移动到其他位置)。
有什么解决方法吗?
(这不是作业,我正在为算法竞赛做准备并在在线评测中解决问题。)
1 <= N <= 2^60, 1 <= K <= 20。
编辑:这是我的解决方案。 它起作用 :)
#include <iostream>
#include <string>
#include <queue>
#include <vector>
#include <iomanip>
#include <algorithm>
#include <cmath>
using namespace std;
int main()
{
string n;
int k;
cin >> n >> k;
int b = n.size() - k - 1;
int c = n.size() - b;
int ind = 0;
vector<char> res;
char max = n.at(0);
for (int i=0; i<n.size() && res.size() < n.size()-k; i++) {
max = n.at(i);
ind = i;
for (int j=i; j<i+c; j++) {
if (n.at(j) > max) {
max = n.at(j);
ind = j;
}
}
b--;
c = n.size() - 1 - ind - b;
res.push_back(max);
i = ind;
}
for (int i=0; i<res.size(); i++)
cout << res.at(i);
cout << endl;
return 0;
}