题意:有一个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;
}

  

UVA - 11491 Erasing and Winning(奖品的价值)(贪心)的更多相关文章

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

    题意:给你一个n位整数,让你删掉d个数字,剩下的数字要尽量大. 题解:因为最后数字位数是确定的,而且低位数字对答案的贡献是一定不及高位数字的,所以优先选择选最大且最靠左边的数字,但是有一个限制,选完这 ...

  2. 【思路、优化】UVa 11491 - Erasing and Winning

    Juliano is a fan of the TV show Erasing and Winning, where participants are selected in a draw and r ...

  3. UVA 11491 Erasing and Winning

    题意: 给你一个n位整数,让你删掉d个数字,剩下的数字要尽量大. 分析: 用了vector数组模拟.如果当前要插入的数>vector数组里的最后一位数,就替换且d-- 代码: #include ...

  4. UVa 11491 Erasing and Winning (贪心,单调队列或暴力)

    题意:给一个数字(开头非0),拿掉其中的d个数字,使剩下的数字最大(前后顺序不能变). 析:拿掉d个数字,还剩下n-d个数字.相当于从n个数字中按先后顺序选出n-d个数字使组成的数字最大,当然采用窗口 ...

  5. uva 11491:Erasing and Winning(贪心)

    题意:给一个长n(n<10^5)位的数,删除d位,求删除后最大的数.(原数无前导0) 思路:从前往后扫,如果a[i] > a[i-1],则删除a[i-1].我暴力的用链表实现了…… #in ...

  6. 【uva 11491】Erasing and Winning(算法效率--贪心+单调队列)

    题意:有一个N位整数,要求输出删除其中D个数字之后的最大整数. 解法:贪心.(P.S.要小心,我WA了2次...)由于规定了整数的位数,那么我们要尽量让高位的数字大一些,也就是要尽量删去前面小的数字. ...

  7. UVA11491 奖品的价值

    奖品的价值C804 运行时间限制:1000ms: 运行空间限制:51200KB 试题描述 你是一个电视节目的获奖嘉宾.主持人在黑板上写出一个 n 位非负整数(不以 0 开头),邀请你删除其中的 d 个 ...

  8. uva11491 奖品的价值(贪心)

    uva11491 奖品的价值(贪心) 给你一个n位的整数,请你删除其中的d个数字,使得整数尽可能大.1<=d<n<=1e5. 首先因为前面的数位更重要,所以从左往右将每一位数字加入栈 ...

  9. UVA LA 7146 2014上海亚洲赛(贪心)

    option=com_onlinejudge&Itemid=8&page=show_problem&category=648&problem=5158&mosm ...

随机推荐

  1. android sqlite 图片保存和读出 用流 转字节码

    原文:http://blog.sina.com.cn/s/blog_8cfbb99201012oqn.html package com.yiyiweixiao; import android.cont ...

  2. VUE 父子组件之间通信传值 props和 $emit

    1.父组件传值给子组件 $props,子组件传值给父组件 $emit 父组件          <div id="app" >               <tr ...

  3. 2019护网杯baby_forensic

    题目名称:baby_forensic题目描述:can you catch the flag?附件:“data.7z” 2019护网杯初赛的一道取证题,比赛时没做出来,赛后又研究了一下. 获取profi ...

  4. 二、Linux目录结构&常用指令

    Linux目录结构: ps -ef:任务管理器 ifconfig: 查看ip ping :  测试与目标主机的连通性,ctrl+c停止 目录指令: ll:列出当前目录下的文件信息 ls -al : 列 ...

  5. firewalld学习-zone的使用和配置

    原文地址:http://www.excelib.com/article/290/show zone文件标签名词解释 target:目标,这个前面学生也已经给大家介绍过了,可以理解为默认行为,有四个可选 ...

  6. nohup command 2>&1 & 的含义

    nohup command 2>&1 &的含义: nohup:no hang up,意思是不挂断.表示永久执行命令,哪怕当前终端已经退出登录. 并且命令前面添加nohup之后,会 ...

  7. 软件构造 Lab1

    大二软件构造第一次实验 本人本次实验操作系统:macOS high Sierra 10.13.3 任务一:MagicSquare 对于本任务,主要需要实现两个方法,一个是isLegalMagicSqu ...

  8. 【LeetCode】101. 对称二叉树

    题目 给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 / \ 2 2 / \ / \ 3 4 4 3 但是下面这个 [1,2,2,null,3, ...

  9. GNS3 模拟Arp命令1

    R1 : conf t int f0/0 no shutdown ip add 192.168.1.1 255.255.255.0 end R2: conf t int f0/0 no shutdow ...

  10. mysql IF-IFNULL和IF-ISNULL同样逻辑的运行差别

    首先,目标记录是存在的 SELECT * FROM d_device_user_bind dub WHERE dub.`uid`='222222222221' 其次, SELECT dub.uid,d ...