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个数字,剩下的数字要尽量大. 题解:因为最后数字位数是确定的,而且低位数字对答案的贡献是一定不及高位数字的,所以优先选择选最大且最靠左边的数字,但是有一个限制,选完这 ...
随机推荐
- Netty实战一之异步和事件驱动
Netty是一款异步的事件驱动的网络应用程序框架,支持快速地开发可维护的高性能的面向协议的服务器和客户端. 使用Netty你可以并不是很需要网络编程.多线程处理.并发等专业Java知识的积蓄. Net ...
- hive SQL查询结果添加行号
用窗口函数可以解决这个问题: 例:select row_number() over(order by user_id desc) ,tab.* from dws_user_visit_month1 a ...
- 并发之AQS
一.概述 谈到并发,不得不谈ReentrantLock:而谈到ReentrantLock,不得不谈AbstractQueuedSynchronizer(AQS)! 类如其名,抽象的队列式的同步器,AQ ...
- MySQL添加新用户、为用户创建数据库、为新用户分配权限
登录MySQL [root@VM_0_2_33_centos /]#mysql -u root -p 添加新用户 允许本地 IP 访问 localhost, 127.0.0.1 mysql>'; ...
- canvas-8searchLight2.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 苹果8plus怎么录屏视频
现在越来越多的手机控,不管在什么地方,什么时候,都是低头看手机的居多,因为手机信息量太大了,一部手机就可以了解最新咨询,但是作为苹果8plus怎么录制手机屏幕,你们知道吗?今天就和大家一起分享苹果8p ...
- Win7录制电脑屏幕视频
在日常生活中,有时候我们需要在电脑上录制视频,那就需要找到一款合适的录像工具,选择迅捷屏幕录像工具就是一个不错的选择,操作简单轻松易上手,美轮美奂的无损画质,教学视频.电影.游戏等都可以进行录制哦! ...
- 原型链、闭包四种作用、继承、命名空间、枚举类型(day13)
原型链 JavaScript 对象是动态的属性“包”(指其自己的属性).JavaScript 对象有一个指向一个原型对象的链.当试图访问一个对象的属性时,它不仅仅在该对象上搜寻,还会搜寻该对象的原型, ...
- ORA-01440: column to be modified must be empty to decrease precision or scale
在修改表字段的NUMBER类型的精度或刻度时,你可能会遇到ORA-01440: column to be modified must be empty to decrease precision or ...
- weblogic的web.xml报错----Malformed UTF-8 char -- is an XML encoding declaration missing
weblogic报错: Malformed UTF-8 char -- is an XML encoding declaration missing 把编码修改成utf8,上传到weblogic就报这 ...