TOJ 4171 L-The math problem(水题)

本文介绍了一个简单的编程问题:给定一组整数,寻找最大的数差(aj-ai),其中j≥i。通过一次遍历并维护当前最小值及最大差值的方式解决此问题。输入包含多个测试案例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

4171.   L-The math problem
Time Limit: 1.0 Seconds   Memory Limit:65536K
Total Runs: 287   Accepted Runs:86

Given an array a with n intergers, please tell me the max(ajai),0ijn1max(aj−ai),0≤i≤j≤n−1.

Input

The input consists of multiple test cases. The first line contains an integer TT, indicating the number of test cases.(1T1000)(1≤T≤1000)
Each case contains one integer NN.(0N107)(0≤N≤107). Then comes a line with N intergers ai(107ai107)ai(−107≤ai≤107)

Output

For each case only output the answer.

Sample Input

1
5
1 3 5 4 2

Sample Output

4

水题 直接扫一遍 维护最大差值

注意要看清题,j是大于等于i的


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdlib>
using namespace std;

int a[10000005];

int main()
{
	int t;
	while(~scanf("%d",&t))
	{
		while(t--){
			memset(a,0,sizeof(a));
			int n;
			scanf("%d",&n);
			for(int i=0;i<n;i++){
				scanf("%d",&a[i]);
			} 
			int max_dif=0;
			int minn=999999;
			for(int i=0;i<n;i++){
				if(minn>a[i]){
					minn=a[i];
				}
				if(a[i]-minn>max_dif){
					max_dif=a[i]-minn;
				}
			}
			printf("%d\n",max_dif);
		}
	}
	return 0;	
} 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值