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,i,tt,len,j;
    while(~scanf("%s %d",str,&m))
    {
        len=strlen(str);
        for(i=0; i<len; i++)
            a[i]=str[i]-'0';
        k=0;
        for(i=0;i<len-1&&m>0;i++)
        {
            if(a[i]<=a[i+1])
            {
                c[k++]=a[i];
            }
            else
            {
                m--;
                while(c[k-1]>a[i+1]&&m>0)
                {
                    k--;
                    m--;
                }
            }
        }
        for(;i<len;i++)
            c[k++]=a[i];
    //    for(i=0;i<k;i++)
     //       printf("%d",c[i]);
   //     printf("\n");
        for(i=0;i<k-m;i++)
        {
            if(c[i]!=0)
                break;
        }
        if(i==k-m)
            printf("0");
        else
        {
            for(j=i;j<k-m;j++)
                printf("%d",c[j]);
        }
        printf("\n");
    }
    return 0;
}

时间: 2024-10-08 01:58:55

hdu 3183 A Magic Lamp (贪心)的相关文章

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)

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)

题目链接: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, unfortunately the genie in the lamp is not so kind. Kiki must answer a question

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

第一种做法是贪心做法,只要前面的数比后面的大就把他删掉,这种做法是正确的,也比较好理解,这里就不说了,我比较想说一下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数中第一个数,记录所选的位置

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(贪心)

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

【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