zoukankan      html  css  js  c++  java
  • HDU-3183 A Magic Lamp--贪心+暴力数组移动

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3183

    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

    题目大意:给你一串数字,让你删去其中m个数字,使得最终的结果最小,你不能改变顺序。
    其实挺好想的,要使得最终结果最小,那么我们只需要每次删去下降的那个点就好了,如178543 4:
    8->5下降,那么我们删去8,剩下17543 3
    7->5下降,我们删去7,剩下1543 2
    5->4下降,删去5,剩下143 1
    4->3下降,删去4,剩下13 0
    然后结束了。
    当然,如果删完之后m还剩余,且数列递增,那么我们就从后往前删就好了,最后记得去掉前导零就OK了。
    以下是AC代码:
    #include <cstdio>
    #include <algorithm>
    #include <cstring>
    using namespace std;
    
    const int mac=1e3+10;
    
    char s[mac];
    
    void solve(char *s,int pos,int n)
    {
        for (int i=pos; i<n; i++)
            s[i]=s[i+1];
    }
    
    int main()
    {
        int m,n;
        while (~scanf("%s",s)){
            scanf("%d",&m);
            n=strlen(s);
            for (int i=0; i<n-1; i++){
                if (!m) break;
                while (s[i]>s[i+1] && m) solve(s,i,n--),m--,i--;
            }
            while (m){
                m--;s[--n]='';
            }
            int len=strlen(s);
            int i=0;
            while(s[i]=='0') i++;
            for (int j=i; j<len; j++) printf("%c",s[j]);
            if (i==len) printf("0");
            printf("
    ");
        }
        return 0;
    }
     
    路漫漫兮
  • 相关阅读:
    【JLOI 2015】城池攻占
    【BalticOI 2004】Sequence
    罗马游戏
    《STL源码剖析》STL迭代器分类
    《Effective C++》模版与泛型编程
    《Effective C++》继承与面向对象设计
    《Effective C++》实现 章节
    [C++]const_cast,dynamic_cast,reinterpret_cast,static_cast转型
    [C++]default constructor默认构造函数
    [C++]union联合体总结
  • 原文地址:https://www.cnblogs.com/lonely-wind-/p/12196055.html
Copyright © 2011-2022 走看看