HDU 3183.A Magic Lamp-区间找最小值-RMQ(ST)

本文解析了HDU3183.AMagicLamp题目,介绍了一种通过去除指定数量的数字来形成最小可能整数的算法解决方案。利用RMQ(Range Minimum Query)技术,文章详细说明了如何有效地找到并移除不必要的数字,以达到目标最小值。

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

A Magic Lamp

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


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
 

 

Source

 

题意就是

一个序列A[1...N],一共N个数,除去M个数使剩下的数组成的整数最小。就是在A[1...N]中顺次选取N-M个数,使值最小。

直接RMQ,找l到n-m+1的最小值,然后下一个从选取的数下一个开始,因为N-M个数,所以不能过界,要不然长度不够。

不能有前导零,具体的代码里写的。

 

代码:

 1 //HDU 3183.A Magic Lamp-RMQ
 2 #include<bits/stdc++.h>
 3 using namespace std;
 4 typedef long long ll;
 5 const int maxn=1e3+10;
 6 
 7 int n,m,h;
 8 char c[maxn];
 9 int a[maxn],ans[maxn];
10 int mi[maxn][maxn];
11 
12 int Min(int x,int y)
13 {
14     return a[x]<=a[y]?x:y;
15 }
16 
17 void ST()
18 {
19     for(int i=1;i<=n;i++)
20         mi[i][0]=i;
21     for(int j=1;(1<<j)<=n;j++){
22         for(int i=1;i+(1<<j-1)<=n;i++){
23             mi[i][j]=Min(mi[i][j-1],mi[i+(1<<(j-1))][j-1]);
24         }
25     }
26 }
27 
28 int RMQ(int l,int r)
29 {
30     int k=0;
31     while((1<<(k+1))<=r-l+1) k++;
32     int cnt=Min(mi[l][k],mi[r-(1<<k)+1][k]);
33     return cnt;
34 }
35 
36 int main()
37 {
38     while(~scanf("%s%d",c,&m)){
39         n=strlen(c);h=0;
40         for(int i=0;i<n;i++)
41             a[i+1]=c[i]-'0';
42         ST();
43         int l=1;
44         m=n-m;
45         while(m>0){
46             int pos=RMQ(l,n-m+1);
47             ans[++h]=pos;
48             l=pos+1;
49             m--;
50         }
51         if(h==0) cout<<0<<endl;
52         else{
53             int flag=0;
54             for(int i=1;i<=h;i++){
55                 if(!flag&&a[ans[i]]==0){
56                     if(i!=h) continue;
57                     else cout<<a[ans[i]];
58                 }
59                 else{
60                     flag=1;cout<<a[ans[i]];
61                 }
62             }
63             cout<<endl;
64         }
65     }
66 }

 

转载于:https://www.cnblogs.com/ZERO-/p/10679789.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值