HDU 3183 A Magic Lamp (RMQ,4级)

时间:2022-11-24 12:52:31
G - A Magic Lamp
Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Appoint description:

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[i]<=s[j]?i:j;
using namespace std;
const int mm=1009;
int rmq[mm][25];
int bit[mm];
char s[mm],ans[mm];
int n,m;
void initRMQ()
{ int len=strlen(s);
  bit[0]=-1;
  FOR(i,1,mm-1)bit[i]=(i&(i-1))==0?bit[i-1]+1:bit[i-1];
  FOR(i,0,len-1)rmq[i][0]=i;
  FOR(i,1,bit[len])
  for(int j=0;j+ll(i)-1<len;++j)
    rmq[j][i]=check(rmq[j][i-1],rmq[j+ll(i-1)][i-1]);
}
int RMQ(int l,int r)
{
  int t=bit[r-l+1];
  r-=ll(t)-1;
  int z=check(rmq[l][t],rmq[r][t]);
  return z;
}
int main()
{
  while(~scanf("%s%d",s,&n))
  {
    m=strlen(s);
    n=m-n;///需要选n次最小值
    int pos=0,to=0;
    initRMQ();
    for(int i=n;i>=1;--i)
    { //printf("%d x %d\n",to,m-i);
      to=RMQ(to,m-i);
      //cout<<to<<" "<<s[to]<<endl;
      ans[pos++]=s[to++];
    }
    bool yes=0;
    FOR(i,0,pos-1)
    if(ans[i]!='0'||yes)
    { yes=1;
      putchar(ans[i]);
    }
    if(!yes)putchar('0');
    putchar('\n');
  }
}