HDU3183 A Magic Lamp(RMQ)

对于一个序列A[1...N],一共N个数,除去M个数使剩下的数组成的整数最小。

也就是说在A[1...N]中顺次选取N-M个数,使值最小。

它主要是基于以下事实:

对于序列A[1...N],选取N-M个数,使组成的值最小,而且顺序不能交换,既然要选取N-M个,那么可以容易知道这N-M位数的第一位一定在数组A中的区间

我们就可以这样做了,第一位可以在区间[1M+1]里面找,假设第一位在位置x,因为第二位肯定在第一位的后面,所以第二位一定存在于

区间[x+1M+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? 
Input
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. 
Output
For each case, output the minimum result you can get in one line. 
If the result contains leading zero, ignore it. 
Sample Input
178543 4 
1000001 1
100001 2
12345 2
54321 2
Sample Output
13
1
0
123
321
AC代码:

#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,害我找了好久好久····

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值