HDU 3183 A Magic Lamp (RMQ)

Source: Internet
Author: User

Links: http://acm.hdu.edu.cn/showproblem.php?pid=3183

Topic:A Magic LampProblem 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 would realize one of her dreams.
The question is:give you a integer, you is allowed to delete exactly m digits. The left digits would form a new integer. You should make it minimum.
You aren't allowed to the order of the digits. Now can-you-help Kiki-realize her dream?

Input There is several test cases.
Each of the test case would contain an integer given (which could at most contains-digits.) and the integer m (if the I Nteger contains n digits, m won't bigger then N). The given integer would not contain leading zero.

Output for each case, output the minimum result of you can get on 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


Ideas:

Enter an integer to handle as a set of arrays. For this is the array, the total Len number, minus the n number makes the remaining number of integers the smallest. That is, in a[1 ... cold] in order to select the number of len-n, so that the value is the smallest, using the RMQ algorithm query interval (I,J) the minimum value. In order to find the smallest integer composed of len-n books. So the first digit of the integer must be found in (1,len-n+1) to satisfy the condition and the K-bit (k,len-m+1-(k-1)).

Suppose that the A array has only 6 numbers, which are a[1],a[2],a[3],a[4],a[5],a[6], we remove 2 numbers and make the resulting values minimal. Then our len-n=4 then we say that the formation of the first bit of the 4-digit number must be in the interval [1,3], if larger, then only a[5],a[6] two digits (or even less), so that the most likely to form a maximum of 3 digits, absolutely cannot form 4 bits. So the first bit can be found in the interval [1,len-n+1], assuming that the first bit is in position x, because the second bit must be in the back of the first bit, so the second bit must exist in the interval [x+1,len-m+1-(x+1-1)], now only need to determine the n-m-1 bit, So the interval can be increased backwards by 1, so the cycle continues, you can find.

The code is as follows:

<pre name= "code" class= "CPP" > #include <cstdio> #include <iostream> #include <algorithm> #

Include<cmath> #include <cstring> using namespace std;
int dpmin[1005][20];
int vis[1005], ans[1000];

Char str[1005];

    int query (int x, int y) {int q = log2 (y-x+1.0);
    if (Vis[dpmin[x][q]] > vis[dpmin[y-(1<<q) +1][q]) {return dpmin[y-(1<<q) +1][q];
    } else {return dpmin[x][q];
    }} int main () {int n;
        while (~SCANF ("%s%d", str, &n)) {int L = strlen (str);
            for (int i = 0; i < l; i + +) {vis[i+1] = str[i]-' 0 ';
        Dpmin[i+1][0] = i+1;
            } for (int j = 1; J <, J + +) for (int i = 1; i + (1<<j)-1 <= l; i + +)
                {int p = 1<< (j-1);
    if (Vis[dpmin[i][j-1]] <= Vis[dpmin[i+p][j-1]]) {dpmin[i][j] = dpmin[i][j-1];            } else {dpmin[i][j] = dpmin[i+p][j-1];
        }} int m = l-n, p = 0, k = 1;
            for (int i = 1; I <= l-n; i + +) {k = query (k,l-m+1);
            ans[p++] = vis[k++];
        m--;
        } int f = 0; 
                for (int i = 0; i < p; i + +) {if (f | | ans[i]) {cout<<ans[i];
            f = 1;
        }} if (!f) cout<< "0";
    cout<<endl;
} return 0;
 }



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.