friht 发表于 2015-8-20 13:44:13

HDU 3183 A Magic Lamp (RMQ,4级)

G - A Magic Lamp
Crawling in process...
Crawling failed
Time Limit:1000MS   
Memory Limit:32768KB   64bit IO Format:%I64d & %I64u


Submit
Status



Appoint description:
System Crawler (2013-05-30)



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


思路:转变一下,从原先的数中选出m个使其值最小,这样就是一个赤果果的RMQ了。


#include<cstring>
#include<iostream>
#include<cstdio>
#define FOR(i,a,b) for(int i=a;i<=b;++i)
#define clr(f,z) memset(f,z,sizeof(f))
#define ll(x) (1<<x)
#define check(i,j) s<=s?i:j;
using namespace std;
const int mm=1009;
int rmq;
int bit;
char s,ans;
int n,m;
void initRMQ()
{ int len=strlen(s);
bit=-1;
FOR(i,1,mm-1)bit=(i&(i-1))==0?bit+1:bit;
FOR(i,0,len-1)rmq=i;
FOR(i,1,bit)
for(int j=0;j+ll(i)-1<len;++j)
rmq=check(rmq,rmq);
}
int RMQ(int l,int r)
{
int t=bit;
r-=ll(t)-1;
int z=check(rmq,rmq);
return z;
}
int main()
{
while(~scanf(&quot;%s%d&quot;,s,&n))
{
m=strlen(s);
n=m-n;///需要选n次最小值
int pos=0,to=0;
initRMQ();
for(int i=n;i>=1;--i)
{ //printf(&quot;%d x %d\n&quot;,to,m-i);
to=RMQ(to,m-i);
//cout<<to<<&quot; &quot;<<s<<endl;
ans=s;
}
bool yes=0;
FOR(i,0,pos-1)
if(ans!='0'||yes)
{ yes=1;
putchar(ans);
}
if(!yes)putchar('0');
putchar('\n');
}
}
页: [1]
查看完整版本: HDU 3183 A Magic Lamp (RMQ,4级)