HDU 3183 A Magic Lamp(RMQ)

题目链接:

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

A Magic Lamp

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6736    Accepted Submission(s): 2697

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

题目大意:

给你一个正数a,删掉其中m个数字之后,使其最后结果最小

思路:

设数字一共有n位,然后删掉m位,就是剩下n-m位,然后可以思考

n-m位数字中:

第一位就是[0,m-1](使用zi'f字符串储存,最左边的位置是0)范围中的最小值,并记录最小值的位置p

剩下的依次是[p+1,m+i-1]中的最小值,(1<=i<=n-m)

上面的问题就转换到RMQ的问题了,需要注意的是:

这次比较的不是数字,需要重载min

最后去除前导0

输出(完美)

This is the code :

#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<sstream>
#include<stack>
#include<string>
#include<set>
#include<vector>
using namespace std;
#define PI acos(-1.0)
#define EPS 1e-8
#define MOD 1e9+7
#define LL long long
#define ULL unsigned long long     //1844674407370955161
#define INT_INF 0x7f7f7f7f      //2139062143
#define LL_INF 0x7f7f7f7f7f7f7f7f //9187201950435737471
// ios::sync_with_stdio(false);
// 那么cin, 就不能跟C的 scanf,sscanf, getchar, fgets之类的一起使用了。
const int dr[]= {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[]= {-1, 1, 0, 0, -1, 1, -1, 1};
const int MAXN=1000+100;
int dmin[MAXN][20];
char a[MAXN];
int ans[MAXN];
int minn(int i,int j)//注意这一点
{
    if(a[i]<=a[j])
        return i;
    return j;
}
void initMin(int n)
{
    for(int i=0;i<n;++i)
        dmin[i][0]=i;
    for(int i=1;(1<<i)<=n;++i)
    {
        for(int j=0;j+(1<<i)-1<n;++j)
            dmin[j][i]=minn(dmin[j][i-1],dmin[j+(1<<(i-1))][i-1]);
    }
}
int getMin(int l,int r)
{
    int k=0;
    while((1<<(k+1))<=r-l+1)
        k++;
    return minn(dmin[l][k],dmin[r-(1<<k)+1][k]);
}
int main()
{
    int m;
    while(~scanf("%s%d",a,&m))
    {
        int n=strlen(a);
        int p=-1;
        initMin(n);
        for(int i=1;i<=n-m;++i)
        {
            p=getMin(p+1,m+i-1);//减一是因为字符串数组的起始位置是0
            ans[i]=a[p]-'0';
        }
        int i=1;
        for( ;i<=n-m;++i)//去除前导0
            if(ans[i]!=0)
                break;
        if(i>n-m)
            printf("0\n");
        else
        {
            for( ; i<=n-m;++i)
                printf("%d",ans[i]);
            printf("\n");
        }
    }
}

 

 

 

 

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值