1786: 求最大值

Time Limit: 1 Sec  Memory Limit:
128 MB

Submit: 222  Solved: 46



SubmitStatusWeb
Board

Description

给定n个数,a[1]到a[n],请你帮忙找出a[i] - a[j]的最大值,其中1 <= i < j <= n.

Input

第一行一个数T,表示一共有T组数据(T <= 20); 每组测试数据第一行一个整数n(2 <= n <= 10 ^ 5),表示接下来有n个数,第二行n个整数,不超过10 ^ 6,中间用空格隔开。

Output

每组数据输出一行,表示最大值是多少。

Sample Input

255 2 3 6 123 2

Sample Output

51

HINT


#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int num[100100],pre[100100];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
memset(num,0,sizeof(num));
memset(pre,0,sizeof(pre));
scanf("%d",&num[1]);
int k=1;
pre[1]=1;
for(int i=2;i<=n;i++)
{
scanf("%d",&num[i]);
if(num[i]>=num[k])
{
k=i;
}
else
{
if(pre[k])
{
if(num[i]<num[pre[k]])
pre[k]=i;
}
else
pre[k]=i;
}
}
int m=-1;
for(int i=1;i<n;i++)
m=max(m,num[i]-num[pre[i]]);
printf("%d\n",m);
}
return 0;
}

<pre name="code" class="cpp">#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int m,n;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
n--;
int m;
scanf("%d",&m);
int k;
int mm=-1;
while(n--)
{
scanf("%d",&k);
if(k>m)
m=k;
else
{
int s=m-k;
mm=max(mm,s);
}
}
printf("%d\n",mm);
}
return 0;
}

最新文章

  1. Mybatis 新增修改一条SQL
  2. 安装vim
  3. Mac 在命令行中获得Root权限
  4. Java性能提示(全)
  5. soapUI参数
  6. org.hibernate.id.IdentifierGenerationException: Unknown integral data type for ids : java.lang.String
  7. leetcode:Power of Two
  8. jquery实现点击页面空白隐藏指定菜单
  9. php 实现文件下载,兼容IE、Firefox、Chrome等浏览器
  10. ADO.NET——获取output 和 return值
  11. careercup-数学与概率 7.5
  12. 【HDU 4451 Dressing】水题,组合数
  13. 关联分析:FP-Growth算法
  14. 自写JS分页工具【基于JQ】
  15. PHP常见错误
  16. 《CLR Via C#》学习--线程开销
  17. 使用MFC创建C++程序
  18. visual studio 加入zen-codding
  19. VuePress
  20. Linux学习之Vim/Vi使用(十三)

热门文章

  1. 剑指offer——03从尾至头打印列表(Python3)
  2. mock non-virtual methods
  3. C#中SQL参数传入空值报错解决方案
  4. AARRR:互联网创业者一定要掌握的指标
  5. GCC中的强符号和弱符号及强引用和弱引用
  6. RocketMQ学习笔记(11)----RocketMQ的PushConsumer和PullConsumer
  7. criteria的projection做分页
  8. CF1012B Chemical table 构造_思维_并查集
  9. python的迭代器、生成器、三元运算、列表解析、生成器表达式
  10. IOS - Autorelease for CGMutablePathRef?