A Magic Lamp(HDU-3183)(RMQ 与 贪心)

来源:互联网 发布:软件算无形资产吗 编辑:程序博客网 时间:2024/06/08 06:33

A Magic Lamp

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

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 1100001 212345 254321 2
 
Sample Output
1310123321

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

题目大意:给出一个长度1000以内数字,再给出要删除的数字个数n,求得删除n个数字之后最小的数不包括前导0。
    这个题中虽然说n是小于数字的长度,最后给出的数据好像有超出范围的可以直接特判一下。

题解:  两种方法

贪心:

 这个题可以有两种方法实现,看题目给出的数据可以得出规律,每次删除的数都是比后一位要大的数。可以得出贪心的方法

首先考虑对于n个数字组成的数,只删除1位的情况。

        比如176832,删除一位使得剩下的数值最小。结果是删除7而不是删除8所以可知并不总是删除最大的那个数字。

        一种可行的贪心策略是:对于n位数构成的数删除m位,每次总是删除这样的a[i]:它是第一个a[i]>a[i+1]的数,如果不存在则就删除a[n]。如何证明给贪心策略是正确的呢?

        假设始终不删除a[i],那么最终m位数就必然包含a[i]。但其实a[i]>a[i+1],所以我们完全可以删除a[i],然后让a[i+1]在a[i]最终的位置上,那么得到的m位数自然更小了。所以a[i]必定要被删除的。以此类推,贪心得证。

代码如下:

#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <cmath>#include <queue>#include <vector>using namespace std;const int N=2099;char str[N];int num[N];int main(){    int n,i,j;    while(scanf(" %s%d",str,&n)!=EOF)    {        memset(num,0,sizeof(num));        int len=strlen(str);        for(i=0;i<len;i++)            num[i]=str[i]-'0';        if(n>len)        {            printf("0\n");            continue;        }        while(n--)        {            for(i=0;i<len;i++)            {                if(num[i]>0)                {                    for(j=i+1;j<len;j++)                        if(num[j]>=0)                            break;                    if(num[i]>num[j]) //删除前面比后面大的那一位                    {                        num[i]=-1;                        break;                    }                }            }        }        int flag=0;        for(i=0;i<len;i++)        {            if(num[i]==-1) continue;//跳过被删除的数            if(num[i]>0) //避免出现前导0                flag=1;            if(flag)            printf("%d",num[i]);        }        if(flag==0) printf("0"); //如果都是0则输出一个0        printf("\n");    }    return 0;}

RMQ:

对于一个序列A[1...N],一共N个数,除去M个数使剩下的数组成的整数最小。

也就是说在A[1...N]中顺次选取N-M个数,使值最小。

本题很有技巧性,一开始我总是想不明白,后来在纸上画了一下,大概明白了是怎么回事。

它主要是基于以下事实:

对于序列A[1...N],选取N-M个数,使组成的值最小,而且顺序不能交换,既然要选取N-M个,那么可以容易知道这N-M位数的第一位一定在数组A中的区间

[1M+1]中出现,为什么是这样呢?

我们可以这样来模拟一下:假设A数组就只有6个数,分别是A[1]A[2]A[3]A[4]A[5]A[6],我们去掉M=2个数,使形成的值最小。

那么我们此时的N=6,M=2N-M=4

则我们说形成的4位数的第一位一定在区间[13]中出现,因为如果区间范围再大点,比如[14],这样就不科学了,因为第一位一定不会是A[4],更不会是

A[5]A[6],我们假设可以是A[4],那么后面只有A[5]A[6]两位数了,这样的话最多只可能形成3位数,绝对不能形成N-M=4位了。

当然到了这里,我们就可以这样做了,第一位可以在区间[1M+1]里面找,假设第一位在位置x,因为第二位肯定在第一位的后面,所以第二位一定存在于

区间[x+1M+2],为什么是M+2,因为第一位已经确定了,现在只需要确定N-M-1位了,所以区间就可以向后增加1,一直这样循环下去,就可以找到了。

代码如下:

#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <cmath>#include <queue>using namespace std;typedef long long LL;const int N=1099;int dp[N][30],a[N],num[N];void RMQ(int n){    for(int i=0; i<n; i++)        dp[i][0]=i;    for(int j=1; (1<<j)<=n; j++)    {        for(int i=0; (i+(1<<j)-1)<n; i++)        {            int a=dp[i][j-1];            int b=dp[i+(1<<(j-1))][j-1];            if(num[a]<=num[b]) dp[i][j]=a;            else dp[i][j]=b;        }    }}int f(int l,int r){    int k=(int)(log(r-l+1.0)/log(2.0));    int b=dp[l][k];    int c=dp[r-(1<<k)+1][k];    if(num[b]<=num[c]) return b;    else return c;}int main(){    char str[N];    int n;    while(scanf(" %s%d",str,&n)!=EOF)    {        int i,j,len;        len=strlen(str);        for(i=0;i<len;i++)            num[i]=str[i]-'0';        RMQ(len);        int m=len-n;//要取得数字的长度        i=j=0;        while(m--)        {            i=f(i,len-m-1);             a[j++]=num[i++];        }        for(i=0;i<j;i++)        {            if(a[i]!=0)               break;        }        if(i==j)        {            printf("0\n");            continue;        }        while(i<j)        {            printf("%d",a[i]);            i++;        }        printf("\n");    }    return 0;}












原创粉丝点击