A Magic Lamp
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2041 Accepted Submission(s): 816
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?
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.
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.
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
RMQ,但是里面有很多细节问题需要注意一下!先讲一下为什么是RMQ算法,假如
有个数是6位数减去两个数就代表顺序选择四位数,第一位数必须在1-3内选择,第二
个数必
须在第一个数之后到剩下的位数之内选择,以此类推!也就是贪心法则!用RMQ计
算区间最小值比较方便!
/*
这一题遇到几个比较棘手的问题:
如果直接求最小值,但是如何找到最小值的位置
就是一个比较严重的问题
所以我们应该怎么办啊?
*/
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
const int N=1010;
int minsum[N][20];
int m;
char s[N],ans[N];
/*
void RMQ(int n)
{
int i,j;
for(i=1;i<=stl;i++)
{
minsum[i][0]=s[i]-'0';
}//初始化
for(j=1;j<20;j++)
{
for(i=1;i<=stl;i++)
{
if(i+(i<<j)-1<=stl)
{
minsum[i][j]=min(minsum[i][j-1],minsum[i+(1<<(j-1))][j-1]);
}
}
}
}
如上所述,如果直接求的是最小值就找不到位置了,怎么解决呢?
*/
int mins(int a,int b)
{
return s[a]<=s[b]?a:b;
}//return的是位置而不是字符!记住了!
void RMQ(int n)
{
int i,j;
for(i=0;i<n;i++)
{
minsum[i][0]=i;//记住这里minsum[][]记录的是位置而不是字符
}//初始化
for(j=1;(1<<j)<n;j++)
{
for(i=0;i<=n;i++)
{
if(i+(1<<j)-1<n)
{
minsum[i][j]=mins(minsum[i][j-1],minsum[i+(1<<(j-1))][j-1]);//这个也是位置!
}
}
}
}
int query(int a,int b)
{
int c=(int)(log(b-a+1.0)/log(2.0));
// cout<<"c="<<c<<endl;
return mins(minsum[a][c],minsum[b-(1<<c)+1][c]);
}
int main()
{
int p,st,ed,i,stl;
while(cin>>s>>m)
{
stl=strlen(s);
// cout<<s<<" "<<m<<" "<<stl<<" "<<endl;
RMQ(stl);
p=st=0;
ed=stl-m;
/* for(i=1;i<=stl-ed;i++)
{
cout<<"选择区间为:"<<st<<" --"<<st+m<<endl;
st=query(st,st+m);
cout<<"st="<<st<<endl;
ans[p++]=s[st++];
cout<<"选择后:st="<<st<<endl;
}*/
/*
前面的算法是错的?为什么呢?区间的大小不是固定的你比如最后一个
实例!54321这样算我们知道区间是3第一个获取的数是3那么应该在3-6获取
则第二个数就是1了,实际上他应该是2再说了,算出了1后面又不知道怎么
结束了,所以需要改进!怎么改进呢?
*/
while(ed--)
{
st=query(st,stl-ed-1);
ans[p++]=s[st++];
}
for(i=0;i<p;i++)
{
if(ans[i]!='0')
{
break;
}
}
if(i==p)
{
cout<<0<<endl;
}
else
{
while(i<p)
{
cout<<ans[i];
i++;
}
cout<<endl;
}
}
}