hdu 3183 A Magic Lamp(RMQ)
A Magic Lamp
Time Limit: 2000/1000 MS (Java/Others) Memory Limit:
32768/32768 K (Java/Others)
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.
178543 4
1000001 1
100001 2
12345 2
54321 2
13
1
0
123
321
(1)由于有n-m个数字。那么在1到m+1位置中最小的那个数字必是结果中的第一个数字,记录其位置为pos
(2)然后从这个位置的下个位置pos+1開始到m+2位置的数字中最小的那个数字必然是结果中第二个数字,以此类推下去向后找。
(3)为了保证数字最小,所以要保证高位最小,还要保证数字长度满足条件
#include<cstdio>
#include<cstring>
#include<cmath> char s[1010];
char ans[1020];
int st[1010][20]; int Min(int x,int y)
{
return s[x] <= s[y] ? x : y;
} void RMQ_Init(int len)
{
for(int i = 0; i < len; i++)
st[i][0] = i;
for(int j = 1; (1<<j) < len; j++)
for(int i = 0; i+(1<<j)-1 < len;i++)
st[i][j] = Min(st[i][j-1],st[i+(1<<(j-1))][j-1]);
} int Query(int l,int r)
{
int k = (int)(log((double)(r-l+1))/log(2.0));
return Min(st[l][k],st[r-(1<<k)+1][k]);
} int main()
{
int len, m, i;
while(scanf("%s%d",s, &m)!=EOF)
{
len = strlen(s);
RMQ_Init(len);
m = len - m;
int pos = 0, num = 0;
while(m--)
{
pos = Query(pos, len - m - 1);
ans[num++] = s[pos++];
}
for(i = 0; i < num; i++)
if(ans[i]!='0')
break;
if(i == num)
printf("0");
else
{
while(i < num)
printf("%c",ans[i++]);
}
puts("");
}
return 0;
}
二、这个题还能够直接使用贪心来求解,思路和RMQ基本同样。
#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
string s;
int m;
while(cin >> s >> m) {
int len = s.length(), i, j;
int p = 0, tmp_pos = m, flag = 0;
for(i = 0; i < len - m; i++) {
char mmin = s[p];
for(j = p + 1; j <= tmp_pos; j++)
if(s[j] < mmin) {
mmin = s[j];
p = j;
}
tmp_pos++;
p++;
if(!flag) {
if(mmin == '0') continue;
else {
cout << mmin;
flag = 1;
}
}
else {
cout << mmin;
}
}
if(!flag)
cout << "0";
cout << endl;
}
return 0;
}
hdu 3183 A Magic Lamp(RMQ)的更多相关文章
- HDU 3182 ——A Magic Lamp(思维)
Description Kiki likes traveling. One day she finds a magic lamp, unfortunately the genie in the lam ...
- hdu 3183 A Magic Lamp 【RMQ】
<题目链接> <转载于 >>> > 题目大意: 给出一个长度不超过1000位的数,求删去m位数字以后形成的最小的数字是多少. 解题分析: 分析:我们可以把题 ...
- HDU 3183:A Magic Lamp(RMQ)
http://acm.hdu.edu.cn/showproblem.php?pid=3183 题意:给出一个数,可以删除掉其中m个字符,要使得最后的数字最小,输出最后的数字(忽略前导零). 思路:设数 ...
- HDU 3183 A Magic Lamp(二维RMQ)
第一种做法是贪心做法,只要前面的数比后面的大就把他删掉,这种做法是正确的,也比较好理解,这里就不说了,我比较想说一下ST算法,RMQ的应用 主要是返回数组的下标,RMQ要改成<=(这里是个坑点, ...
- hdu 3183 A Magic Lamp(给一个n位的数,从中删去m个数字,使得剩下的数字组成的数最小(顺序不能变),然后输出)
1.题目大意是,给你一个1000位的数,要你删掉m个为,求结果最小数. 思路:在n个位里面删除m个位.也就是找出n-m个位组成最小数 所以在区间 [0, m]里面找最小的数.相应的下标标号i 接着找区 ...
- hdu 3183 A Magic Lamp(RMQ)
题目链接:hdu 3183 A Magic Lamp 题目大意:给定一个字符串,然后最多删除K个.使得剩下的组成的数值最小. 解题思路:问题等价与取N-M个数.每次取的时候保证后面能取的个数足够,而且 ...
- 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或者暴力
A Magic Lamp Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Pro ...
- HDU 3183 - A Magic Lamp - [RMQ][ST算法]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3183 Problem DescriptionKiki likes traveling. One day ...
随机推荐
- Mac WIn7 QQ聊天记录互导 聊天记录合并
也许等哪天老了回过头来看看.说不定还有一丝欢乐. 有几个方法可以实现 一.dropbox数据同步 二.QQ会员 三.下面方法 1.因为现在的Mac QQ还不支持聊天记录的导入导出.所以只能手动了 如果 ...
- advertisingIdentifier
iOS 7后Mac 地址就不能用了. 不过可以用advertisingIdentifier来取,再多个project 里测试是唯一的,但如果遇到系统升级或是重刷这个就不一定能唯一了.. 这里还要加一个 ...
- WinForm 之 程序启动不显示主窗体
在 WinForm 程序启动时,不显示主窗体的实现方法主要有以下5种,第五种最简单,而且效果也不错,第四种方法也值得推荐. 实例代码如下: //隐藏窗体的方法1/5:不指定任何窗体为主窗体 //注意: ...
- 使用Newtonsoft进行JSON序列化时将枚举序列化为字符串的方法
一.实体书写 将枚举类型的属性前面加上[JsonConverter(typeof(StringEnumConverter))]即可. 二.举例 [JsonConverter(typeof(String ...
- Centos7中安装Python3
环境:CentOS 7 1. 安装依赖环境 # yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-dev ...
- Struts2学习笔记三:深入Action
一:Action中获取参数值 在Servlet中,我们通过doGet()/doPost()方法中的参数,获取request/response对象,然后提取参数值: 处理请求结束后,有数据需要携带返回客 ...
- cocos2d-js 3.0 rc2 自定义UI控件组件 例子:能播放动画的MenuItem。MenuItemSprite的bug
其实总体自定义UI组件都比较简单,尤其是cocos2d-js是开源的,如果有什么不明白的直接看js代码或者C++代码即可.当然js代码基本就够了. 另外,js的ctor函数虽然说是构造函数,但毕竟不是 ...
- java base64编码、解码的三种方式
java base64编码.解码 CreationTime--2018年7月24日10点38分 Author:Marydon 1.方式一:DatatypeConverter 说明:使用jdk自带的 ...
- 〖Linux〗Linux高级编程 - 进程间通信(Interprocess Communication)
[转自: http://blog.csdn.net/Paradise_for_why/article/details/5550619] 这一章就是著名的IPC,这个东西实际的作用和它的名字一样普及.例 ...
- Aspect实现对方法日志的拦截记录
在实际的业务系统中,我们通常都希望程序自动的打印方法的入参和返回值,某些特定的方法可能不想打印返回值(返回数据过大,打印日志影响效率),特有了下面的实现. 1.忽略返回值的java注解类 import ...