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. 前端学习 之 Bootstrap(二)

    一.代码 内联代码:用<code>包裹,但是需要用<和>表示尖括号. 键盘输入:用<kbd>包裹表示键盘输入的内容. 多行代码:用<pre>包裹多行代码 ...

  2. 贝塞尔曲线与CSS3动画、SVG和canvas的应用

    简介 贝塞尔曲线是可以做出很多复杂的效果来的,比如弹跳球的复杂动画效果,首先加速下降,停止,然后弹起时逐渐减速的效果. 使用贝塞尔曲线常用的两个网址如下: 缓动函数:http://www.xuanfe ...

  3. Android为TV端助力 同时setTag两次,保存多种值

    示例代码: view.setTag(R.string.action_settings,hodler.content); 接收两个值,一个是key值,必须是唯一值,而且要写在values/ids.xml ...

  4. Android为TV端助力 SharedPreferences 轻量级存储!

    首先在当前进程也就是当前的项目里面进行存储 SharedPreferences.Editor editor = mContext.getSharedPreferences("tvplay&q ...

  5. Implemented the “Importance Sampling of Reflections from Hair Fibers”

      Just the indirect specular pass by importance sampling. With all layers. Manually traced by 3D Ham ...

  6. WPF:解决DataGrid横向滚动条无法显示的问题

    DataGrid的最后一列的宽度设置为“Width=”auto””即可. 如果显示指定长度或者设置为“*”,那么不管怎么拖动列头,或者不管行里面的内容有没有超过DataGrid的显示区域,DataGr ...

  7. git 入门教程之个性化 git

    前情概要 初识 git 时,我们就已经接触过 git 的基本配置,使用 git config 命令配置用户名和邮箱: # 配置当前项目(`local`)的用户名(`snowdreams1006`) g ...

  8. (网页)websocket例子

    转载自博客园张果package action; import javax.websocket.CloseReason; import javax.websocket.OnClose; import j ...

  9. [20180914]oracle 12c 表 full_hash_value如何计算.txt

    [20180914]oracle 12c 表 full_hash_value如何计算.txt --//昨天在12c下看表full_hash_value与11g的full_hash_value不同,不过 ...

  10. MSSQL-sql server-视图简介

    转自:http://www.maomao365.com/?p=4511 一.视图简介 视图在MSSQL中是一张虚拟表. 视图的数据由sql语句定义生成,视图中指定新生成数据的列名称和数据格式,视图中的 ...