http://acm.hdu.edu.cn/showproblem.php?pid=3183

A Magic Lamp

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

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   |   We have carefully selected several similar problems for you:  3188 3189 3184 3185 3186 
 
   思路主要是这样的: 因为需要你删除m个数使得结果最小,所以每次对字符串进行一次遍历,从前往后,只要a[i]>a[j] (j的位置为i后面未标记的第一个)  则对a[i]进行一次标记(赋赋值)将其除外,进行n次遍历,这样就删除了n个数字。因为删除的都是和后面比较相比下大的,所得结果当然就最小了。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <algorithm>
#include <iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include <stdio.h>
#include <string.h>
#include <vector>
#define ME(x , y) memset(x , y , sizeof(x))
#define SF(n) scanf("%d" , &n)
#define rep(i , n) for(int i = 0 ; i < n ; i ++)
#define INF  0x3f3f3f3f
using namespace std;
] ;
];
];
int main()
{
    int n , m ;
    while(~scanf("%s%d" , s , &m))
    {
        n = strlen(s);
        memset(a ,  , sizeof(a));
         ; i < n ; i++)
            a[i] = s[i] - ';
         ; i < m ; i++)//找到m个要删除的数
        {
             ; j < n ; j++)
            {
              //  if(a[j] > 0)//排除标记的
              //  {
                    int k ;
                     ; k < n ; k++)
                    {
                        )//排除标记的
                        break ;
                    }
                    if(a[j] > a[k])//找第一个开始递减的数
                    {
                        a[j] = - ;
                        break ;
                    }
              //  }
            }
        }
         , len =  ;
         ; i < n ; i++)
        {
            )//去前置零
                continue ;
            )
                continue ;
            b[len++] = a[i];
            )
                flag =  ;
        }
         ; i < len ; i++)
            printf("%d" , b[i]);
        )
            printf(");
        printf("\n");
    }
     ;
}

RMQ:

因为要找n-m个数,删除m个数。所以原数的第1位到m+1位的数字中最小的那位(假设是第i位)肯定是n-m位数的第一位。(想想为什么)

这样我们就找到了第一位a[i],接下来我们在从第i+1位数到m+2位数中找最小的那位,这个肯定是n-m位数的第二位。

以此类推,找够n-m位即可。

RMQ函数要做点修改。dmin[i][j]=k表示的是区间[i,i+(1<<j)-1]内最小值的下标而不是值了。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <algorithm>
#include <iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include <stdio.h>
#include <string.h>
#include <vector>
#define ME(x , y) memset(x , y , sizeof(x))
#define SF(n) scanf("%d" , &n)
#define rep(i , n) for(int i = 0 ; i < n ; i ++)
#define INF  0x3f3f3f3f
using namespace std;
] ;
];
] , dp[][];
int m , n ;

int Min(int x , int y)
{
    return s[x] <= s[y] ? x : y ;
}

void RMQ()
{
    memset(dp ,  , sizeof(dp));
      ; i < n ; i++)
        dp[i][] = i ;
     ; j <  ; j++)
    {
         ; i < n ; i++)
        {
             << j) -  < n)
            {
                dp[i][j] = Min(dp[i][j-] , dp[i+(<<j-)][j-]);
            }
        }
    }

}

int query(int l , int r)
{
    );
    <<k)+][k]);
}

vector<int>v;
int main()
{
    while(~scanf("%s%d" , s , &m))
    {
        v.clear();
        n = strlen(s);
        RMQ();
        m = n - m ;
         ;
        while(m--)
        {
            // 在 n - m 区间至少留一个数
            pos = query(pos , n - m - );//求的是最小值的下标
            v.push_back(pos);
            pos +=  ;
        }
         ;
         ; i < v.size() ; i++)
        {
            if(flag)
            {
                flag =  ;
                cout << s[v[i]];
            }
            ')
            {
                flag =  ;
                cout << s[v[i]] ;
            }
        }
        if(!flag)
            cout <<  ;
        cout <<endl ;
    }

     ;
}

RMQ(鸽巢原理或字符串操作)的更多相关文章

  1. hdu 3183 rmq+鸽巢原理

    题目大意: 给你一个数字字符串序列,给你要求删掉的数字个数m,删掉m个数使的剩下的数字字符串的之最小.并输出这个数字: 基本思路; 这题解法有很多,贪心,rmq都可以,这里选择rmq,因为很久没有写r ...

  2. [BZOJ4722]由乃[鸽巢原理+bitset+倍增]

    题意 给定长为 \(n\) 序列 \(a\) ,要求支持两种操作: \(1.\) 询问在一个区间 \([l,r]\) 中,是否能够选出两个交集为空的集合 $ \rm X ,Y$, 使得 \(\sum_ ...

  3. POJ2356 Find a multiple 抽屉原理(鸽巢原理)

    题意:给你N个数,从中取出任意个数的数 使得他们的和 是 N的倍数: 在鸽巢原理的介绍里面,有例题介绍:设a1,a2,a3,……am是正整数的序列,试证明至少存在正数k和l,1<=k<=l ...

  4. 51nod 1574 排列转换(贪心+鸽巢原理)

    题意:有两个长度为n的排列p和s.要求通过交换使得p变成s.交换 pi 和 pj 的代价是|i-j|.要求使用最少的代价让p变成s. 考虑两个数字pi和pj,假如交换他们能使得pi到目标的距离减少,p ...

  5. Two progressions(CodeForces-125D)【鸽巢原理】

    题意:将一列数划分为两个等差数列. 思路:首先,我要吹爆鸽巢原理!!!真的很强大的东西!!! 加入能完成题设操作,则前三个数中,必有至少两个数在同一序列,枚举三种情况(a1 a2,a2 a3,a1 a ...

  6. POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理

    Halloween treats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7644   Accepted: 2798 ...

  7. POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理

    Find a multiple Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7192   Accepted: 3138   ...

  8. cf319.B. Modulo Sum(dp && 鸽巢原理 && 同余模)

    B. Modulo Sum time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  9. poj 2356 Find a multiple(鸽巢原理)

    Description The input contains N natural (i.e. positive integer) numbers ( N <= ). Each of that n ...

随机推荐

  1. EwoMail 邮件服务器安装

    ewomail 安装及使用 主页:http://www.ewomail.com/ 开源版主页:http://www.ewomail.com/list-9.html 开源版文档:http://doc.e ...

  2. 解压版(.zip)的MySQL数据库下载安装

    1.首先到mysql官网下载(https://dev.mysql.com/downloads/mysql/5.6.html#downloads): 2.解压下载好的压缩包文件,并将解压后的文件夹放到合 ...

  3. 破坏双亲委托机制的一些情况---Tomcat和JDBC,破坏后的安全问题

    采用双亲委托机制的原因 类加载器就是将字节码搬进jvm方法区的组件.我们知道,JVM识别加载进来的类是通过类加载器+类全名完成的,也就是说同一个类由不同类加载器加载进去的话就会被视为不同的类.jdk提 ...

  4. JavaScript 复杂判断的更优雅写法借鉴

    前言: 我们编写js代码时经常遇到复杂逻辑判断的情况,通常大家可以用if/else或者switch来实现多个条件判断,但这样会有个问题,随着逻辑复杂度的增加,代码中的if/else/switch会变得 ...

  5. XML 语法

    XML 语法规则 本节的目的是想让你了解 XML 中的语法所依据的规则,避免在编写 XML 文档的时候遇到错误. XML 的语法规则很简单,且很有逻辑.这些规则很容易学习,也很容易使用. 所有的 XM ...

  6. jquery用法第二波

    过滤器 属性过滤选择器: $("div[id]")选取有id属性的<div> $(“#id”) $("div[title=test]")选取titl ...

  7. flutter中的按钮组件

    Flutter 里有很多的 Button 组件很多,常见的按钮组件有:RaisedButton.FlatButton.IconButton.OutlineButton.ButtonBar.Floati ...

  8. 【SpringBoot】 Java中如何封装Http请求,以及JSON多层嵌套解析

    前言 本文中的内容其实严格来说不算springboot里面的特性,属于JAVA基础,只是我在项目中遇到了,特归纳总结一下. HTTP请求封装 目前JAVA对于HTTP封装主要有三种方式: 1. JAV ...

  9. random——伪随机数生成模块

    random——伪随机数生成模块 转自:https://blog.csdn.net/zhtysw/article/details/79978197 该模块包含构造伪随机数生成器的多个方法.对于整数,伪 ...

  10. 凉经-乐糖游戏-PHP开发实习生

    收到面试通知当天因为学校出事要求我明天必须回去,所以就买当晚的火车票,然后跟公司说学校有事明天没法去面试了,公司人事比较好给我安排到当天下午面试.公司规模不是很大,但位置好下了地铁到,可能因为招的是实 ...