zoukankan      html  css  js  c++  java
  • zzuli求最大值

    1786: 求最大值

    Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 134  Solved: 28
    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

    2 5 5 2 3 6 1 2 3 2

    Sample Output

    5 1
    题解:
    题意是让找出a[i] - a[j]的最大值,但是i<j;
    思路是找出a[i]前面最大的数px,再贪心找出px-a[i]的最大值就好了。。。
    但是自己竟然想到了用LIS来找。。。直接px>a[i]?不就好了吗?把问题复杂化了。。。
    代码:
     1 #include<cstdio>
     2 #include<iostream>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<algorithm>
     6 using namespace std;
     7 const int MAXN=1e5+100;
     8 int main(){
     9     int T,N;
    10     scanf("%d",&T);
    11     while(T--){
    12         scanf("%d",&N);
    13         int x,px,ans=-1e7;
    14         scanf("%d",&x);
    15         px=x;
    16         for(int i=1;i<N;i++){
    17             scanf("%d",&x);
    18             ans=max(ans,px-x);
    19             if(x>px)px=x;
    20         }
    21         printf("%d
    ",ans);
    22     }
    23     return 0;
    24 }

    自己的二分超时代码。。。。纯属无用工。。。肯定超时了。。。

    //#include<bits/stdc++.h>
    #include<cstdio>
    #include<iostream>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    #include<vector>
    using namespace std;
    const int MAXN=1e5+100;
    int m[MAXN];
    vector<int>vec;
    typedef vector<int>::iterator ITE;
    ITE erfen(ITE a,ITE b,int x){
    	int mid,l=0,r=b-a;
    	while(l<r){
    		mid=(r-l)/2;
    		if(vec[mid]>=x)r=mid-1;
    		else l=mid+1;
    	}
    	return a+l;
    }
    int main(){
    	int T,n;
    	scanf("%d",&T);
    	while(T--){
    		scanf("%d",&n);
    		int mx,ans=-1e7;
    		vec.clear();
    		for(int i=0;i<n;i++){
    			scanf("%d",m+i);
    			if(i){
    				mx=vec[vec.size()-1];
    			//	printf("%d
    ",mx);
    				ans=max(ans,mx-m[i]);
    			}
    			vector<int>::iterator iter;
    			iter=erfen(vec.begin(),vec.end(),m[i]);
    			if(iter==vec.end())vec.push_back(m[i]);
    			else *iter=m[i];
    		}
    		printf("%d
    ",ans);
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    Ubuntu 上安装MySql
    Vue中组件的使用
    Swagger在 NETcore 中的使用
    awk 中 fieldwidths使用方法
    3.BIND从服务器及缓存服务器配置
    2.BIND服务基础及域主服务器配置
    1.DNS基础及域名系统架构
    Linux防火墙iptables的策略
    inode占用100%时硬盘无法写入文件故障处理
    特殊计算方式(数组)
  • 原文地址:https://www.cnblogs.com/handsomecui/p/4985556.html
Copyright © 2011-2022 走看看