hdu3183—A Magic Lamp(RMQ,贪心)

本文介绍了一道ACM竞赛题目,要求从一个长整数中删除指定数量的数字以形成最小可能的新整数。文章详细解释了解题思路,采用RMQ(Range Minimum Query)与贪心算法结合的方法,并提供了完整的C++实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3183

A Magic Lamp

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4071    Accepted Submission(s): 1670


Problem Description
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
 

题目大意:给你一个很长的数字,去掉其中的m个数位,求重新获得数字的最小值

解题思路:RMQ+贪心

给出一串长度为L的数字,去掉其中的m个数字,也就是要从中取得(L-m)个数字,取第一个数字,从区间[1,m+1]取一个最小的数字,可以使得最终取得的数最小,因为这是取得的数的第一位,而剩下的(L-m-1)的数都可以在区间[m+2,L]全部取得,找到第一个最小的数所在位置t,则第二个数在区间[t+1,m+2]取最小值,依次取完m个数,得到是最终数的最小值。然后问题又变成了如何快速求得一个区间内的最小值,这里用个RMQ就可以了。


#include <cstdio>  
#include <cstring>  
#include <cmath>  
#include <iostream>  
#include <queue>
#include <set>
#include <string>
#include <stack>
#include <algorithm>
#include <map>
using namespace std;  
typedef  long long ll;
const int N = 1008;
const int M = 20;
const int INF = 0x3fffffff;
const double Pi = acos(-1.0);

int data[N];
int dp[N][10];

void RMQ( int n )
{
	for( int i = 1 ; i <= n ; ++i ) dp[i][0] = data[i];
	for( int i = 1 ; (1<<i) <= n ; ++i ){
		for( int j = 1 ; j+(1<<i)-1 <= n ; ++j ){
			dp[j][i] = min( dp[j][i-1] , dp[j+(1<<(i-1))][i-1] );
		}
	}
}

int getValue( int l , int r )
{
	int k = 0;
	while( (1<<(k+1)) <= r-l+1 ) k++;
	return min( dp[l][k] , dp[r-(1<<k)+1][k] );
}

int main()
{
	string str;int n,cnt;
	while( cin>>str>>n ){
		cnt = 0;
		int len = str.length();
		for( int i = 0 ; i < len ; ++i ){
			data[i+1] = str[i]-'0';
			if( str[i] == '0' ) ++cnt;
		}
		if( cnt >= len-n){ cout << 0 << endl; continue; }
		RMQ( len );
		int l = 1,r = n+1;
		char st[N];
		for( int i = 0 ; i < len-n ; ++i ){
			int s = getValue( l , r );
			st[i] = s+'0';
			for( int j = l ; j <= r ; ++j ){
				if( data[j] == s ) { l = j+1; break; }
			}
			r = r+1;
		}
		st[len-n] = '\0';
		int k = 0;
		while( st[k] == '0' ) ++k;
		cout << st+k << endl;
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值