原题目

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

Problem Description
Kiki likes traveling. One day she finds a magic lamp, unfortunately the genie in the lamp is not so kind. Kiki must answer a question, and then the genie will realize one of her dreams. 
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?
 
Input
There are several test cases.
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.
 
Output
For each case, output the minimum result you can get in one line.
If the result contains leading zero, ignore it. 
 
Sample Input
178543 4
1000001 1
100001 2
12345 2
54321 2
 
Sample Output
13
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算法)的更多相关文章

  1. hdu 3183 A Magic Lamp RMQ ST 坐标最小值

    hdu 3183 A Magic Lamp RMQ ST 坐标最小值 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3183 题目大意: 从给定的串中挑 ...

  2. hdu 3183 A Magic Lamp(RMQ)

    题目链接:hdu 3183 A Magic Lamp 题目大意:给定一个字符串,然后最多删除K个.使得剩下的组成的数值最小. 解题思路:问题等价与取N-M个数.每次取的时候保证后面能取的个数足够,而且 ...

  3. HDU 3183 - A Magic Lamp - [RMQ][ST算法]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3183 Problem DescriptionKiki likes traveling. One day ...

  4. hdu 3183 A Magic Lamp rmq或者暴力

    A Magic Lamp Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Pro ...

  5. hdu 3183 A Magic Lamp(RMQ)

    A Magic Lamp                                                                               Time Limi ...

  6. HDU 3183 A Magic Lamp(二维RMQ)

    第一种做法是贪心做法,只要前面的数比后面的大就把他删掉,这种做法是正确的,也比较好理解,这里就不说了,我比较想说一下ST算法,RMQ的应用 主要是返回数组的下标,RMQ要改成<=(这里是个坑点, ...

  7. hdu 3183 A Magic Lamp 【RMQ】

    <题目链接> <转载于 >>>  > 题目大意: 给出一个长度不超过1000位的数,求删去m位数字以后形成的最小的数字是多少. 解题分析: 分析:我们可以把题 ...

  8. hdu 3183 A Magic Lamp(给一个n位的数,从中删去m个数字,使得剩下的数字组成的数最小(顺序不能变),然后输出)

    1.题目大意是,给你一个1000位的数,要你删掉m个为,求结果最小数. 思路:在n个位里面删除m个位.也就是找出n-m个位组成最小数 所以在区间 [0, m]里面找最小的数.相应的下标标号i 接着找区 ...

  9. hdu 3183 A Magic Lamp 贪心

    #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm& ...

随机推荐

  1. Vue自定义事件:触发自定义事件

    一 项目结构 二 子组件(Mongo.vue) <template> <button @click="eat">按钮</button> < ...

  2. 爬虫(十一)—— XPath总结

    目录 XPath总结 一.何为XPath 二.XPath语法 1.语法 2.实例 三.XPath轴 1.XPath轴语法 2.XPath轴实例 四.XPath运算符 XPath总结 一.何为XPath ...

  3. [Codeforces 865C]Gotta Go Fast(期望dp+二分答案)

    [Codeforces 865C]Gotta Go Fast(期望dp+二分答案) 题面 一个游戏一共有n个关卡,对于第i关,用a[i]时间通过的概率为p[i],用b[i]通过的时间为1-p[i],每 ...

  4. mongodb的有关操作

    mongodb的几种启动方法 https://www.cnblogs.com/LLBFWH/articles/11013791.html MongoDB 之 你得知道MongoDB是个什么鬼 Mong ...

  5. 使用再生龙对ubuntu16.04系统完全备份与还原

    1.制作再生龙U盘启动 1.所需要的软件 1.clonezilla-live-2.5.5-38-amd64.iso(再生龙系统镜像) 2.UltraISO(镜像刻录软件) 3.两个空白U盘(U盘A-用 ...

  6. CSS-01 CSS代码标准和规范

    一:代码规范 1.所有的书写都是在英文半角下进行 2.统一用table键进行缩进 3.属性值必须带引号(单引和双引都可以) 4.p,dt,h标签里不能嵌套块属性标签 5.a标签不能嵌套a 二:文件命名 ...

  7. navigate连接不上Centos7+mariadb的问题

    链接数据库时忽然遇到一个问题.Mac Navicat链接时报错Can’t connect to MySQL server on ‘xx.xx.xx.xx’ (61). PS. win版Navicat ...

  8. 关于手机端在同一个Grid中使用不同的布局展现即Layout的使用

    标题可能说的不是很清楚,我举个栗子好了,现在你正在写手机端的一个审批模块,这个模块要求能够展示所有待审批的信息 比如出差申请,请假申请,加班申请,以及报销申请 那么我的思路有两个 1:建立一个Tab页 ...

  9. Linux就该这么学08学习笔记

    参考链接:https://www.linuxprobe.com/chapter-08.html 防火墙管理工具 众所周知,相较于企业内网,外部的公网环境更加恶劣,罪恶丛生.在公网与企业内网之间充当保护 ...

  10. Fabric管理组件的使用

    Fabric的官方网站: http://www.fabfile.org 帮助文档: https://fabric-chs.readthedocs.io/zh_CN/chs/tutorial.html ...