A Magic Lamp

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

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
 
这个题题意很简单,就是给你一个长度为n的数,要求从中去掉m位,剩余位顺序不变,求能得到的最小的数
其中允许得到有前导零的数
用贪心做的话就很简单,通过观察我们可以得到一个结论
最终答案的第一位一定在下标为0~m之间 如果不是这样的话我们就无法凑出n-m位的数了
同理,第二位一定在上一位的下标+1到m+1之间
代码也很简单:
 #include <algorithm>
#include <iostream>
#include <cstring>
using namespace std; int main()
{
ios::sync_with_stdio(false);
int m;
char str[];
while (cin >> str >> m)
{
int len = strlen(str);
if (len <= m)
{
cout << "" << endl;
continue;
} int flag = ;
int n = len;
int l = , r = m;
for (int i = ; i < n - m; i++)
{
char min = str[l];
int pos = l;
for (int j = l; j <= r; j++)
{
if (str[j]< min)
{
min = str[j];
pos = j;
}
}
l = pos + ;
r++;
if (!flag && min == '')
continue;
cout << min;
flag++;
} if (!flag)
cout << ;
cout << endl;
} return ;
}

用st表的话也差不多是一个思路,我们先用st表处理得到各个区间最小的数的下标,然后同样按照贪心去求0~m,pos+1~m+1,pos+1~m+2.......

 #include <iostream>
#include <cmath>
#include <string> using namespace std; string s;
int n;
int st[][];
char ans[]; int Min(int a, int b)
{
return s[a] <= s[b] ? a : b;
} void init()
{
for (int i = ; i < s.size(); i++)
st[i][] = i; for (int j = ; ( << j) < s.size(); j++)
for (int i = ; i + ( << j) - < s.size(); i++)
{
st[i][j] = Min(st[i][j - ], st[i + ( << (j - ))][j - ]);
//cout << i << ends << j << endl;
}
} int search(int l, int r)
{
int k = (int)(log((double)(r - l + )) / log(2.0));
return Min(st[l][k], st[r - ( << k) + ][k]);
} int main()
{
ios::sync_with_stdio(false);
while (cin >> s >> n)
{
init();
int len = s.size() - n;
int pos = ;
int flag = ;
while (len--)
{
pos = search(pos,n + flag);
ans[flag++] = s[pos];
//cout << s[pos] << endl;
pos++;
} flag = ; for (int i = ; i < s.size() - n; i++)
{
if (!flag && ans[i] == '')
continue;
flag++;
cout << ans[i];
}
if (!flag)
cout << ;
cout << endl;
} return ;
}

HDU3183 贪心/RMQ-ST表的更多相关文章

  1. BZOJ_2006_[NOI2010]超级钢琴_贪心+堆+ST表

    BZOJ_2006_[NOI2010]超级钢琴_贪心+堆+ST表 Description 小Z是一个小有名气的钢琴家,最近C博士送给了小Z一架超级钢琴,小Z希望能够用这架钢琴创作出世界上最美妙的 音乐 ...

  2. 【NOI2010】超级钢琴 题解(贪心+堆+ST表)

    题目链接 题目大意:求序列内长度在$[L,R]$范围内前$k$大子序列之和. ---------------------- 考略每个左端点$i$,合法的区间右端点在$[i+L,i+R]$内. 不妨暴力 ...

  3. RMQ——ST表

    ST表 ST表是一种解决RMQ问题的强有力工具, 可以做到O(nlogn)预处理,O(1)查询. st[i][j] 表示区间 [i, i + 2 ^ j - 1] 的最大值. 初值 st[i][0] ...

  4. hdu6356 Glad You Came 杭电多校第五场 RMQ ST表(模板)

    Glad You Came Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) ...

  5. RMQ—ST表

    RMQ(Range Minimum/Maximum Query),RMQ是一个求给定范围内最大最小值的问题.我们一般使用st算法来解决这类问题(Sparse Table).这个算法原理不难,主要是各种 ...

  6. [NOI2010] 超级钢琴 - 贪心,堆,ST表

    这也算是第K大问题的套路题了(虽然我一开始还想了个假算法),大体想法就是先弄出最优的一批解,然后每次从中提出一个最优解并转移到一个次优解,用优先队列维护这个过程即可. 类似的问题很多,放在序列上的,放 ...

  7. Hdu 5289-Assignment 贪心,ST表

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=5289 Assignment Time Limit: 4000/2000 MS (Java/Others) ...

  8. 51Nod.1766.树上最远点对(树的直径 RMQ 线段树/ST表)

    题目链接 \(Description\) 给定一棵树.每次询问给定\(a\sim b,c\sim d\)两个下标区间,从这两个区间中各取一个点,使得这两个点距离最远.输出最远距离. \(n,q\leq ...

  9. CF1039E Summer Oenothera Exhibition 贪心、根号分治、倍增、ST表

    传送门 感谢这一篇博客的指导(Orzwxh) $PS$:默认数组下标为$1$到$N$ 首先很明显的贪心:每一次都选择尽可能长的区间 不妨设$d_i$表示在取当前$K$的情况下,左端点为$i$的所有满足 ...

  10. RMQ问题 - ST表的简单应用

    2017-08-26 22:25:57 writer:pprp 题意很简单,给你一串数字,问你给定区间中最大值减去给定区间中的最小值是多少? 用ST表即可实现 一开始无脑套模板,找了最大值,找了最小值 ...

随机推荐

  1. Scala操作MongoDB

    Scala操作MongoDB // Maven <dependencies> <dependency> <groupId>org.mongodb</group ...

  2. Apache Hive (五)DbVisualizer配置连接hive

    转自:https://www.cnblogs.com/qingyunzong/p/8715250.html 一.安装DbVisualizer 下载地址http://www.dbvis.com/ 也可以 ...

  3. 【BZOJ 2120】数颜色【分块/莫队】

    题意 给出n个数字和m个操作.操作有两种.1:查询区间[l,r]内不同种类得数字个数.2: 将下标为p得数字修改为v 分析 如果不是修改操作的话,用莫队贼简单就可以水过,但是因为带了修改就有一些麻烦了 ...

  4. 十大基于Docker的开发工具

    http://www.infoq.com/cn/news/2014/08/top-10-open-source-docker FlynnFlynn是一个使用Go语言编写的开源PaaS平台,Flynn使 ...

  5. Nginx概述、安装及配置详解

    nginx概述 nginx是一款自由的.开源的.高性能的HTTP服务器和反向代理服务器:同时也是一个IMAP.POP3.SMTP代理服务器:nginx可以作为一个HTTP服务器进行网站的发布处理,另外 ...

  6. 创建一个实例&创建一个线程。。

    using System; using System.Threading; namespace WorkerThread02 { class ThreadTest { bool done; stati ...

  7. Charles 4.2.5 破解原理.RP

    请支持正版,仅供技术交流. 写了个小工具方便在线处理,在线破解 https://github.com/8enet/Charles-Crack 2018/4/8 Charles 4.2.5 替换类名和方 ...

  8. 迁移ORACLE数据库文件到ASM

    迁移数据文件到ASM 数据库一致性情况下迁移:将数据库启动到mount状态,生成rman copy 语句,然后在rman中执行: SQL> startup mount SQL> selec ...

  9. C#记录程序运行时间

    主要:using System.Diagnostics;当中有Stopwatch类: 介绍如下: // 摘要: // 提供一组方法和属性,可用于准确地测量运行时间. public class Stop ...

  10. 编写高质量代码改善C#程序的157个建议——建议86:Parallel中的异常处理

    建议86:Parallel中的异常处理 建议85阐述了如何处理Task中的异常.由于Task的Start方法是异步启动的,所以我们需要额外的技术来完成异常处理.Parallel相对来说就要简单很多,因 ...