zoukankan      html  css  js  c++  java
  • uva11491 奖品的价值(贪心)

    uva11491 奖品的价值(贪心)

    给你一个n位的整数,请你删除其中的d个数字,使得整数尽可能大。1<=d<n<=1e5。

    首先因为前面的数位更重要,所以从左往右将每一位数字加入栈中。如果它比栈顶的大,就把栈顶的删掉,因为这样数肯定更大。如果删满了d个,就不再弹出栈顶了。如果全部扫完都删不满d个,只需舍弃最后的数位,使被删的数达到d即可。

    #include <cstdio>
    #include <cstring>
    using namespace std;
    
    const int maxn=1e5+5;
    int n, m, lens, tail, dis;
    char s[maxn], ans[maxn];
    
    int main(){
        while (~scanf("%d%d", &n, &m)&&n){
            scanf("%s", s);
            tail=0; dis=0;
            for (int i=0; i<n; ++i){
                while (tail>0&&dis<m&&ans[tail-1]<s[i])
                    --tail, ++dis;
                ans[tail++]=s[i];
            }
            for (int i=0; i<n-m; ++i)
                printf("%c", ans[i]);
            puts("");
        }
        return 0;
    }
    
  • 相关阅读:
    在线pdm查看
    vscode
    idea for Mac 代码提示设置
    定位功能
    canvas刮奖
    jquery生成二维码
    Redux DevTools浏览器插件调试redux
    .gitignore
    HBuilder在MAC下的SVN
    UMD编码规范
  • 原文地址:https://www.cnblogs.com/MyNameIsPc/p/8454356.html
Copyright © 2011-2022 走看看