对于一个序列A[1...N],一共N个数,除去M个数使剩下的数组成的整数最小。
也就是说在A[1...N]中顺次选取N-M个数,使值最小。
它主要是基于以下事实:
对于序列A[1...N],选取N-M个数,使组成的值最小,而且顺序不能交换,既然要选取N-M个,那么可以容易知道这N-M位数的第一位一定在数组A中的区间
我们就可以这样做了,第一位可以在区间[1,M+1]里面找,假设第一位在位置x,因为第二位肯定在第一位的后面,所以第二位一定存在于
区间[x+1,M+2],为什么是M+2,因为第一位已经确定了,现在只需要确定N-M-1位了,所以区间就可以向后增加1,一直这样循环下去,就可以找到了。
题目:
Kiki likes traveling. One day she finds a magic lamp, unfortunately the genie in the lamp is not so kind. Kiki must answer a question, and then the genie will realize one of her dreams.
The question is: give you an integer, you are allowed to delete exactly m digits. The left digits will form a new integer. You should make it minimum.
You are not allowed to change the order of the digits. Now can you help Kiki to realize her dream?
The question is: give you an integer, you are allowed to delete exactly m digits. The left digits will form a new integer. You should make it minimum.
You are not allowed to change the order of the digits. Now can you help Kiki to realize her dream?
Each test case will contain an integer you are given (which may at most contains 1000 digits.) and the integer m (if the integer contains n digits, m will not bigger then n). The given integer will not contain leading zero.
If the result contains leading zero, ignore it.
178543 4 1000001 1 100001 2 12345 2 54321 2
13 1 0 123 321AC代码:
#include <stdio.h>
#include <string.h>
#include<cmath>
char s[1010];
int m;
int d[1010][1005];
char c[1010];
int min(int x, int y)
{
return s[x] <= s[y] ? x : y;
}
void RMQ_init()
{
int n = strlen(s);
for (int i = 0;i < n;i++) d[i][0] = i;
for (int j = 1;(1 << j) <= n;j++)
for (int i = 0;i + (1 << j) - 1 < n;i++)
d[i][j] = min(d[i][j - 1], d[i + (1 << (j - 1))][j - 1]);
}
int RMQ(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) != EOF)
{
RMQ_init();
int cnt = 0;
int n = strlen(s);
m = n - m;
int p = 0;
while(m--)
{
p = RMQ(p, n - m - 1);
c[cnt++] = s[p++];
}
int i;
for (i = 0;i < cnt;i++)
if (c[i] != '0')
break;
if (i == cnt)
printf("0");
else
{
while(i<cnt)
printf("%c", c[i++]);
}
printf("\n");
}
return 0;
}
RMQ函数的返回类型写成char,害我找了好久好久····