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 ...
随机推荐
- mysql function 中使用游标
create function p_t1(tid varchar(20))returns varchar(200)begin declare tmpName varchar(50) default ' ...
- xpress for node 路由route几种实现方式
路由实现方式和顺序第二种路由写法第三种路由写法最佳 http://blog.csdn.net/mociml/article/details/11137571# http://blog.fens.me/ ...
- C# 通过SendMessage获取浏览器地址栏的地址
1:通过SPY++获得地址栏的层次结构,然后一层一层获得 2:代码 using System; using System.Collections.Generic; using System.Linq; ...
- Qt,Qt/E,Qtopia Core, Qtopia之间的区别和联系
转自:http://www.qtcn.org/bbs/read.php?tid=10373 关于Qt,Qt/E,Qtopia Core, Qtopia这些版本之间的区别和联系: Qt泛指Qt的所有桌面 ...
- vue初始化数据加载
使用created钩子 import AppLayout from '@/components/app-layout' import axios from 'axios' export default ...
- 转:清理系统垃圾的BAT代码
@echo off title @echo off color 2 echo. echo. echo 请不要关闭此窗口! echo. echo 开始清理垃圾文件,请稍等...... echo. ech ...
- Python学习笔记六:数据库操作
一:Python操作数据库的流程 二:开发环境准备 1:开发工具PyCharm 2:Python操作mysql的工具:需要安装Python-Mysql Connector,网址:https://sou ...
- OpenERP 7中 openerp-server.conf 的解释
服务器启动配置 – 通用项 程序代码: [选择] # Admin password for creating, restoring and backing up databases admin_pas ...
- SQL Server 默认跟踪(Default Trace)获取某个Trace跟踪了哪些Event和column
检查Default Trace是否已经开启,如果返回Figure1中value为1,那就说明已经开启默认跟踪了:如果value为0表示关闭默认跟踪: --查询Default Trace是否开启 ; 如 ...
- java if语句
//if语句 //Test10.java import java.util.Scanner; public class Test16{ public static void main(String[] ...