HDU_3183_A Magic Lamp

时间:2023-03-09 09:09:16
HDU_3183_A Magic Lamp

A Magic Lamp

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

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
  • 长度为n的数串去掉m个数问剩下的数的最小值是多少
  • 等价于求长度为n的数挑(n-m)个数使得其值最小
  • 那么我们只要从高位开始每次挑选最小数一共挑(n-m)个就是结果
  • 用ST表计算区间min
  • 注意前导零的处理
 #include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <climits>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
typedef long long LL ;
typedef unsigned long long ULL ;
const int maxn = 1e3 + ;
const int inf = 0x3f3f3f3f ;
const int npos = - ;
const int mod = 1e9 + ;
const int mxx = + ;
const double eps = 1e- ;
const double PI = acos(-1.0) ; int n, m, fac[], dp[maxn][], l, r, p, ans[maxn], tot, rec;
char str[maxn];
int main(){
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
for(int i=;i<;i++)
fac[i]=(<<i);
while(~scanf("%s %d",str+,&m)){
n=strlen(str+);
m=n-m;
rec=m;
int k=(int)(log((double)n)/log(2.0));
for(int i=;i<=n;i++)
dp[i][]=i;
for(int j=;j<=k;j++)
for(int i=;i+fac[j]-<=n;i++){
int pl=dp[i][j-], pr=dp[i+fac[j-]][j-];
if(str[pl]<=str[pr])
dp[i][j]=pl;
else
dp[i][j]=pr;
}
l=;
tot=;
while(m){
r=n-m+;
k=(int)(log((double)(r-l+))/log(2.0));
int pl=dp[l][k], pr=dp[r-fac[k]+][k];
if(str[pl]<=str[pr])
p=pl;
else
p=pr;
ans[tot++]=p;
l=p+;
m--;
}
int flag=;
for(int i=;i<tot;i++){
if(!(str[dp[ans[i]][]]-'')){
if(flag)
printf("%c",str[ans[i]]);
}else{
flag=;
printf("%c",str[ans[i]]);
}
}
if(!flag)
printf("");
cout<<endl;
}
return ;
}