【思路、优化】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次...)由于规定了整数的位数,那么我们要尽量让高位的数字大一些,也就是要尽量删去前面小的数字. ...
随机推荐
- 通过一次实验来了解HTML5的 Web Worker
web worker 是运行在后台的 JavaScript,不会影响页面的性能. 当在 HTML 页面中执行脚本时,页面的状态是不可响应的,直到脚本已完成. web worker 是运行在后台的 Ja ...
- UVALive 7278 Game of Cards (sg函数)
Game of Cards 题目链接: http://acm.hust.edu.cn/vjudge/contest/127406#problem/G Description Alice and Bob ...
- STC89C52RC片内资源介绍
STC89C52RC片内有:用户应用程序区(AP)8K,地址0000h-1FFFh. 数据flash区(EEPROM)4K,2000h-2FFFh ISP引导区空间1K/2k/4k. RAM 512B ...
- MySQL安装配置最后时未响应解决方法
安装MySQL出示未响应,一般显示在安装MySQL程序最后一步的2,3项就不动了. 这种情况一般是你以前安装过MySQL数据库服务项被占用了.解决方法:一种方法:你可以安装MySQL的时候在这一步时它 ...
- BestCoder Round #65 hdu5591(尼姆博弈)
ZYB's Game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- Android流量监控 思路,想法
1,开启一个服务,每5分钟跑动一次更新流量,用于能够准确记录流量 每一个小时,更新一次流量,用于清除非本月的流量 2,保存流量的时候,进行判断 a,若是数据库中保存的 ...
- Light oj 1214-Large Division (同余定理)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1214 题意很好懂,同余定理的运用,要是A数被B数整除,那么A%B等于0.而A很大,那我 ...
- 浅析网站开发中的 meta 标签的作用
*:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...
- IOS开发--数据持久化篇之文件存储(一)
前言:个人觉得开发人员最大的悲哀莫过于懂得使用却不明白其中的原理.在代码之前我觉得还是有必要简单阐述下相关的一些知识点. 因为文章或深或浅总有适合的人群.若有朋友发现了其中不正确的观点还望多多指出,不 ...
- Android 多点触控错误处理(java.lang.IllegalArgumentException: pointerIndex out of range)
最近做View的多点触控时,每次第一次触控事件完美运行,第二次就直接崩了,错误信息如下: 01-03 00:05:44.220 4377-4410/system_process E/AndroidRu ...