UVA 11491 Erasing and Winning 奖品的价值 (贪心)
题意:给你一个n位整数,让你删掉d个数字,剩下的数字要尽量大。
题解:因为最后数字位数是确定的,而且低位数字对答案的贡献是一定不及高位数字的,所以优先选择选最大且最靠左边的数字,但是有一个限制,选完这个数字以后右边剩下的数字要保证足够接下来的选择,所以想到了优先队列,记录一个信息,选的数字所在的位置,以及上一个数字所在的位置,如果当前出队的数字在上一个选的位置前面就直接丢掉,每次选完一个以后剩下要选的数字就减少了,满足限制的条件的数字会增加,再把新的待选数字加入队列。
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+;
struct dig
{
int val,pos;
bool operator < (const dig & rhs) const {
return val < rhs.val || (val == rhs.val && pos > rhs.pos);
}
}D[maxn]; priority_queue<dig> q; int n,d;
void sovle()
{
while(q.size()) q.pop();
int i;
for(i = ; i <= d; i++) {
q.push(D[i]);
}
int Need = n-d,pre = -;
while(Need){
while(q.top().pos<pre) q.pop();
const dig &u = q.top();
if(u.pos == n-Need) {
for(i = u.pos; i < n; i++)
printf("%d",D[i].val);
break;
}
pre = u.pos;
printf("%d",u.val);
Need--; q.pop();
q.push(D[i++]);
}
putchar('\n');
} int main()
{
// freopen("in.txt","r",stdin);
while(scanf("%d%d",&n,&d),n){
for(int i = ; i < n; i++){
scanf("%1d",&D[i].val);
D[i].pos = i;
}
sovle();
} return ;
}
UVA 11491 Erasing and Winning 奖品的价值 (贪心)的更多相关文章
- UVA - 11491 Erasing and Winning(奖品的价值)(贪心)
题意:有一个n位整数(不以0开头),要求删除其中的d个数字,使结果尽量大.(1<=d<n<=10^5) 分析: 1.从头扫一遍,如果当前填的数字小于n-d,则将当前数字填上. 2.如 ...
- 【思路、优化】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
题意: 给你一个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(n<10^5)位的数,删除d位,求删除后最大的数.(原数无前导0) 思路:从前往后扫,如果a[i] > a[i-1],则删除a[i-1].我暴力的用链表实现了…… #in ...
- 【uva 11491】Erasing and Winning(算法效率--贪心+单调队列)
题意:有一个N位整数,要求输出删除其中D个数字之后的最大整数. 解法:贪心.(P.S.要小心,我WA了2次...)由于规定了整数的位数,那么我们要尽量让高位的数字大一些,也就是要尽量删去前面小的数字. ...
- UVA11491 奖品的价值
奖品的价值C804 运行时间限制:1000ms: 运行空间限制:51200KB 试题描述 你是一个电视节目的获奖嘉宾.主持人在黑板上写出一个 n 位非负整数(不以 0 开头),邀请你删除其中的 d 个 ...
- uva11491 奖品的价值(贪心)
uva11491 奖品的价值(贪心) 给你一个n位的整数,请你删除其中的d个数字,使得整数尽可能大.1<=d<n<=1e5. 首先因为前面的数位更重要,所以从左往右将每一位数字加入栈 ...
- UVA LA 7146 2014上海亚洲赛(贪心)
option=com_onlinejudge&Itemid=8&page=show_problem&category=648&problem=5158&mosm ...
随机推荐
- Python 在windows上安装BeautifulSoup和request以及小案例
Python以及PyCharm安装成功后,操作如下: 此时,代码import requests不报错了. 那么,Python 在windows上安装BeautifulSoup,怎么操作呢? 1. 打开 ...
- layer常用方法
弹出层layer的使用 弹出层layer的使用 Intro layer是一款web弹层组件,致力于服务各个水平段的开发人员.layer官网:http://layer.layui.com/ layer侧 ...
- Web.config文件中关于Cookie安全性的考量和设置
cookie的内容,如图所示: HTTP response header: Set-Cookie: <name>=<value>[; <Max-Age>=<a ...
- Thinkphp3.2 备份数据库和还原数据的方法
其实Thinkphp框架并没有自带备份数据库的功能,但是细心的朋友可能会发现Thinkphp的一套内容管理系统oneThink是有备份数据库和还原数据的功能的. 所以今天我就来聊一聊,oneThink ...
- Java集合Set、Map、HashSet、HashMap、TreeSet、TreeMap等
1.Set和Map的关系: Set代表一种集合元素无序.不可重复的集合,Map代表一种由多个key-value对组成的集合. Set的集合继承体系: Map关系集合 Map集合的key特征:所有key ...
- jquery获取元素对应高度
https://www.cnblogs.com/sntetwt/p/3823592.html 获取点击元素距离页面的高度 $(select).offset().top - $(document).sc ...
- Insert into a Binary Search Tree
Given the root node of a binary search tree (BST) and a value to be inserted into the tree, insert t ...
- AES 加密 PHP 和 JAVA 互通
PHP代码: <?php class Security { public static function encrypt($input, $key) { $size = mcrypt_get_b ...
- 调试接口,返回的json数据,我定义了一个类,用来序列化,其中有一个字段定义为string 然后序列化的时候报错
调试接口,返回的json数据,我定义了一个类,用来序列化,其中有一个字段定义为string 然后序列化的时候报错 在需要解析的类型类上加上声明 eg:
- 微服务架构 Steeltoe
.NET Core 微服务架构 Steeltoe 使用(基于 Spring Cloud) https://www.cnblogs.com/xishuai/p/steeltoe-and-spring-c ...