UVA 11491 Erasing and Winning(奖品的价值)(贪心)

题意:有一个n位整数(不以0开头),要求删除其中的d个数字,使结果尽量大。(1<=d<n<=10^5)

分析:

1、从头扫一遍,如果当前填的数字小于n-d,则将当前数字填上。

2、如果已经的填的数字个数加上当前位置及其后的所有数字个数>n-d,即在当前位置上还有足够多的数可以填写,即cnt + (n-i) > n - d,则删除数组ans中比当前数字小的数字,因为要保证,最后剩的n-d位数的高位尽可能大。

#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const double eps = 1e-8;
const int MAXN = 1e5 + 10;
const int MAXT = 10000 + 10;
using namespace std;
char ans[MAXN];
int main(){
    int n, d;
    while(scanf("%d%d", &n, &d) == 2){
        if(!n && !d) return 0;
        getchar();
        int cnt = 0;
        for(int i = 0; i < n; ++i){
            char c = getchar();
            while(cnt > 0 && cnt + d - i > 0 && ans[cnt] < c){
                --cnt;
            }
            if(cnt < n - d){
                ans[++cnt] = c;
            }
        }
        ans[++cnt] = '\0';
        printf("%s\n", ans + 1);
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/tyty-Somnuspoppy/p/6375835.html