A Magic Lamp

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 989    Accepted Submission(s): 371

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
 

Source
 

Recommend
lcy
 

贪心+RMQ

在其中取出len-m,使得取出的len-m位的数最小....这个问题是贪心问题: 设给出的数是X[1..len]..当前一共要取出N = len-m 个数字..

第一次取出的是[1..len-N+1]中取了一个最小的数字,下标为pre.然后N--..第二次就从[pre+1..len-N+1]中取出最小的那个数字..这样一直取出len-m

 

其中求区间最小值时,可用线段树或RMQ实现..

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int dp[1111][22];
char a[1111];
int n;
int out[1111];
void RMQ_init()
{
    for(int i=1;i<=n;i++)
        dp[0]=i-1;
    for(int j=1;(1<<j)<=n;j++)
        for(int i=1;i+(1<<j)-1<=n;i++)
    {
        int m=(1<<(j-1))+i;
        if(a[dp[j-1]]>a[dp[j-1]])
            dp[j]=dp[j-1];
        else if(a[dp[j-1]]==a[dp[j-1]])
            dp[j]=min(dp[j-1],dp[j-1]);
        else if(a[dp[j-1]]<a[dp[j-1]])
            dp[j]=dp[j-1];
    }
}
int RMQ(int l,int r)
{
    int k=0;
    while((1<<(k+1))<=r-l+1)   k++;
    if(a[dp[l][k]]>a[dp[r-(1<<k)+1][k]])
        return dp[r-(1<<k)+1][k];
    else if(a[dp[l][k]]==a[dp[r-(1<<k)+1][k]])
        return min(dp[l][k],dp[r-(1<<k)+1][k]);
    else //if(a[dp[l][k]]<a[dp[r-(1<<k)+1][k]])
        return dp[l][k];
}
int main()
{
    int t;
while(scanf("%s%d",a,&t)!=EOF)
{
    memset(dp,-1,sizeof(dp));
    memset(out,-1,sizeof(out));
    n=strlen(a);
    if(n==t)  {   printf("0\n"); continue;}
    RMQ_init();
    int m=n-t;
    int cur=0;
    int pre=1;
    while(m)
    {
        out[cur]=RMQ(pre,n-m+1);
        pre=out[cur]+2;
  //      cout<<pre<<endl;
        m--;cur++;
    }
 //   cout<<cur<<endl;
    int start=0;
    for(int i=0;i<cur;i++)
    {
        if(a[out]!='0')
        {
            start=i;
            break;
        }
        if(i==cur-1&&a[out]=='0')
            start=cur-1;
    }
    for(int i=start;i<cur;i++)
        printf("%c",a[out]);
    putchar(10);
}
    return 0;
}

HDOJ 3183 A Magic Lamp的更多相关文章

  1. hdu 3183 A Magic Lamp(RMQ)

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

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

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

  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 Limi ...

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

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

  6. HDU 3183 A Magic Lamp(RMQ问题, ST算法)

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

  7. 【HDOJ】3183 A Magic Lamp

    RMQ. /* 3183 */ #include <cstdio> #include <cstring> #include <cstdlib> #define MA ...

  8. HDU 3183 A Magic Lamp

    直接模拟   如果后一位比前一位小,那就一直 向前 pop()掉 维护他单调递增: #include<iostream> #include<cstring> #include& ...

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

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

随机推荐

  1. SQL Server 基础:Case两种用法

    测试数据 1).等值判断->相当于switch case select S#,C#,C#=( case C# when 1 then '语文' when 2 then '数学' when 3 t ...

  2. 003-python基础-变量与常量

    一.变量的定义 变量就是用来在程序运行期间存储各种需要临时保存可以不断改变的数据的标示符,一个变量应该有一个名字,并且在内存中占据一定的存储单元,在该存储单元中存放变量的值. 二.变量的声明 #!/u ...

  3. 【转载/修改】ScrollLayout代码修正,追加模仿viewpager滚动速度

    组件作用为类似ViewPager但直接插视图的横向滚动容器. 修改自:http://blog.csdn.net/yaoyeyzq/article/details/7571940 在该组件基础上修正了滚 ...

  4. eclipse集成maven

    1.工具下载: Eclipse4.2 jee版本(这里使用最新的Eclipse版本,3.7以上版本按照以下步骤都可以) 下载地址:http://www.eclipse.org/downloads/do ...

  5. 排序 选择排序&&堆排序

    选择排序&&堆排序 1.选择排序: 介绍:选择排序(Selection sort)是一种简单直观的排序算法.它的工作原理如下.首先在未排序序列中找到最小(大)元素,存放到排序序列的起始 ...

  6. poj 1383 Labyrinth

    题目连接 http://poj.org/problem?id=1383 Labyrinth Description The northern part of the Pyramid contains ...

  7. asp.net实现手机号码归属地查询

    protected void Button1_Click(object sender, EventArgs e)        {            if (Regex.IsMatch(TextB ...

  8. iOS之push present 动画

    直接源码: - (void) transitionWithType:(NSString *) type WithSubtype:(NSString *) subtype ForView : (UIVi ...

  9. centos下各种c++库文件的安装

    Centos编译boost   1.下载最新的boost http://www.boost.org/   2.解压文件 tar -xzvf boost_1_45_0.tar.gz    3.编译bja ...

  10. Modal的跳转方法为什么会显得那么奇怪

    初学Modal Segue的时候,并不能理解它为什么要做成这样.从A界面跳转到B界面还算正常,但是从B界面返回A界面,就显得略显猎奇了.必须先在A界面的Controller中自己造个方法@IBActi ...