题目大意:给定一个字符串,然后最多删除K个。使得剩下的组成的数值最小。
解题思路:问题等价与取N-M个数。每次取的时候保证后面能取的个数足够,而且取的数最小,查询最小的操作用RMQ优化。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 10005;
int N, M, d[maxn][20];
char s[maxn];
void rmq_init() {
N = strlen(s);
for (int i = 0; i < N; i++)
d[i][0] = s[i];
for (int k = 1; (1<<k) <= N; k++) {
for (int i = 0; i < N; i++)
d[i][k] = min(d[i][k-1], d[i+(1<<(k-1))][k-1]);
}
}
int rmq_query(int l, int r) {
int k = 0;
while ((1<<(k+1)) <= r - l + 1) k++;
return min(d[l][k], d[r-(1<<k)+1][k]);
}
int main () {
while (scanf("%s%d", s, &M) == 2) {
rmq_init();
M = N - M;
int mv = 0;
bool flag = true;
for (int i = M; i; i--) {
int c = rmq_query(mv, N - i);
while (mv < N && s[mv] != c) mv++;
mv++;
if (c == '0' && flag)
continue;
flag = false;
printf("%c", c);
}
if (flag)
printf("0");
printf("\n");
}
return 0;
}