A Magic Lamp

                                                                              Time Limit: 2000/1000 MS (Java/Others)    Memory Limit:
32768/32768 K (Java/Others)

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
 

题意:给出一个不超过1000位的数,求删去m个数字以后形成的最小的数是多少。

分析:我们能够把题目转化为这样一个模型:从A[1]、A[2]、……、A[n] n个数中选出n-m个数,使得组成的数最小。
一、使用RMQ。设原数字长为n,那么除去m个数字后还剩n-m个数字。

(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)的更多相关文章

  1. HDU 3182 ——A Magic Lamp(思维)

    Description Kiki likes traveling. One day she finds a magic lamp, unfortunately the genie in the lam ...

  2. hdu 3183 A Magic Lamp 【RMQ】

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

  3. HDU 3183:A Magic Lamp(RMQ)

    http://acm.hdu.edu.cn/showproblem.php?pid=3183 题意:给出一个数,可以删除掉其中m个字符,要使得最后的数字最小,输出最后的数字(忽略前导零). 思路:设数 ...

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

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

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

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

  6. hdu 3183 A Magic Lamp(RMQ)

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

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

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

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

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

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

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

随机推荐

  1. Android 设计原则【转载+整理】

    原文地址 本文内容 吸引我的眼球 简化我的生活 让我眼前一亮 在使用过大量 Android APP 后,你会发现,遵循了下面这些原则的 APP 将会有更好的用户体验. 我们知道,往往国企的那些软件,都 ...

  2. Android Studio打开出现:Default activity not found

    昨天项目可以正常打开,没有问题,今天打开的时候就出现了这个问题.可以编译,但是无法生成APK调试.当然,如果选择 Do not launch Activity就可以成功编译.出现这个 Default ...

  3. Elasticsearch - 理解字段分析过程(_analyze与_explain)

    我们经常会遇到问题.为什么指定的文档没有被搜索到.许多情况下, 这都归因于映射的定义和分析例程配置存在问题. 针对分析过程的调试,ElasticSearch提供了专用的REST API. _analy ...

  4. Ado.Net,关于DataSet和DataTable

    DataSet和DataTable的 区别与联系 1.简要说明二者关系 在我们编写代码的时候从数据库里取出数据,填充到dataset里,再根据表的名字,实例化到 DataTable 中. ●注意如下  ...

  5. 我的webrequest经验

    1 webrequest 是什么:编程方式模拟web请求,利用webrequest可以实现 相当于一个浏览器请求一个网页的效果,但是它始终是模拟请求, 与浏览器输入框输入网址请求不一样. 2 程序设计 ...

  6. Apache Rewrite规则详解

    参考链接:http://slj.me/2009/04/apache-rewrite-regular/ 1.Rewrite规则简介 Rewirte主要的功能就是实现URL的跳转,它的正则表达式是基于Pe ...

  7. Linux下安装Supervisor的多种方法

    一.安装 1.方法一: pip install  supervisor #!/bin/bash wget http://pypi.python.org/packages/source/s/setupt ...

  8. Openwrt15.05网关后pptp外拨失败的解决办法

    路由器升级openwrt chaos_calmer 15.05版后发现NAT后面的客户端外拨pptp vpn服务器失败,经google后得知,在14.07版本中默认安装的又一个叫做 kmod-ipt- ...

  9. wordpress 开源博客系统部署

     1.开发工具 server apache    下载地址:http://www.apache.org   http://httpd.apache.org/download.cgi 数据库 mys ...

  10. Knockout学习之表单绑定器(下)

    “hasFocus”绑定 hasFocus绑定器会将DOM元素的焦点状态与视图模型中的属性相关联,当你设置视图模型中关联的属性为true或false后,将能够设置关键的DOM元素是否获得焦点. 比如下 ...