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]?不就好了吗?把问题复杂化了。。。
代码:
 #include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int MAXN=1e5+;
int main(){
int T,N;
scanf("%d",&T);
while(T--){
scanf("%d",&N);
int x,px,ans=-1e7;
scanf("%d",&x);
px=x;
for(int i=;i<N;i++){
scanf("%d",&x);
ans=max(ans,px-x);
if(x>px)px=x;
}
printf("%d\n",ans);
}
return ;
}

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

//#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\n",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\n",ans);
}
return 0;
}

  

zzuli求最大值的更多相关文章

  1. HDU 1754 I Hate It 线段树单点更新求最大值

    题目链接 线段树入门题,线段树单点更新求最大值问题. #include <iostream> #include <cstdio> #include <cmath> ...

  2. HDU 2795 Billboard(区间求最大值的位置update的操作在query里做了)

    Billboard 通过这题,我知道了要活用线段树的思想,而不是拘泥于形式, 就比如这题 显然更新和查询放在一起很简单 但如果分开写 那么我觉得难度会大大增加 [题目链接]Billboard [题目类 ...

  3. c# 任意多个数,求最大值

    c#  任意多个数,求最大值 使用parms: 正在研究中,如果有好的方案,可评论,共同进步,共同提高,谢谢!

  4. 【c语言】求最大值

    一.我个人觉得求最大值比较简单的一种方法(当然同时求最大值和最小值时稍微改改也能行) #include <stdio.h> int main(void) { int f, i, max; ...

  5. POJ 3264 Balanced Lineup【线段树区间查询求最大值和最小值】

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 53703   Accepted: 25237 ...

  6. js求最大值最小值

    比较数组中数值的大小是比较常见的操作,比较大小的方法有多种,比如可以使用自带的sort()函数,代码如下: <html> <head> <meta charset=&qu ...

  7. java求最大值以及定义方法调用

    class ArrayDome { public static void main(String[] args) { int[] arr = {-12,-51,-12,-11}; int max = ...

  8. 算法笔记_096:蓝桥杯练习 算法提高 求最大值(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 问题描述 给n个有序整数对ai bi,你需要选择一些整数对 使得所有你选定的数的ai+bi的和最大.并且要求你选定的数对的ai之和非负,bi之和非负 ...

  9. elasticsearch聚合案例--分组、求最大值再求最大值的均值

    一.需求 A.B.C代表3个用户,第二列代表各自的得分,求A.B.C的最好成绩以及A.B.C最好成绩的均值 A 10 A 11 A 13 B 11 B 11 B 12 C 10 C 10 C 11 C ...

随机推荐

  1. php定界符 <<< 的作用及使用注意事项

    按照原样输出,包括换行符.特殊字符等 任何特殊字符都不需要转义,比如双引号.单引号,它会按照原样输出 像在双引号字符串中一样使用php的变量输出 定界符<<<,当需要输出大段文本时, ...

  2. Flex4 设置combobox选项不可编辑

    近日做数据的增删改查,使用的flex4的ComboBox控件---> flex4中ComboBox其实就是TextInput的叠加 flex3中ComboBox其实就是Label的叠加 开始是使 ...

  3. Qt5 FOR WINCE7, Visual Studio 2008环境的搭建

    Qt5 FOR WINCE7, Visual Studio 2008环境的搭建 Qt5发布时,试过配置Qt5 for wince的环境,原因是暂时不支持WINCE.前几天意外发现官方博客说明已经开始支 ...

  4. 下载xcode 6 beta.dmg

    太大.无法上传到csdn上,百度网盘下载 http://pan.baidu.com/s/1eQtcV7c

  5. Value Categories

    Value categories Three primary categories primary categories mixed special Each C++ expression (an o ...

  6. jmeter 压力测试 参数

    Aggregate Report 是 JMeter 常用的一个 Listener,中文被翻译为“聚合报告”.今天再次有同行问到这个报告中的各项数据表示什么意思,顺便在这里公布一下,以备大家查阅. 如果 ...

  7. LinkList Operation

    链表典型数据结构: #define ElemType int typedef struct LinkNode{ ElemType value; struct LinkNode* next; }; 相比 ...

  8. jQuery(二)

    table 全选.反选.清除 <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...

  9. Linux系统目录(转载)

    /         (这就是著名的根)├── bin         (你在终端运行的大多数程序,比如cp.mv...)├── boot         (内核放在这里,这个目录也经常被作为某个独立分 ...

  10. Google浏览器的缓存文件过大(mega网站导致的)

    到选项里清空所有内容也没有用. 后来手动找了一下,原来在这里,存了在这里存了整整10G的缓存: C:\Users\my\AppData\Local\Google\Chrome\User Data\Pr ...