您的位置:首页 > 其它

hdu 3183 A Magic Lamp(ST表)

2016-04-10 20:10 351 查看
[align=left]Problem Description[/align]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?

[align=left]Input[/align]There are several test cases.
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.

[align=left]Output[/align]For each case, output the minimum result you can get in one line.
If the result contains leading zero, ignore it.

[align=left]Sample Input[/align]178543 4
1000001 1
100001 2
12345 2
54321 2

[align=left]Sample Output[/align]
13
1
0
123
321

solution:
从n个数中删除m个,使得剩下的数最大,那么我们可以知道你选择n-m个数的第一个数一定在1~m这期间,每次在上次最小和当前位置中间选择一个最小的数作为答案,用ST表查询区间最小值

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
const double eps = 1e-6;
int minx[1050][22], num[1050], ans[1050];
char a[1050];
int mins(int a, int b)
{
return num[a] <= num[b] ? a : b;
}
int qmin(int l, int r)
{
int k = r - l + 1;
k = log2((double)k) + eps;
return mins(minx[l][k], minx[r - (1 << k) + 1][k]);
}
void init(int n)
{
for (int i = 0; i <n; i++)
minx[i][0] = i;
for (int j = 1; j < 20; j++)
{
int k = 1 << (j - 1);
for (int i = 0; i + (k << 1) - 1 <= n; i++)
minx[i][j] = mins(minx[i][j - 1], minx[i + k][j - 1]);
}
}
int main()
{
int m;
while (scanf("%s%d", a, &m) != EOF)
{
int len = strlen(a);
for (int i = 0; i < len; i++)
num[i] = a[i] - '0';
init(len);
int pos = 0, len1 = 0;
for (int i = m; i <len; i++)
{
pos = qmin(pos, i);
ans[len1++] = num[pos++];
}
int i = -1;
while (ans[++i] == 0 && i<len1);
if (i == len1)printf("0\n");
else {
for (int j = i; j < len1; j++)
printf("%d", ans[j]);
printf("\n");
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: