hdu 3183 A Magic Lamp (RMQ)

链接:http://acm.hdu.edu.cn/showproblem.php?pid=3183

题目:

                                                 A Magic Lamp

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


思路:

        输入一个整数,将他当做一组数组来处理。对于这是数组,一共len个数,除去n个数使剩下的数组成的整数最小。也就是说在A[1...冷]中顺次选取len-n个数,使值最小

,采用RMQ算法查询区间(i,j)内的最小值。由于要找出len-n个书组成的最小的整数。所以整数的第一位必须在(1,len-n+1)里面找才能满足条件而第k位则要在(k,len-m+1-(k-1))。

       假设A数组就只有6个数,分别是A[1]A[2]A[3]A[4]A[5]A[6],我们去掉2个数,使形成的值最小。那么我们此时的len-n=4则我们说形成的4位数的第一位一定在区间[13]中出现,若再大,那么后面只有A[5]A[6]两位数了(甚至更少),这样的话最多只可能形成3位数,绝对不能形成4位了。所以第一位可以在区间[1len-n+1]里面找,假设第一位在位置x,因为第二位肯定在第一位的后面,所以第二位一定存在于区间[x+1len-m+1-(x+1-1)]现在只需要确定N-M-1位了,所以区间就可以向后增加1,一直这样循环下去,就可以找到了。

代码如下:

<pre name="code" class="cpp">#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;

int dpmin[1005][20];
int vis[1005], ans[1000];
char str[1005];

int query( int x, int y )
{
    int q = log2( y-x+1.0);

    if( vis[dpmin[x][q]] > vis[dpmin[y-(1<<q)+1][q]] )
    {
        return  dpmin[y-(1<<q)+1][q];
    }
    else
    {
        return dpmin[x][q];
    }
}

int main()
{
    int n;
    while( ~scanf("%s%d",str, &n ) )
    {
        int l = strlen(str);
        for( int i = 0; i < l; i ++ )
        {
            vis[i+1] = str[i]-'0';
            dpmin[i+1][0] = i+1;
        }

        for( int j = 1; j < 20; j ++ )
            for( int i = 1; i + (1<<j) - 1 <= l; i ++ )
            {
                int p = 1<<(j-1);
                if( vis[dpmin[i][j-1]] <= vis[dpmin[i+p][j-1]] )
                {
                    dpmin[i][j] = dpmin[i][j-1];
                }
                else
                {
                    dpmin[i][j] = dpmin[i+p][j-1];
                }
            }
        int m = l-n, p = 0, k = 1;
        for( int i = 1; i <= l-n; i ++ )
        {
            k = query(k,l-m+1);
            ans[p++] = vis[k++];
            m--;
        }
        int f = 0;
        for( int i = 0; i < p; i ++ )
        {
            if( f || ans[i] )
            {
                cout<<ans[i];
                f = 1;
            }
        }
        if( !f ) cout<<"0";
        cout<<endl;
    }
    return 0;
}

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值