HDU 3183 A Magic Lamp(RMQ问题, ST算法)
A Magic Lamp
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3964 Accepted Submission(s): 1605
The question is: give you an integer, you are allowed to delete exactly m digits. The left digits will form a new integer. You should make it minimum.
You are not allowed to change the order of the digits. Now can you help Kiki to realize her dream?
Each test case will contain an integer you are given (which may at most contains 1000 digits.) and the integer m (if the integer contains n digits, m will not bigger then n). The given integer will not contain leading zero.
If the result contains leading zero, ignore it.
1000001 1
100001 2
12345 2
54321 2
1
0
123
321
********************************************************************************************************************************************************************************************
题意:给你一个数字串 (每一个串有n个数字), 和一个m (要你删除m个数字 ), 使得剩下的数字组成的整数最小。
一开始想了一下删除m个,等价与在数字串中选择 n-m 个数字组成最小整数。
一开始一直做不出,贪心有点问题。后来被dalao教一下就懂了;
贪心的方法:
我们先假设 n 是串的个数, m 是要删除的个数, need 是需要选的个数;
1) 因为是按照顺序组合数字的, 所以第一个删除的数字一定在 [0, n-need] 之内;
证明: 假设数字有 a0, a1, a2, a3, a4, a5 个,要删除2个,n=6, m=2, need = 4;
因为是连续选择的,我们假设在 [0, 3]之内选择第一个,那么我们只剩下a4, a5了,无法构成4个;
所以假设最后的need-1个我们都要, 那么 从 0 ~ (n - 1) - ( need - 1 )==[0, n-need]之内第一个一定在里面(数组从0开始,所以 n-1);
2) 当在[0, n-need]之内选好了第一个,找到第一个的位置pre,那么下一个 数字一定在 [ pre + 1, n-need + 1 ], 就可以一直找下去了,记录你找到的数;
3) 最后在你找到的数里面讨论前导0的情况。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <deque>
using namespace std;
const int maxn = +;
int d[maxn][maxn];
int a[maxn];
void RMQ_init(string s)
{
memset(d, , sizeof(d));
memset(a, , sizeof(a));
int n = s.size();
for(int i=;i< n ;i++) a[i]=d[i][]=s[i]-'';
for(int j = ; (<<j) <= n ;j++)
for(int i = ; i+(<<j ) - < n ;i++)
d[i][j] = min(d[i][j-] , d[i+(<<(j-))][j-] );
}
int RMQ(int L, int R)
{
int k=;
while((<<(k+)) <= R-L+) k++;
return min(d[L][k], d[R-(<<k) + ][k]);
}
int findmin(int L, int R, int Min)
{
for(int i=L; i <= R ;i++){
if(a[i] == Min) return i;
}
}
int main()
{
string s;
int m, n, need;
while(cin >> s >> m){
RMQ_init(s);
n = s.size() ;
need = n - m;
int Left=, Right = n - need;
// cout << Left << " " << Right << endl;
// cout << RMQ(Left, Right) << endl;
deque <int > q;
while(){
if(need == ) break;
int Min = RMQ(Left, Right);
// cout << Min << endl;
q.push_back(Min);
int pre = findmin(Left, Right, Min);
Left = pre+;
Right ++ ;
need --;
}
while(!q.empty()){
if(q.front() == ) q.pop_front();
else break;
}
if(q.empty()){
cout << "";
}
else{
while(!q.empty()){
cout << q.front();
q.pop_front();
}
}
cout << endl;
}
return ;
}
HDU 3183 A Magic Lamp(RMQ问题, ST算法)的更多相关文章
- hdu 3183 A Magic Lamp RMQ ST 坐标最小值
hdu 3183 A Magic Lamp RMQ ST 坐标最小值 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3183 题目大意: 从给定的串中挑 ...
- hdu 3183 A Magic Lamp(RMQ)
题目链接:hdu 3183 A Magic Lamp 题目大意:给定一个字符串,然后最多删除K个.使得剩下的组成的数值最小. 解题思路:问题等价与取N-M个数.每次取的时候保证后面能取的个数足够,而且 ...
- HDU 3183 - A Magic Lamp - [RMQ][ST算法]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3183 Problem DescriptionKiki likes traveling. One day ...
- hdu 3183 A Magic Lamp rmq或者暴力
A Magic Lamp Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Pro ...
- hdu 3183 A Magic Lamp(RMQ)
A Magic Lamp Time Limi ...
- HDU 3183 A Magic Lamp(二维RMQ)
第一种做法是贪心做法,只要前面的数比后面的大就把他删掉,这种做法是正确的,也比较好理解,这里就不说了,我比较想说一下ST算法,RMQ的应用 主要是返回数组的下标,RMQ要改成<=(这里是个坑点, ...
- hdu 3183 A Magic Lamp 【RMQ】
<题目链接> <转载于 >>> > 题目大意: 给出一个长度不超过1000位的数,求删去m位数字以后形成的最小的数字是多少. 解题分析: 分析:我们可以把题 ...
- hdu 3183 A Magic Lamp(给一个n位的数,从中删去m个数字,使得剩下的数字组成的数最小(顺序不能变),然后输出)
1.题目大意是,给你一个1000位的数,要你删掉m个为,求结果最小数. 思路:在n个位里面删除m个位.也就是找出n-m个位组成最小数 所以在区间 [0, m]里面找最小的数.相应的下标标号i 接着找区 ...
- hdu 3183 A Magic Lamp 贪心
#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm& ...
随机推荐
- Git013--多人协作
Git--多人协作 本文来自于:https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/ ...
- Kubernetes V1.16.2部署Dashboard V2.0(beta5)
Kubernetes V1.16.2部署Dashboard V2.0(beta5) 在Master上部署Dashboard 集群安装部署请看安装Kubernetes V1.16.2 kubectl g ...
- python------模块和包及异常处理
一.模块 所有的模块导入都应该尽量往上写,且顺序为: a:内置模块 b:扩展模块 c:自定义模块 #my_module.py print('from the my_module.py') money= ...
- P5468 [NOI2019]回家路线
传送门 看题目一眼斜率优化,然后写半天调不出来 结果错误的 $dfs$ 有 $95$ 分?暴力 $SPFA$ 就 $AC$ 了? 讲讲正解: 显然是斜率优化的式子: 先不考虑 $q_{s_k}$ 的贡 ...
- springboot的jar包部署
由于springboot常用war包部署,改为cloud开发模式多端口情况下,部署反而不习惯 毕竟,war包要不要项目名访问都必须放在tomcat的root目录下 而此目录限制只能放置一个项目,并且登 ...
- AJAX —— JSON 字符串 与 JSON 对象
一.JSON 字符串转 JSON 对象 ----> JSON.parse(JString); 1 // JSON 字符串转 JSON 对象 ----> JSON.parse(JString ...
- 二、jquery Try{}catch(e){}
一.Try{}catch(e){} try{ $.each($("div"),function(i,item){ if(...){ throw("异常信息"); ...
- Linux之scp命令的使用
Linux之scp命令的使用 1. scp简介 1.1 命令功能: scp是 secure copy的缩写, scp是linux系统下基于ssh登陆进行安全的远程文件拷贝命令.linux的scp命令可 ...
- ansible笔记(三)--模块讲解
ansible 常用命令 ansible-doc ansible-playbook ansible-vault ansible-console ansible-galaxy ansible-pull ...
- MySQL系列之二四种隔离级别及加锁
事务 1.定义:所有操作必须成功完成,否则在每个操作中所作的所有更改都会备撤销. 2.事务的ACID 原子性atomicity 一致性consistency 隔离性isolation 持续 ...