Problem UVA11491-Erasing ans Winning

Accept: 799  Submit: 5753
Time Limit: 3000 mSec

Problem Description

Juliano is a fan of the TV show Erasing and Winning, where participants are selected in a draw and receive money for taking part in the show. In the show, the presenter writes a number of N digits in a board. The participant must then erase exactly D digits from the number in the board; the number formed by the remaining digits is the value of the money prize for the participant. Juliano was at last selected to take part in the show, and asked you to write a program that, given the number the presenter wrote in the board, and the number of digits Juliano must erase, determines the highest value of the prize he can win.

Input

The input contains several test cases. The first line of a test case contains two integers N and D (1 ≤ D < N ≤ 105) indicating respectively the number of digits of the number the presenter wrote in the board and the number of digits that must be erased. The next line contains the number the presenter wrote; the number does not start with a zero. The end of input is indicated by a line containing only two zeros, separated by a space.

 Output

For each test case in the input your program must produce one single line in the output, containing the highest prize Juliano can win.
 

 Sample Input

4 2
3759
6 3
123123
7 4
1000000
0 0
 

 Sample Output

79
323
100

题解:这个题贪心的思路很明显,但是不同的贪心策略在效率上差距很大(我的效率就很差),我的思路很直接,要保留d个,那么答案的最高位肯定是前d+1个数的最大值,找到之后删去它前面的,继续找,如果删到最后发现d还是大于0,就说明在已经找到的答案串中还需要继续删,就把答案串当作原始串继续删,最后输出即可。交上去虽然A了,但是用了770ms,然后我惊奇地发现大佬们都是0ms过,找了找题解,发现了很好的做法(orz),大家可以作为参考。

博客的链接如下:

https://www.cnblogs.com/zyb993963526/p/6354314.html

 #include <bits/stdc++.h>

 using namespace std;

 int n, d;

 int main()
{
//freopen("input.txt", "r", stdin);
while (~scanf("%d%d", &n, &d) && (n || d)) {
string num, ans = "";
cin >> num; bool flag = false;
int pos = , len = num.length();
while (d) {
if (d >= len-pos) {
flag = true;
d -= len - pos;
num = ans;
continue;
}
char Max = ;
int i, p;
for (i = pos; i < pos + d + && i < len; i++) {
if (Max < num[i]) {
Max = num[i];
p = i;
}
}
ans += num[p];
d -= p - pos;
pos = p + ;
} if (!flag) ans += num.substr(pos, num.size() - pos);
cout << ans << endl;
}
return ;
}

UVA11491-Erasing ans Winning(贪心)的更多相关文章

  1. uva11491 Erasing and Winning

    边读入边处理 优化了速度一开始有想错了的地方.处理输入有点想用stringstream, 的问题在于他把字符串连续的数字作为一个整体,遇到空格才分开,所以不适用 #include<cstdio& ...

  2. UVA-11491 Erasing and Winning (单调队列)

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

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

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

  4. 【思路、优化】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 ...

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

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

  6. Erasing and Winning UVA - 11491 贪心

    题目:题目链接 思路:不难发现,要使整体尽量大,应先满足高位尽量大,按这个思路优先满足高位即可 AC代码: #include <iostream> #include <cstdio& ...

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

    题意:有一个n位整数(不以0开头),要求删除其中的d个数字,使结果尽量大.(1<=d<n<=10^5) 分析: 1.从头扫一遍,如果当前填的数字小于n-d,则将当前数字填上. 2.如 ...

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

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

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

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

随机推荐

  1. Java高并发--线程安全策略

    Java高并发--线程安全策略 主要是学习慕课网实战视频<Java并发编程入门与高并发面试>的笔记 不可变对象 发布不可变对象可保证线程安全. 实现不可变对象有哪些要注意的地方?比如JDK ...

  2. cloudera manager 安装配置

    前面cloudera manager 环境准备和安装我参考的是: https://blog.csdn.net/m0_38017084/article/details/82218559 这篇博客,写的非 ...

  3. 汇编语言--微机CPU的指令系统(五)(算术运算指令)

    (3)算术运算指令 算术运算指令是反映CPU计算能力的一组指令,也是编程时经常使用的一组指令.它包括:加.减.乘.除及其相关的辅助指令. 该组指令的操作数可以是8位.16位和32位(80386+).当 ...

  4. 初识 Java-监听器

    使用Listener类当java  web应用程序在web容器中运行时,在java web应用程序内部会不断发生各种事件,例如web应用的启动,暂停,销毁等.以及web应用中session开始和结束 ...

  5. CSS单位【记录】

    1.长度 2.角度 3.时间 4.分辨率 5.颜色 6.函数 7.生成内容 8.图像 9.数字 1.长度 <length>:数字和单位之间没有空格,0之后的长度单位是可选的 相对长度单位 ...

  6. [新特性]PeopleTools8.54+:PeopleSoft Application Engine新特性

    PeopleTools 8.54 的Application Engine 已经被更新,特别是在AE跟踪设置中有了更多的选项,本文将帮助您了解8.54的新AE特性以及如何使用这些特性. AE trace ...

  7. getCacheDir()、getFilesDir()、getExternalFilesDir()、getExternalCacheDir()

    一.getCacheDir.getCacheDir getCacheDir()方法用于获取/data/data//cache目录 getFilesDir()方法用于获取/data/data//file ...

  8. uni-app 如何在当前页调上个页面的方法

    1.获取上个页面 var pages = getCurrentPages();//当前页 var beforePage = pages[pages.length - 2];//上个页面 2.在当前页调 ...

  9. split 分割 字符串(分隔符如:* ^ : | , .)

    [1]单个符号作为分隔符 String address="上海|上海市|闵行区|吴中路"; String[] splitAddress=address.split("\\ ...

  10. zabbix监控tomcat(使用jmx监控,但不使用系统自带模版)

    一,zabbx使用jmx监控tomcat的原理分析 1.Zabbix-Server找Zabbix-Java-Gateway获取Java数据 2.Zabbix-Java-Gateway找Java程序(j ...