码迷,mamicode.com
首页 > 其他好文 > 详细

uva11491 奖品的价值(贪心)

时间:2018-02-19 20:43:37      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:turn   string   namespace   ons   blog   can   出栈   down   --   

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

uva11491 奖品的价值(贪心)

标签:turn   string   namespace   ons   blog   can   出栈   down   --   

原文地址:https://www.cnblogs.com/MyNameIsPc/p/8454356.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!