• zzulioj--1786--求最大值(技巧题)


    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
    ",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
    ",mm);
    	}
    	return 0;
    }


    
    

  • 相关阅读:
    Extjs 中combobox下拉框初始化赋值
    关于继承的理解
    近来很郁闷
    Java String 常量池理解
    关于时间复杂度的讲解
    List、Set和数组之间的转换(转载)
    提升自我修养的具体方法有哪些?
    Mapped Statements collection does not contain value fo
    io流导出csv
    反射,System.Type类
  • 原文地址:https://www.cnblogs.com/playboy307/p/5273585.html
Copyright © 2020-2023  润新知