题意:有一个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. SpringCloud 跨域访问cors

    import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Co ...

  2. java学习-初级入门-面向对象①-面向对象概述-结构化程序设计

    为了学习面向对象程序设计,今天我们先利用面向对象以前的知识,设计一个学生类. 要求进行结构化程序设计. 学生类: Student 要求:存储学生的基本信息(姓名.性别.学历层次和年级),实现学生信息的 ...

  3. Lamda简单使用

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  4. MySQL存储过程例子

    -- 索引 INDEXCREATE INDEX idx_sname ON student( sname(4)); ALTER TABLE teacher add index idx_tname(tna ...

  5. Django(十八)后台管理:列表页选项、编辑页选项、自定义后台页面

    [参考]https://blog.csdn.net/u010132177/article/details/103814357 [参考]https://docs.djangoproject.com/zh ...

  6. Java 实现 POS 打印机无驱打印

    来源:https://www.ibm.com/developerworks/cn/java/j-lo-pos/index.html 行业需求 我们是一家专业做酒店餐饮软件的公司,餐饮软件一个重要的功能 ...

  7. 5G/NR OTA (Over The Air) 测试详解

    原文链接:http://www.sharetechnote.com/html/5G/5G_OTA.html 1 什么是OTA (Over The Air) OTA代表Over The Air.为了使用 ...

  8. 19 包含min函数的栈

    题目描述 定义栈的数据结构,请在该类型中实现一个能够得到栈最小元素的min函数.   思路:一个栈存普通元素,一个最小栈存放目前位置最小的元素,只在压入的时候判断是否为空以及最小元素,其他情况正常处理 ...

  9. dataGridView与数据源dataTable同步排序

    private void dataGridView1_Sorted(object sender, EventArgs e)         {             string _sortStr ...

  10. ROS-1 : Ubuntu16.04中安装ROS Kinetic

    1.安装 ROS Kinetic仅支持Wily(Ubuntu 15.10).Xenial( Ubuntu16.04)和Jessie(Debian 8)的debian软件包. 1.1 配置Ubuntu ...