HDU 3410 Passing the Message
可以先处理出每个a[i]最左和最右能到达的位置,L[i],和R[i]。然后就只要询问区间[ L[i],i-1 ]和区间[ i+1,R[i] ]最大值位置即可。
#include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<vector> #include<map> #include<set> #include<queue> #include<stack> #include<iostream> using namespace std; typedef long long LL; const double pi=acos(-1.0),eps=1e-8; void File() {freopen("D:\\in.txt","r",stdin);freopen("D:\\out.txt","w",stdout); } inline int read() {char c = getchar(); while(!isdigit(c)) c = getchar();int x = 0;while(isdigit(c)) { x = x * 10 + c - '0'; c = getchar(); }return x; }const int maxn=50000+10; int T,n,a[maxn],L[maxn],R[maxn],dp[maxn][30];void RMQ_init() {for(int i=0;i<n;i++) dp[i][0]=i;for(int j=1;(1<<j)<=n;j++)for(int i=0;i+(1<<j)-1<n;i++){if(a[dp[i][j-1]]>a[dp[i+(1<<(j-1))][j-1]]) dp[i][j]=dp[i][j-1];else dp[i][j]=dp[i+(1<<(j-1))][j-1];} }int RMQ(int L,int R) {int k=0;while((1<<(k+1))<=R-L+1) k++;if(a[dp[L][k]]>a[dp[R-(1<<k)+1][k]]) return dp[L][k];return dp[R-(1<<k)+1][k]; }int main() {scanf("%d",&T); int cas=1;while(T--){scanf("%d",&n);for(int i=0;i<n;i++) scanf("%d",&a[i]), L[i]=R[i]=i;for(int i=1;i<n;i++){if(a[i]<a[i-1]) continue; int pre=L[i-1];while(1) { L[i]=pre; if(pre==0||a[pre-1]>a[i]) break; pre=L[pre-1]; }}for(int i=n;i>=1;i--) R[i]=i;for(int i=n-2;i>=0;i--){if(a[i]<a[i+1]) continue; int pre=R[i+1];while(1) { R[i]=pre; if(pre==n-1||a[pre+1]>a[i]) break; pre=R[pre+1]; }}RMQ_init();printf("Case %d:\n",cas++);for(int i=0;i<n;i++){if(L[i]>i-1) printf("0 "); else printf("%d ",RMQ(L[i],i-1)+1);if(R[i]<i+1) printf("0\n"); else printf("%d\n",RMQ(i+1,R[i])+1);}}return 0; }
转载于:https://www.cnblogs.com/zufezzt/p/5740703.html
HDU 3410 Passing the Message相关推荐
- HDU - 3410 Passing the Message 单调递减栈
Passing the Message What a sunny day! Let's go picnic and have barbecue! Today, all kids in "Su ...
- 单调栈 左右传消息 HDU 3410
Passing the Message What a sunny day! Let's go picnic and have barbecue! Today, all kids in "Su ...
- 【HDU - 3410 】 Passing the Message(单调栈)
题干: What a sunny day! Let's go picnic and have barbecue! Today, all kids in "Sun Flower" k ...
- Passing the Message(HDU-3410)
Problem Description What a sunny day! Let's go picnic and have barbecue! Today, all kids in "Su ...
- Passing the Message(单调栈)
What a sunny day! Let's go picnic and have barbecue! Today, all kids in "Sun Flower" kinde ...
- 【ZOJ - 2724】【HDU - 1509】Windows Message Queue(优先队列)
题干: Message queue is the basic fundamental of windows system. For each process, the system maintains ...
- HDU 4300 Clairewd’s message
一道KMP的变式 本题仍是求最大前缀后缀,所以仍用KMP,但不同的是,本题有一个密码转换规则,不过好在题目中说了两段不重合,那么我们就可以在中间插入一个特殊符号'*'',保证求next数组时不会越过中 ...
- HDU3410 Passing the Message 【单调栈】
题意 一个小孩可以看到左边比他矮,但是最高的孩子,同样可以看到右边比他矮,但是最高的小孩 分析 求左边比他矮,但是最高的孩子:维护一个单调栈,从栈顶到栈底单调递增: 从左边第一个孩子开始,如果栈为空或 ...
- HDU - 4300 Clairewd’s message(扩展KMP)
题目链接:点击查看 题目大意:给出两个字符串 s 和 t ,字符串 s 代表着一种密码的映射,字符串 t 代表着一段密文+明文,题目保证密文是完整的,但明文只有一部分,现在问如何补全字符串 t ,使得 ...
最新文章
- oracle误删scott文件如何恢复
- MATLAB从入门到精通-Matlab R2020b新功能 | 子标题和标题/标签对齐功能!
- mysql为什么采用b树_为什么MongoDB采用B树索引,而Mysql用B+树做索引
- 可以输入值的下拉框(select和input的组合使用)
- pythonjava解释xml_Python解析XML文档
- linux 高性能读书笔记之通用socket地址
- 在Nginx/Tengine服务器上安装SSL证书
- 炫酷流光个人主页源码分享及在线部署教程
- Springboot+vue项目疫情社区防控系统
- amtlib.dll被McAfee删除之后?
- Windows 7安装超级终端连接COM口设备
- logisim 数码管输出设计
- 大数据在智慧消防领域的应用
- Web前端开发中最基本的找错误的方法, 你得了解一下
- 硬件:Nand Flash、Nor Flash
- 如何带好一个20人团队?
- 【动网论坛7.1 sp1 修改】-加强检查注册昵称,防止全数字及简单重复ID注册
- windows XP系统文件诠释C(转)
- Hi-End音箱真伪(3)-巴汉著文
- python音乐可视化效果_Python 一个漂亮的音乐节奏可视化方案!我觉得可行!