hdu 3183 A Magic Lamp(贪心,RMQ)

A Magic Lamp

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

点击打开链接

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

题目大意就是给你一个数字串,从中间删除 d 个数字,在不改变原有顺序的情况下,得到的最终的数字串最小。
刚一开始我就想到了搜索,枚举每一个点然后再来一次,这样子最坏的情况下复杂度可能到达 1000! 肯定是不可以的,然后一想,字符串的长度是给定的,删除的个数也是给定的,那么最终串的长度也是给定的,因此就有了贪心的想法。

字符串的长度是 n ,需要删除的个数是 d 因此最终串的长度是 n-d 。
需要一个数字一个数字的找出答案,而且不能改变原有的顺序,因此第一个答案一定只能在前 d+1 个数字里,也就是区间【1,d+1】,因为最坏的情况是第 d+1 个数字当选,然后后面剩余 n-d-1 个数字,加起来正好是 n-d 个数字,因此在拿到每一个数字的时候一定要考虑到给下一次搜索足够的数字个数。 

假设当前找到的数字下表为 x ,那么下一次需要找的区间是 【x+1,d++】(考虑到给下一次搜索足够的数字个数)。(也是因为这里我WA了好几发)

搜到最后一个点也就结束了,答案就出来了,输出时注意处理下前导 0 。

AC 代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>

#define SIZE 1050
#define Maxn 1000
using namespace std;
typedef long long ll;

char a[SIZE],ans[SIZE];
int d,n,len,cnt,flag;
void DFS(int i)
{
    if(flag) return ;
    if(n <= len-cnt)        //数组里剩余数字正好填补剩余空缺
    {
        for(int z = i; z < (int)strlen(a) ; z ++)
            ans[cnt ++] = a[z];
        flag = 1;
        return ;
    }
    int x = i;
    char minn = a[i];

    int j;      //找到当前区域里的最小值
    for( j = i+1 ; j <= d && j < (int)strlen(a) ; j ++)
    {
        if(minn > a[j])
        {
            minn = a[j];x = j;
        }
    }
    ans[cnt ++] = a[x];
    if(cnt == len)      //找完了所有数字
    {
        flag =1;return ;
    }
    n -= x-i+1;     //数组剩余的数字个数
    d ++;	//WA的地方
    DFS(x+1);
}
void output()
{
    int i;
    for(i = 0 ; i < len-1 ; i ++)
    {
        if(ans[i] != '0')break;;
    }
    for( ; i < len-1; i ++)
    {
        printf("%c",ans[i]);
    }
    printf("%c\n",ans[i]);
}
int main()
{
    freopen("in.txt","r",stdin);
    while(scanf("%s%d",a,&d)!=EOF)
    {
        n = strlen(a);
        if(d >= n){printf("0\n");continue;}
        len = n - d;
        flag = cnt = 0;
        DFS(0);
        output();
        memset(ans,0,sizeof(ans));
    }
    return 0;
}

RMQ  也可以写出来,思路是一样的,一次找到区间的最小值。
char s[1010];       //目标数组
int st[1010][20];       //区间最值数组

int Min(int x,int y)
{
    return s[x] <= s[y] ? x : y;
}

void RMQ_Init(int len)
{
    for(int i = 0; i < len; i++)
    st[i][0] = i;
    for(int j = 1; (1<<j) < len; j++)
    for(int i = 0; i+(1<<j)-1 < len;i++)
    st[i][j] = Min(st[i][j-1],st[i+(1<<(j-1))][j-1]);
}

int Query(int l,int r)
{
    int k = (int)(log((double)(r-l+1))/log(2.0));
    return Min(st[l][k],st[r-(1<<k)+1][k]);
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值