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. Texture Combiner

    [Texture Combiner] After the basic vertex lighting has been calculated, textures are applied. In Sha ...

  2. Hibernate批量抓取

    ------------------siwuxie095 Hibernate 批量抓取 以客户和联系人为例(一对多) 1.批量抓取 同时查询多个对象的关联对象,是 Hibernate 抓取策略的一种 ...

  3. IDEA06 代码规范检测插件之Alibaba Java Coding Guidelines

    1 官方资料 1.1 官方介绍 https://mp.weixin.qq.com/s/IbibsXlWHlM59kfXJqRvZA#rd 1.2 github地址 https://github.com ...

  4. hibernate 反向生实体类 and 为什么老是多一个id

    hibernate 反向生实体类 and 为什么老是多一个id 2017年04月01日 20:32:51 阅读数:548

  5. Qt + OpenSenceGraph(osg) 加载OSG模型

  6. 解决git无法clone地址为https的库

    一.问题描述 早上在学习<Spark快速大数据分析>的时候,需要下载书本的实例代码,于是用git clone一下给出的库: https://github.com/databricks/le ...

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

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

  8. Checked异常和Runtime异常体系

    Java的异常被分为两大类:Checked异常和Runtime异常(运行时异常).所有的RuntimeException类及其子类的实例被称为Runtime异常:不是RuntimeException类 ...

  9. Netty之Reactor模式

    无论是C++还是Java编写的网络框架,大多数都是基于Reactor模式进行设计和开发,Reactor模式基于事件驱动,特别适合处理海量的I/O事件. 1. 单线程模型 Reactor单线程模型,指的 ...

  10. oracle 环境变量(中文显示乱码)

     NLS_LANGSIMPLIFIED CHINESE_CHINA.ZHS16GBK