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
Sample Input
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(贪心)的更多相关文章
- uva11491 Erasing and Winning
边读入边处理 优化了速度一开始有想错了的地方.处理输入有点想用stringstream, 的问题在于他把字符串连续的数字作为一个整体,遇到空格才分开,所以不适用 #include<cstdio& ...
- UVA-11491 Erasing and Winning (单调队列)
题目大意:给一个数字(开头非0),拿掉其中的d个数字,使剩下的数字最大(前后顺序不能变). 题目分析:拿掉d个数字,还剩下n-d个数字.相当于从n个数字中按先后顺序选出n-d个数字使组成的数字最大,当 ...
- uva 11491:Erasing and Winning(贪心)
题意:给一个长n(n<10^5)位的数,删除d位,求删除后最大的数.(原数无前导0) 思路:从前往后扫,如果a[i] > a[i-1],则删除a[i-1].我暴力的用链表实现了…… #in ...
- 【思路、优化】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 ...
- UVa 11491 Erasing and Winning (贪心,单调队列或暴力)
题意:给一个数字(开头非0),拿掉其中的d个数字,使剩下的数字最大(前后顺序不能变). 析:拿掉d个数字,还剩下n-d个数字.相当于从n个数字中按先后顺序选出n-d个数字使组成的数字最大,当然采用窗口 ...
- Erasing and Winning UVA - 11491 贪心
题目:题目链接 思路:不难发现,要使整体尽量大,应先满足高位尽量大,按这个思路优先满足高位即可 AC代码: #include <iostream> #include <cstdio& ...
- UVA - 11491 Erasing and Winning(奖品的价值)(贪心)
题意:有一个n位整数(不以0开头),要求删除其中的d个数字,使结果尽量大.(1<=d<n<=10^5) 分析: 1.从头扫一遍,如果当前填的数字小于n-d,则将当前数字填上. 2.如 ...
- 【uva 11491】Erasing and Winning(算法效率--贪心+单调队列)
题意:有一个N位整数,要求输出删除其中D个数字之后的最大整数. 解法:贪心.(P.S.要小心,我WA了2次...)由于规定了整数的位数,那么我们要尽量让高位的数字大一些,也就是要尽量删去前面小的数字. ...
- UVA 11491 Erasing and Winning 奖品的价值 (贪心)
题意:给你一个n位整数,让你删掉d个数字,剩下的数字要尽量大. 题解:因为最后数字位数是确定的,而且低位数字对答案的贡献是一定不及高位数字的,所以优先选择选最大且最靠左边的数字,但是有一个限制,选完这 ...
随机推荐
- Android开发过程中的坑及解决方法收录(二)
bug 1: bug描述: 无法成功地将edittext中的内容传入数据库中 bug动图: 经过: 最近写了个项目,项目要使用到SQL数据库,由于没有相关知识,便是找到了各种资料开始了自学之旅,在de ...
- 【Tomcat】Tomcat工作原理
Tomcat 总体结构 Tomcat 的结构很复杂,但是 Tomcat 也非常的模块化,找到了 Tomcat 最核心的模块,您就抓住了 Tomcat 的“七寸”.下面是 Tomcat 的总体结构图: ...
- 修改CentOS服务器时间为北京时间
购买了VPS,CentOS系统,发现服务器时间与北京时间往往不一致,存在时差. [clive@server workspace]$ date 2018年 05月 30日 星期三 02:02:47 UT ...
- Python3 系列之 可变参数和关键字参数
刚开始接触 python 的时候,对 python 中的 *wargs (可变参数) 和 **kwargs (关键字参数)的理解不是很透彻,看了一下 <Explore Python>一书, ...
- SAP MM ME57 把PR转成PO
SAP MM ME57 把PR转成PO 加入P3项目里,笔者负责采购和IM库存管理domain.采购环节里的PR转PO,客户global template里建议使用ME57或者在MD04里做.笔者在项 ...
- 系统调用syscall---用户态切换到内核态的唯一途径
1.应用程序有时需要内核协助完成一些处理,但是应用程序不可能执行内核代码(主要是安全性考虑), 那么,应用程序需要有一种机制告诉内核,它现在需要内核的帮助,这个机制就是系统调用. 2.系统调用的本质是 ...
- Python运维开发:运算符与数据类型(二)
python对象的相关术语: python程序中保存的所有数据都是围绕对象这个概念展开的: 程序中存储的所有数据都是对象 每个对象都有一个身份.一个类型和一个值 例如,school='MaGe Lin ...
- Android项目实战(五十):微信支付 坑总结
大部分APP必备需求,使用总结 Android接入文章在此:官方文档 文档很简单,Android分为四步: 1.后台配置 2.Android 内 注册appId 3.Android 内 调起支付 4. ...
- Screen Space Depth Varying Glow based on Heat Diffusion
Inspired by "Interactive Depth of Field Using Simulated Diffusion on a GPU" with heat diff ...
- git 入门教程之里程碑式标签
"春风得意马蹄疾,一日看尽长安花",对于项目也是如此,最值得期待的恐怕就要数新版本发布的时刻了吧?每当发布新版本时要么是版本号命名(比如v0.0.1)或者代号命名(比如Chelse ...