HDU3183 贪心/RMQ-ST表
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
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?
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.
If the result contains leading zero, ignore it.
#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表的更多相关文章
- BZOJ_2006_[NOI2010]超级钢琴_贪心+堆+ST表
BZOJ_2006_[NOI2010]超级钢琴_贪心+堆+ST表 Description 小Z是一个小有名气的钢琴家,最近C博士送给了小Z一架超级钢琴,小Z希望能够用这架钢琴创作出世界上最美妙的 音乐 ...
- 【NOI2010】超级钢琴 题解(贪心+堆+ST表)
题目链接 题目大意:求序列内长度在$[L,R]$范围内前$k$大子序列之和. ---------------------- 考略每个左端点$i$,合法的区间右端点在$[i+L,i+R]$内. 不妨暴力 ...
- RMQ——ST表
ST表 ST表是一种解决RMQ问题的强有力工具, 可以做到O(nlogn)预处理,O(1)查询. st[i][j] 表示区间 [i, i + 2 ^ j - 1] 的最大值. 初值 st[i][0] ...
- hdu6356 Glad You Came 杭电多校第五场 RMQ ST表(模板)
Glad You Came Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) ...
- RMQ—ST表
RMQ(Range Minimum/Maximum Query),RMQ是一个求给定范围内最大最小值的问题.我们一般使用st算法来解决这类问题(Sparse Table).这个算法原理不难,主要是各种 ...
- [NOI2010] 超级钢琴 - 贪心,堆,ST表
这也算是第K大问题的套路题了(虽然我一开始还想了个假算法),大体想法就是先弄出最优的一批解,然后每次从中提出一个最优解并转移到一个次优解,用优先队列维护这个过程即可. 类似的问题很多,放在序列上的,放 ...
- Hdu 5289-Assignment 贪心,ST表
题目: http://acm.hdu.edu.cn/showproblem.php?pid=5289 Assignment Time Limit: 4000/2000 MS (Java/Others) ...
- 51Nod.1766.树上最远点对(树的直径 RMQ 线段树/ST表)
题目链接 \(Description\) 给定一棵树.每次询问给定\(a\sim b,c\sim d\)两个下标区间,从这两个区间中各取一个点,使得这两个点距离最远.输出最远距离. \(n,q\leq ...
- CF1039E Summer Oenothera Exhibition 贪心、根号分治、倍增、ST表
传送门 感谢这一篇博客的指导(Orzwxh) $PS$:默认数组下标为$1$到$N$ 首先很明显的贪心:每一次都选择尽可能长的区间 不妨设$d_i$表示在取当前$K$的情况下,左端点为$i$的所有满足 ...
- RMQ问题 - ST表的简单应用
2017-08-26 22:25:57 writer:pprp 题意很简单,给你一串数字,问你给定区间中最大值减去给定区间中的最小值是多少? 用ST表即可实现 一开始无脑套模板,找了最大值,找了最小值 ...
随机推荐
- Linux实战教学笔记34:企业级监控Nagios实践(上)
一,Nagios监控简介 生活中大家应该对监控已司空见惯了,例如:餐馆门前的监控探头,小区里的视频监控,城市道路告诉监控探头等,这些监控的目的大家都很清楚,无须多说.那么,企业工作中为什么要部署监控系 ...
- if UNITY_EDITOR这个判断常用,还有哪个常用捏?
#if DEVELOPMENT_BUILD || UNITY_EDITOR DEVELOPMENT_BUILD表示开发版的意思,会在程序右下角显示 Development Build 我们可以根据这个 ...
- C#创建COM组件
本文详细阐述如何用C#创建COM组件,并能用VC6.0等调用. 附:本文适用任何VS系列工具. 在用C#创建COM组件时,一定要记住以下几点: 1.所要导出的类必须为公有: 2.所有属性.方法也必须为 ...
- table 合并行和列
table合并行列,以及拆分 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "htt ...
- Openssl rand命令
一.简介 rand命令用来产生伪随机字节,随机数字产生器需要一个seed,在没有/dev/srandom系统下的解决方法是自己做一个~/.rnd文件 二.语法 openssl rand [-out f ...
- [c++] polymorphism without virtual function
polymorphism without virtual function
- 清北学堂 day6 兔子
---恢复内容开始--- [问题描述] 在一片草原上有N个兔子窝,每个窝里住着一只兔子,有M条路径连接这些窝.更特殊地是,至多只有一个兔子窝有3条或更多的路径与它相连,其它的兔子窝只有1条或2条路径与 ...
- Flip
Flip是一个能够让任意HTML.文本或jQuery Element产生漂亮翻转效果的jQuery插件. 可以配置翻转方向:从右到左.上到下或从左到右.下到上.翻转的速度也可以配置. 效果如下图所示: ...
- python json.dumps输出中文问题
json.dumps 输出的中文是"\u6211\u662f"格式的,输出中文需要指定ensure_ascii=False. json.dumps(actual,ensure_as ...
- org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'supplierAction': Injection of resource dependencies failed; nested exception is org.springframework.beans.factor
这个错误是因为抽象Action类的时候,把ServletContext写成了serverContext,导致无法注入,正确写法是 import javax.annotation.Resource; i ...