【HDU】3183A Magic Lamp(RMQ实现,贪心同理)

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


 

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

 

 

Source

HDU 2009-11 Programming Contest

 

 

Recommend

lcy   |   We have carefully selected several similar problems for you:  3188 3189 3184 3185 3186 

 

 

题目大意:给你一个位数不超过1000的数,现在让你删除其中的M个数,问你删除M个数之后的最小的数是多少。

只能删除,不能给人家改变顺序!

思路:

首先,我们可以知道如果有N位数字,要删去M个数字,那么第一个数字我们一定会在1~m+1这个区间内去找某一位最小的值,很容易就想出来了,不然的话后面的个数是不够组成一个N-M位数的,

而下一个第二个数呢,比如,我们选择了第p位上的数作为第一个数。那么第二位上的数就应该在p~m+2这个区间内选择,

后面的数字就以此类推即可,直到我们找到n-m个数,这个时候就是最优的

 

还有就是我们这个题目是用的RMQ实现的,而一般RMQ都是返回的是我们需要的值,这个地方只需要改成返回某一位即可。

这个输出的小失误就不提了,心痛~,希望大家慢慢写输出吧。。。。

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int MAXN=10001;
char a[MAXN];


int minn(int i,int j)
{
    if(a[i]<=a[j])
        return i;
    return j;
}
int dmin[MAXN][20];

void initmin(int n)
{
    for(int i=0; i<n; i++)
        dmin[i][0]=i;
    for(int j=1; (1<<j)<=n; j++)
        for(int i=0; i+(1<<j)-1<n; i++)
            dmin[i][j]=minn(dmin[i][j-1],dmin[i+(1<<(j-1))][j-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 ans[MAXN];
int main()
{
    int m;
    while(scanf("%s%d",a,&m)==2)
    {
        int n=strlen(a);
        initmin(n);
        int p=-1;
        for(int i=1; i<=n-m; i++)
        {
            p=getmin(p+1,m+i-1);
            ans[i]=a[p]-'0';
        }
        bool flag=false;
        for(int i=1;i<=n-m;i++)
        {
            if(ans[i]==0&&!flag) continue;
            flag=true;
            printf("%d",ans[i]);
        }
        if(!flag)
            printf("0\n");
        else
            printf("\n");
        

    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值