数据结构实验之栈与队列六:下一较大值(二)(SDUT 3333)
2024-10-06 14:17:22
#include <bits/stdc++.h>
using namespace std;
int a[1000006];
int b[1000006];
int sta[100006];
int main()
{
int t,n,i,j,top;
while(~scanf("%d",&t))
{
while(t--)
{
scanf("%d",&n);
for( i = 1; i <= n; i ++)
{
scanf("%d", &a[i]);
}
top = 0;
b[n] = -1;
for(i = n - 1; i >= 1; i --)
{
if(a[i + 1] > a[i])
{
b[i] = a[i + 1];
sta[top ++] = b[i];
// cout << "sta[top] = "<< sta[top - 1] << endl;
}
else {
int f = 1;
while(top)
{
// cout << "top = " <<top << endl;
if(sta[top - 1] > a[i])
{
b[i] = sta[top - 1];
f = 0;
break;
}
top --;
}
if(top == 0 || f == 1) b[i] = -1;
}
}
for(i = 1; i <= n; i ++)
{
printf("%d-->%d\n",a[i],b[i]);
}
if(t != 0) printf("\n");
}
}
return 0;
}
最新文章
- 并查集+树链剖分+线段树 HDOJ 5458 Stability(稳定性)
- 关于Memo或者Edit之类控件, 直接设置Text无法撤销的解决方案
- Java基础之一组有用的类——使用公历日历(TryCalendar)
- 简单实用的纯CSS百分比圆形进度条插件
- Apache模块 mod_proxy 转自http://www.php100.com/manual/apache2/mod/mod_proxy.html
- 001. 为input type=text 时设置默认值
- MVC中前台如何向后台传递数据------$.get(),$post(),$ajax(),$.getJSON()总结
- SaaS系列介绍之十二: SaaS产品的研发模式
- btn控件
- Error message “Assembly must be registered in isolation” when registering Plugins in Microsoft Dynamics CRM 2011 2013 解决办法
- Python开发【第二十二篇】:Web框架之Django【进阶】
- .Net 4.0 Convert Object to XDocument
- bootstrap之 Badge 角标
- PHP中将对数据库的操作,封装成一个工具类以及学会使用面向对象的方式进行编程
- 深度学习工具caffe具体安装指南
- Ext.Net 使用总结之GridPanel中的选中行
- Web程序和应用程序服务器[转]
- [leetcode-560-Subarray Sum Equals K]
- 历年NOIP选题题解汇总
- 【unix网络编程第三版】ubuntu端口占用问题
热门文章
- SQL优化中的重要概念:锁定
- Asp.Net Core Linux环境下 找不到配置文件、静态文件的问题
- python实现nc
- ThinkPHP5.0.*远程代码执行漏洞预警
- Python中的内存管理机制
- springboot2.0介绍1
- 利用jenkens+github实现简单的CI/CD
- 解决yum命令后出现libldap-2.4.so.2: cannot open shared object file
- MySQL全同步复制基于GR集群架构实现(Centos7)
- Linux服务器TIME_WAIT进程的解决与原因