HDOJ 3183 A Magic Lamp

时间:2023-01-31 22:26:06

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&lpar;RMQ&rpar;

    题目链接: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 - &lbrack;RMQ&rsqb;&lbrack;ST算法&rsqb;

    题目链接: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&lpar;RMQ问题, ST算法&rpar;

    原题目 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. linux 网卡问题 Device eth0 does not seem to be present&comma;delaying initialization&period;

    Device eth0 does not seem to be present,delaying initialization. 网上搜索后才发现原因所在:原来vmware在复制了虚拟机后会自动生成一 ...

  2. &lbrack;译&rsqb;Profile and debug your ASP&period;NET MVC app with Glimpse

    原文:http://www.asp.net/mvc/overview/performance/profile-and-debug-your-aspnet-mvc-app-with-glimpse Gl ...

  3. Codeforces Round &num;354 &lpar;Div&period; 2&rpar;-D

    D. Theseus and labyrinth 题目链接:http://codeforces.com/contest/676/problem/D Theseus has just arrived t ...

  4. C&plus;&plus;的STL中vector内存分配方法的简单探索

    STL中vector什么时候会自动分配内存,又是怎么分配的呢? 环境:Linux  CentOS 5.2 1.代码 #include <vector> #include <stdio ...

  5. Java--再次理解多态

    Java中多态性(polymorphism)的实现 什么是多态 1. 面向对象的三大特性:封装.继承.多态.从一定角度来看,封装和继承几乎都是为多态而准备的.这是我们最后一个概念,也是最重要的知识点. ...

  6. getopt&lowbar;long函数使用【转】

    转自:https://blog.csdn.net/cashey1991/article/details/7942809 平时在写程序时常常需要对命令行参数进行处理,当命令行参数个数较多时,如果按照顺序 ...

  7. C&num; Vista Command Link Control with Windows Forms

    using System; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; u ...

  8. Open ID Connect&lpar;OIDC&rpar;在 ASP&period;NET Core中的应用

    我们在<ASP.NET Core项目实战的课程>第一章里面给identity server4做了一个全面的介绍和示例的练习 ,这篇文章是根据大家对OIDC遇到的一些常见问题整理得出. 本文 ...

  9. 解决ansible上传速度慢的问题

    问题: 假如有A.B.C.D....等机器,机器A为Ansible服务器,机器B.C.D...等为Ansible管理的节点服务器,A机器与其他机器都不在同一个网络,也就是A机器必须添加VPN之后才能与 ...

  10. 学习笔记12之通过ajax动态添加选项