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

题目大意:给一个数,删除其中的m位是其值变得最小,求删除后最小值

看了样例之后,以为只要删除m个较大的数,就看可以了, 结果一直wa,后来发现 21435  1这组数据,

按照这个思路删掉最大的一个数5得到结果2143,发现这不是最小的结果,1435才是最小的结果,这种思路是不对的,

那么要让结果最小,那么如果高位数字比低位数字大的话,就删除这个高位数字,这样就让结果尽可能的小了

比如这个例子:

235564  3

要删除m = 3个数,发现2<3,3<5,5<=5,5<6,高位都不大于低位,而6>4,这时删去6(m = 2),23554, 发现5>4,删去5(m = 1)2354,

发现5>4,删去5(m = 0)234,最终结果为234,这样循环遍历,具体看代码

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>

using namespace std;

typedef long long ll;
const int N = 1010;

int main()
{
    char s[N];
    int a[N], str[N];
    int m, i;
    while(~scanf("%s%d", s, &m))
    {
        memset(str, 0, sizeof(str));
        int k = 0, l = strlen(s);
        for(i = 0 ; s[i] != ‘\0‘ ; i++)
            a[i] = s[i] - ‘0‘;
        for(i = 0 ; i < l && m > 0; i++)
        {
            if(a[i] <= a[i + 1])
                str[k++] = a[i];
            else
            {
                m--;
                while(str[k - 1] > a[i + 1] && m > 0 && k >= 1)
                {
                    m--;
                    k--;
                }
            }
        }
        for(; i < l ; i++)
            str[k++] = a[i];
        for(i = 0 ; i < k - m ; i++)
            if(str[i] != 0)
                break;
        if(i == k - m)
            printf("0");
        else
        {
            for(int j = i ; j < k - m ; j++)
                printf("%d", str[j]);
        }
        printf("\n");
    }
    return 0;
}
时间: 2024-10-14 10:39:04

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

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)

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

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

A Magic Lamp(贪心)

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

HDU 4149 Magic Potion

题意: a[i] ^ x = f[i] ( i = 1...8 ) 和 ( a[1] + a[2] + ... + a[8] ) ^ x = f[9] 现在f为已知  求x 思路: 从低位到高位确定x值  做法见注释 代码: #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int f[20],t,ans; int main() { int i,k,j; scanf(&

hdu 5125 magic balls(dp)

题目链接:hdu 5125 magic balls #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 1005; int N, M, dp[maxn][maxn], A[maxn], B[maxn]; int vec[maxn][maxn], c[maxn]; void init () { scanf("%d%d&quo