【思路、优化】UVa 11491 - Erasing and Winning
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个数,使剩下的数最大。
分析:TLE了很长时间一直优化不成功。后来才知道思路就很暴力。下面给两个思路,可以都尝试一下;
①首先记录每个数字0~9出现的位置,然后从9~0开始循环,从后往前依次将数字放到相应位置,期间注意一旦放置(N-D)个数字后立即退出循环。最后扫一遍字符串,没有数字的位置不输出。这样复杂度为0(n*10);
②第二个思路是网上的思路,先从头扫描,一旦发现digit[i]<digit[j]&&(i<j),就从i处向前将小于digit[j]的数删掉;
首先看了思路之后自己尝试写了一下,可惜优化的还是不好(读者也可以先根据思路尝试写下,独立AC更有成就感吖~)。
后来看了题解,发现可以用list实现,list的erase与iter的灵活运用很有用处。
下附代码
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<list>
using namespace std;
int d, n, alnum[];
char num[]; void solve()
{
list<int> ans;
ans.push_back();
for(int i = ; i < n; i++)
{
ans.push_back(num[i]-'');
}
ans.push_back(); list<int>::iterator head, tail, iter;
head = ans.begin(); head++;
tail = ans.begin();
int cnt = ;
while(cnt < d)
{
if(*head > *tail)
{
ans.erase(tail);
head--;
tail = head;
head++; cnt++;
}
else
{
head++;
tail++;
}
}
ans.pop_back();
for(iter = ans.begin(); iter != ans.end(); iter++)
{
if(*iter == ) continue;
printf("%d", *iter);
}
printf("\n");
} int main()
{
//freopen("in.txt", "r", stdin);
while(scanf("%d%d", &n, &d))
{
if(!n && !d) break;
scanf("%s", num);
solve();
}
return ;
}
【思路、优化】UVa 11491 - Erasing and Winning的更多相关文章
- 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
		题意: 给你一个n位整数,让你删掉d个数字,剩下的数字要尽量大. 分析: 用了vector数组模拟.如果当前要插入的数>vector数组里的最后一位数,就替换且d-- 代码: #include ... 
- UVa 11491 Erasing and Winning (贪心,单调队列或暴力)
		题意:给一个数字(开头非0),拿掉其中的d个数字,使剩下的数字最大(前后顺序不能变). 析:拿掉d个数字,还剩下n-d个数字.相当于从n个数字中按先后顺序选出n-d个数字使组成的数字最大,当然采用窗口 ... 
- UVA 11491 Erasing and Winning 奖品的价值 (贪心)
		题意:给你一个n位整数,让你删掉d个数字,剩下的数字要尽量大. 题解:因为最后数字位数是确定的,而且低位数字对答案的贡献是一定不及高位数字的,所以优先选择选最大且最靠左边的数字,但是有一个限制,选完这 ... 
- UVA - 11491 Erasing and Winning(奖品的价值)(贪心)
		题意:有一个n位整数(不以0开头),要求删除其中的d个数字,使结果尽量大.(1<=d<n<=10^5) 分析: 1.从头扫一遍,如果当前填的数字小于n-d,则将当前数字填上. 2.如 ... 
- Erasing and Winning UVA - 11491 贪心
		题目:题目链接 思路:不难发现,要使整体尽量大,应先满足高位尽量大,按这个思路优先满足高位即可 AC代码: #include <iostream> #include <cstdio& ... 
- 【习题 8-4 UVA - 11491】Erasing and Winning
		[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 考虑删掉第i位. 则第i+1位就会取代第i位. 则肯定第i+1位比第i位大的话,才比较好. 则从小到大贪心删,找到第一个a[i+1] ... 
- 从一个n位数中选出m位按顺序组成新数并使其最大 || Erasing and Winning UVA - 11491
		就是从n位数中取出n-d个数字按顺序排成一排组成一个新数使得其最大 算法: 从前往后确定每一位.找第i位时,要求后面留下d-i位的空间, 因此第i位应该从第i-1位原来位置+1到第d+i位寻找 用线段 ... 
- 【uva 11491】Erasing and Winning(算法效率--贪心+单调队列)
		题意:有一个N位整数,要求输出删除其中D个数字之后的最大整数. 解法:贪心.(P.S.要小心,我WA了2次...)由于规定了整数的位数,那么我们要尽量让高位的数字大一些,也就是要尽量删去前面小的数字. ... 
随机推荐
- [转]eclipse中使用maven插件的时候,运行run as maven build的时候报错
			转至:http://fxb4632242.iteye.com/blog/2193945 -Dmaven.multiModuleProjectDirectory system propery is no ... 
- 从最简单的HelloWorld理解MVP模式
			版权声明:本文为博主原创文章,转载请注明出处:http://www.cnblogs.com/joy99/p/6116855.html 大多数编程语言相关的学习书籍,都会以hello,world这个典型 ... 
- MVC神韵---你想在哪解脱!(十二)
			追加一条电影信息 运行应用程序,在浏览器中输入“http://localhost:xx/Movies/Create”,在表单中输入一条电影信息,然后点击追加按钮,如图所示. 点击追加按钮进行提交,表单 ... 
- 解决mysql导入导出数据乱码问题
			最近在linux上面用mysqldump导出数据,放在windows系统中导入就会出现中文乱码,然后就会导致出现: Unknown MySQL server host和Can't connect to ... 
- 怎么修改电脑MAC地址 电脑MAC地址修改图文教程
			本文转载:http://www.45fan.com/a/Router/2677.html MAC地址是指电脑网卡的硬件地址,此地址一般烧录在网卡上.MAC地址工作在OSI七层模型的第二层,即数据链接层 ... 
- JQuery Plugin 2 - Passing Options into Your Plugin
			overriding the default options with user-supplied options and the jQuery extend() method eg: $.fn.pu ... 
- 401 Palindromes(回文词)
			Palindromes A regular palindrome is a string of numbers or letters that is the same forward as ba ... 
- thinkphp中的HTTP类实现下载
			public function test(){ import('ORG.Net.Http'); $filename="Uploads/v1.2.doc"; //exit($file ... 
- Codeblocks支持C++11
			Setting->Compiler 直接在“Have g++ follow the C++11 ISO C++ language standard [-std=c++11]” 选项上打勾 保存就 ... 
- Maven 插件开发(一)
			项目在重构之后,想由ant切换到maven,在转换的过程中遇到一个问题.因为项目是基于OSGi的架构,而OSGi在运行时是依赖于插件环境的,bundle之间存在package依赖.而maven是基于d ... 
