HDU 3183 A Magic Lamp

  第一种做法是贪心做法,只要前面的数比后面的大就把他删掉,这种做法是正确的,也比较好理解,这里就不说了,我比较想说一下ST算法,RMQ的应用

主要是返回数组的下标,RMQ要改成<=(这里是个坑点,取连续数是可以的),他的转移方程为x = dp[i-1][j],y = dp[i-1][j+1<<(i-1)];

dp[i][j] = a[x] <= a[y] ? x : y;  这里既是转化为取n-m个数,首先在1 ~ m+1中必然选一个数,所以这个数就是n-m数中第一个数,记录所选的位置id,下次便是id+1,m++;

m++ = id + m + 1 - (id - 1),反证法可以很好的证明上述结论,稍微有点难想,如此看来,dp[i][j]将返回i 到 i+2<<j-1的最小值的下标;

说到这里我们应该感受到了,重要的是理解选数的过程,RMQ只是个优化...这个思维过程还是希望读者好好体会一下的.

#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;
}
时间: 2024-12-20 22:54:57

HDU 3183 A Magic Lamp的相关文章

hdu 3183 A Magic Lamp(RMQ)

题目链接:hdu 3183 A Magic Lamp 题目大意:给定一个字符串,然后最多删除K个,使得剩下的组成的数值最小. 解题思路:问题等价与取N-M个数,每次取的时候保证后面能取的个数足够,并且取的数最小,查询最小的操作用RMQ优化. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 10005; int N, M, d[m

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, u

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

HDU 3183 A Magic Lamp(贪心+RMQ)

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 allowe

HDU 3183 A Magic Lamp(RMQ问题, ST算法)

原题目 A Magic Lamp Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3964    Accepted Submission(s): 1605 Problem Description Kiki likes traveling. One day she finds a magic lamp, unfortunately the

hdu 3183 A Magic Lamp(给一个n位的数,从中删去m个数字,使得剩下的数字组成的数最小(顺序不能变),然后输出)

1.题目大意是,给你一个1000位的数,要你删掉m个为,求结果最小数. 思路:在n个位里面删除m个位,也就是找出n-m个位组成最小数 所以在区间 [0, m]里面找最小的数,对应的下标标号i 接着找区间 [i+1,m++]里面的最小数,对于下标为ii 接着找区间 [ii+1,m++]里面的最小数-- 这样就会找n-m个数了.区间这样安排的目的是为了保证取出来的数的顺序. 2代码: #include<cstdio> #include<cstring> #include<cmat

hdu 3183 A Magic Lamp (贪心)

///给你一数字,删除其中的m位使其最小 ///贪心:前面的数要小于后面的数 # include <stdio.h> # include <algorithm> # include <iostream> # include <string.h> # include <math.h> using namespace std; char str[1010]; int a[1010]; int c[1010]; int main() { int k,m

【HDOJ】3183 A Magic Lamp

RMQ. 1 /* 3183 */ 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 6 #define MAXN 1005 7 8 char s[MAXN], ans[MAXN]; 9 int dp[MAXN][MAXN]; 10 int n,len,m; 11 12 int min(int x, int y) { 13 return s[x]<=s[y] ? x:y; 14 } 15

hdu A Magic Lamp

http://acm.hdu.edu.cn/showproblem.php?pid=3183 A Magic Lamp Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2556    Accepted Submission(s): 999 Problem Description Kiki likes traveling. One day