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

时间:2022-08-15 08:52:34

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个数,所以不能过界,要不然长度不够。

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

代码:

 //HDU 3183.A Magic Lamp-RMQ
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e3+; int n,m,h;
char c[maxn];
int a[maxn],ans[maxn];
int mi[maxn][maxn]; int Min(int x,int y)
{
return a[x]<=a[y]?x:y;
} void ST()
{
for(int i=;i<=n;i++)
mi[i][]=i;
for(int j=;(<<j)<=n;j++){
for(int i=;i+(<<j-)<=n;i++){
mi[i][j]=Min(mi[i][j-],mi[i+(<<(j-))][j-]);
}
}
} int RMQ(int l,int r)
{
int k=;
while((<<(k+))<=r-l+) k++;
int cnt=Min(mi[l][k],mi[r-(<<k)+][k]);
return cnt;
} int main()
{
while(~scanf("%s%d",c,&m)){
n=strlen(c);h=;
for(int i=;i<n;i++)
a[i+]=c[i]-'';
ST();
int l=;
m=n-m;
while(m>){
int pos=RMQ(l,n-m+);
ans[++h]=pos;
l=pos+;
m--;
}
if(h==) cout<<<<endl;
else{
int flag=;
for(int i=;i<=h;i++){
if(!flag&&a[ans[i]]==){
if(i!=h) continue;
else cout<<a[ans[i]];
}
else{
flag=;cout<<a[ans[i]];
}
}
cout<<endl;
}
}
}