A Magic Lamp
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1279 Accepted Submission(s): 485
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
Source
题意:一串数字,删去m个数字,输出可以得到的最小值。
思路:一串数字长为lenth,则最后产生lenth-m位数字,即选choose=lenth-m位数字。
1.要想它最小,则第一位要最小。由于只能删m个数,这个最小值的第一位数字必然出现在num的1~m+1位里,前面说了“第一位要最小”,那么要选的这个数字一定是num的1~m+1位里的最小数字,假设为p。
2.选出了第一位,现在需要保证第二位最小。由于1的操作m=m-(p-1),choose--。现在只看p+1(因为p位置的数已选)之后的数字,依然重复1的操作,直到choose==0。
选出最小的操作算法:RMQ_ST
代码:
#include<cstdio>
#include<cmath>
#include<iostream>
#include<cstring>
using namespace std;
int minnum[1010][30];//minnum[i][j]记录的是 i~i+2^j-1之间靠左的最小值的位置
int num[1010];
void RMQ(int n)
{
int i,j;
for(i=1;i<=n;i++)
minnum[i][0]=i;//初始化
for(int j=1;(1<<j)<=n;j++)//从i开始的2^j个数
for(int i=1;i+(1<<j)-1<=n;i++)//找出i~i+2^j-1之间的最小数的靠左的位数
{
if(num[minnum[i][j-1]]<=num[minnum[i+(1<<(j-1))][j-1]])
minnum[i][j]=minnum[i][j-1];
else
minnum[i][j]=minnum[i+(1<<(j-1))][j-1];
}
}
int Search(int a,int b)
{
int k=log2(b-a+1);//一开始写成了log(b-a-1) WA了不知道多少次 两种算k的方法
// int k;
// while(a+(1<<k)<b-(1<<k)+1)++k;
if(num[minnum[a][k]]<=num[minnum[b-(1<<k)+1][k]])//选出靠左最小的数
return minnum[a][k];
else
return minnum[b-(1<<k)+1][k];
}
int main()
{
char s[1010];
char ans[1010];
int k;
int m;
while(scanf("%s%d",s,&m)!=EOF)
{
k=0;
int lenth=strlen(s);
for(int i=1;i<=lenth;i++)
num[i]=s[i-1]-'0';
RMQ(lenth);
int choose=lenth-m;//要选多少个数
int p;//指向选的最小值的位置
for(int i=1;choose;i=p+1)
{
p=Search(i,i+m);//在i~i+m之间m+1个数里找最小的
m=m-(p-i);//更新还需要删几个数
ans[k++]=num[p]+'0';//把选的那个数存起来
choose--;//每一次一定会选出一个数
}
ans[k]=0;
int i;
for(i=0;i<k;i++)//去前导0
{
if(ans[i]!='0')
{
p=i;//记录非0的起始位置
break;
}
}
//cout<<i<<ends<<k<<endl;
if(i==k)//都是0则输出0
printf("0\n");
else
printf("%s\n",ans+p);
}
return 0;
}
不懂RMQ_ST 的可以先看 http://blog.csdn.net/niushuai666/article/details/6624672