码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 3183 A Magic Lamp(RMQ)

时间:2014-08-05 11:23:19      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:数据结构

A Magic Lamp

                                                                              Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

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
 

题意:给出一个不超过1000位的数,求删去m个数字以后形成的最小的数是多少。

分析:我们可以把题目转化为这样一个模型:从A[1]、A[2]、……、A[n] n个数中选出n-m个数,使得组成的数最小。
一、使用RMQ,设原数字长为n,那么除去m个数字后还剩n-m个数字。
(1)因为有n-m个数字,那么在1到m+1位置中最小的那个数字必是结果中的第一个数字,记录其位置为pos
(2)然后从这个位置的下个位置pos+1开始到m+2位置的数字中最小的那个数字必定是结果中第二个数字,以此类推下去向后找。
(3)为了保证数字最小,所以要保证高位最小,还要保证数字长度满足条件
#include<cstdio>
#include<cstring>
#include<cmath>

char s[1010];
char ans[1020];
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]);
}

int main()
{
    int len, m, i;
    while(scanf("%s%d",s, &m)!=EOF)
    {
        len = strlen(s);
        RMQ_Init(len);
        m = len - m;
        int pos = 0, num = 0;
        while(m--)
        {
            pos = Query(pos, len - m - 1);
            ans[num++] = s[pos++];
        }
        for(i = 0; i < num; i++)
            if(ans[i]!='0')
                break;
        if(i == num)
            printf("0");
        else
        {
            while(i < num)
                printf("%c",ans[i++]);
        }
        puts("");
    }
    return 0;
}

二、这个题还可以直接使用贪心来求解,思路和RMQ基本相同。
#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
    string s;
    int m;
    while(cin >> s >> m) {
        int len = s.length(), i, j;
        int p = 0, tmp_pos = m, flag = 0;
        for(i = 0; i < len - m; i++) {
            char mmin = s[p];
            for(j = p + 1; j <= tmp_pos; j++)
            if(s[j] < mmin) {
                mmin = s[j];
                p = j;
            }
            tmp_pos++;
            p++;
            if(!flag) {
                if(mmin == '0')  continue;
                else {
                        cout << mmin;
                        flag = 1;
                }
            }
            else {
                cout << mmin;
            }
        }
        if(!flag)
            cout << "0";
        cout << endl;
    }
    return 0;
}


hdu 3183 A Magic Lamp(RMQ),布布扣,bubuko.com

hdu 3183 A Magic Lamp(RMQ)

标签:数据结构

原文地址:http://blog.csdn.net/lyhvoyage/article/details/38379685

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!