HDU 3183 一盏神奇的灯

A Magic Lamp

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


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
 


刚开始学rmq,光以为它是求某一区间的最小值与最大值,而没有学会变通,其实也可以用于记录最小值与最大值的下标,就如本题

它让选择m个数去掉,但是剩下的数要保持原来的顺序,如果你按照它题意去想,则要挑第一大的,第二大的,第三大的数去掉,可想而知这样做必然是很麻烦的,此时就不如反过来想它让去掉m个数,我们不如从中选n-m个数出来,而这n-m数组成的数要想是最小的,必然的高位的数应该是越小越好,而且仔细想一下原数从第一位到第m+1位其中的最小的值肯定是新数的第一位,如果不成立的话,必然在m+2后必然存在一个数来充当新数的最高位,那么n-m位的要求不就达不到了么,所以这样做是对的。将第一位的位置记为pos,

依次类推去找新数的下一位。

当然我们已经分析出来高位数字必然是越小越好,且原数第一位到m+1位的最小值必然是新数的第一位,那么这一题也可以用贪心来做,最后再贴出贪心的代码,不过这个不是我写的。

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int len;
int a[1005];
int m;
int d[1005][20];
char c[1005];
int ans[1005];
void rmq()
{
    for(int i=0; i<len; i++)
    {
        d[i][0]=i;
    }
    for(int j=1; (1<<j)<=len; j++)
        for(int i=0; i+(1<<j)-1<len; i++)
        {
            if(a[d[i][j-1]]<=a[d[i+(1<<(j-1))][j-1]])
                    d[i][j]=d[i][j-1];
                    else
                        d[i][j]=d[i+(1<<(j-1))][j-1];
            }
}
int rmq_find(int l,int r)
{
    int k=0;
    while((1<<(k+1))<=r-l+1) k++;
    if(a[d[l][k]]>a[d[r-(1<<k)+1][k]])
        return d[r-(1<<k)+1][k];
    else
        return d[l][k];
}
int main()
{
    while(~scanf("%s%d",c,&m))
    {
        len=strlen(c);
        for(int i=0; i<len; i++)
            a[i]=c[i]-'0';
        rmq();
       int t=0;
       int t1=0;
       for(int i=m;i<len;i++)
       {
           t=rmq_find(t,i);
           ans[t1]=a[t];
           t++;
           t1++;
       }
       int i;
       for(i=0;i<t1;i++)
        if(ans[i]!=0)
        break;
       if(i==t1)
        printf("0\n");
       else {
        for(int j=i;j<t1;j++)
        {
            printf("%d",ans[j]);
        }
        printf("\n");
       }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值