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每日一题】20170207

    20170206问题解析请点击今日问题下方的“[Java每日一题]20170207”查看(问题解析在公众号首发,公众号ID:weknow619) package Feb2017; public cla ...

  2. js 1.变量提升 2.条件语句 3.循环语句 4.加号+的使用

    1.变量提升 变量提升是浏览器的一个功能,在运行js 代码执行前,浏览器会给js一个全局作用域叫 window,window 分两个模块,一个叫运营模块,内存模块找到当前作用域下的所有带var和fun ...

  3. JavaScript面向对象编程指南(六) 继承

    第6章 继承 6.1 原型链 6.1.1原型链示例 原型链法:Child.prototype=new Parent(); <script> function Shape(){ this.n ...

  4. Android为TV端助力 布局、绘制、内存泄露、响应速度、listview和bitmap、线程优化以及一些优化的建议!

    1.布局优化 首先删除布局中无用的控件和层级,其次有选择地使用性能较低的viewgroup,比如布局中既可以使用RelativeLayout和LinearLayout,那我们就采用LinearLayo ...

  5. (网页)angularJs中怎么模拟jQuery中的this?(转)

    转载自mini_fan博客园: 今天想在Angular项目中使用jQuery的this功能,发现undefined.代码如下: HTML部分: <p ng-click="testCli ...

  6. Android Room框架学习笔记

    一.使用 1.build.gradle引入 compile "android.arch.persistence.room:runtime:1.0.0" annotationProc ...

  7. [20190211]简单测试端口是否打开.txt

    [20190211]简单测试端口是否打开.txt --//昨天看一个链接,提到如果判断一个端口是否打开可以简单执行如下:--//参考链接:https://dba010.com/2019/02/04/c ...

  8. vs添加到附加进程调试(IIS页面调试)

    有时候单元测试不是很方便,通过页面调试接口会更直观,也跟容易发现问题(尤其是在页面传参的时候),这时vs添加到附加进程的调试方式就显得尤为重要了! 步骤如下: 1.首先是通过IIS建立网站,(前提是要 ...

  9. js获取地址栏中的数据

    window.location.href:设置或获取整个 URL 为字符串window.location.pathname:设置或获取对象指定的文件名或路径window.location.search ...

  10. 单用户实例添加DB账号

    停止实例 net stop mssqlserver 以单用户启动实例,指定以sqlcmd连接 net start mssqlserver /m"SQLCMD" 以单用户启动实例,指 ...