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. cf499A-Watching a movie

    http://codeforces.com/problemset/problem/499/A A. Watching a movie   You have decided to watch the b ...

  2. IDEA错误的忽略了智能补全代码,导致正确的代码自动提示不出来的问题

    标题说起来有点绕,当今大部分IDE都提供 Alt+Enter 呼出自动补全菜单的功能,IDEA也不例外,今天手残了一下,具体问题如下: 1. 通常我们键入一个自定义类时IDEA会自动提示为红色,表示缺 ...

  3. Mask_RCNN训练自己的模型(练习)

  4. ATM取款机的数据库模拟开发和实战总结

    一.ATM实战开发的简介. 学习了几天的Oracle,开始着手用数据库PL/SQL语言做一个简单的ATM取款机业务,主要是为了巩固数据库的知识,并非真正的去实现高端的业务.有兴趣的可以看看,希望对同胞 ...

  5. Jenkins一天中构建多次

    Build after other projects are built:在其他项目触发的时候触发,里面有分为三种情况,也就是其他项目构建成功.失败.或者不稳定的时候触发项目: Poll SCM:定时 ...

  6. DOM0级与DOM2级

    定义: 0级DOM 分为2个:一是在标签内写onclick事件  二是在JS写onlicke=function(){}函数 1) <input id="myButton" t ...

  7. [C++] Swap Two Num

    Swap Two Num

  8. spring boot配置mybatis和事务管理

    spring boot配置mybatis和事务管理 一.spring boot与mybatis的配置 1.首先,spring boot 配置mybatis需要的全部依赖如下: <!-- Spri ...

  9. response乱码和request乱码

    response乱码 一.浏览器乱码 原因是:浏览器显示编码和文件的数据编码不一致 浏览器显示编码  response.setCharacterEncoding("utf-8"); ...

  10. twitter集成第三方登录是窗口一直出现闪退的解决方法

    需要创建自己的token,如下图