• hdu 3183 A Magic Lamp(RMQ)


    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;
    }


  • 相关阅读:
    How to Create a site at the specified URL and new database (CommandLine Operation)
    Using Wppackager to Package and Deploy Web Parts for Microsoft SharePoint Products and Technologies
    SQL Server Monitor v0.5 [Free tool]
    How to build Web Part
    Deploy web part in a virtual server by developing a Web Part Package file(.cab)
    How to recreate "sites" link if you delete it accidentally
    SharePoint Portal Server管理匿名访问设置
    Monitor sql connection from .Net SqlClient Data Provider
    Brief installation instruction of Sharepoint Portal Server
    How to Use SharePoint Alternate URL Access
  • 原文地址:https://www.cnblogs.com/yutingliuyl/p/7384343.html
Copyright © 2020-2023  润新知