In an open credit system, the students can choose any course they like, but there is a problem. Some of the students are more senior than other students. The professor of such a course has found quite a number of such students who came from senior classes (as if they came to attend the pre requisite course after passing an advanced course). But he wants to do justice to the new students. So, he is going to take a placement test (basically an IQ test) to assess the level of difference among the students. He wants to know the maximum amount of score that a senior student gets more than any junior student.

For example, if a senior student gets 80 and a junior student gets 70, then this amount is 10. Be careful that we don’t want the absolute value. Help the professor to figure out a solution. Input Input consists of a number of test cases T (less than 20).

Each case starts with an integer n which is the number of students in the course. This value can be as large as 100,000 and as low as 2. Next n lines contain n integers where the i’th integer is the score of the i’th student. All these integers have absolute values less than 150000. If i < j, then i’th student is senior to the j’th student.

Output

For each test case, output the desired number in a new line. Follow the format shown in

sample

input-output section.

Sample Input

3

2

100

20

4

4

3

2

1

4

1

2

3

4

Sample Output

80

3

-1

解题思路:

这个题目的意思是n个数组成一个序列,求两数最大的差,PS:这个序列不能改变原数的位置,即要从前面第一个数开始与后面每一个数作差,但是是这样时间可能会超时,所以我们每找一个数,与之前的最大数作差后,再与之前最大差相比,存下最大差;然后再与它之前的那个最大数相比,存下最大数,循环下去,只要循环n-1次即可。

程序代码:

#include <cstdio>
using namespace std;
int max(int a,int b)
{
return a>b?a:b;
}
int a[];
int main()
{
int t,n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=;i<n;i++)
scanf("%d",&a[i]);
int ans=-;
int f=a[];
for(int i=;i<n;i++)
{
ans=max(ans,f-a[i]);
f=max(f,a[i]);
}
printf("%d\n",ans);
}
return ;
}

高效算法——M 扫描法的更多相关文章

  1. 深入N皇后问题的两个最高效算法的详解 分类: C/C++ 2014-11-08 17:22 117人阅读 评论(0) 收藏

    N皇后问题是一个经典的问题,在一个N*N的棋盘上放置N个皇后,每行一个并使其不能互相攻击(同一行.同一列.同一斜线上的皇后都会自动攻击). 一. 求解N皇后问题是算法中回溯法应用的一个经典案例 回溯算 ...

  2. CVPR2020论文介绍: 3D 目标检测高效算法

    CVPR2020论文介绍: 3D 目标检测高效算法 CVPR 2020: Structure Aware Single-Stage 3D Object Detection from Point Clo ...

  3. 集训第四周(高效算法设计)M题 (扫描法)

    原题:UVA11078 题意:给你一个数组,设a[],求一个m=a[i]-a[j],m越大越好,而且i必须小于j 怎么求?排序?要求i小于j呢.枚举?只能说超时无上限.所以遍历一遍数组,设第一个被减数 ...

  4. manacher算法——回文串计算的高效算法

    manacher算法的由来不再赘述,自行百度QWQ... 进入正题,manacher算法是一个高效的计算回文串的算法,回文串如果不知道可以给出一个例子:" noon ",这样应该就 ...

  5. 凸包(Convex Hull)构造算法——Graham扫描法

    凸包(Convex Hull) 在图形学中,凸包是一个非常重要的概念.简明的说,在平面中给出N个点,找出一个由其中某些点作为顶点组成的凸多边形,恰好能围住所有的N个点. 这十分像是在一块木板上钉了N个 ...

  6. UVa 1210 (高效算法设计) Sum of Consecutive Prime Numbers

    题意: 给出n,求把n写成若干个连续素数之和的方案数. 分析: 这道题非常类似大白书P48的例21,上面详细讲了如何从一个O(n3)的算法优化到O(n2)再到O(nlogn),最后到O(n)的神一般的 ...

  7. 双有序队列算法——处理哈夫曼K叉树的高效算法

    算法介绍: 哈夫曼树的思路及实现众所周知,大部分是用堆来维护和实现,这种思路比较清晰,在K比较小的时候处理较快(具体例子接下来再说),而且编程复杂度不是很高,利于应用.但是,其所用的数据结构是树,是在 ...

  8. 集训第四周(高效算法设计)O题 (构造题)

    A permutation on the integers from 1 to n is, simply put, a particular rearrangement of these intege ...

  9. 集训第四周(高效算法设计)A题 Ultra-QuickSort

    原题poj 2299:http://poj.org/problem?id=2299 题意,给你一个数组,去统计它们的逆序数,由于题目中说道数组最长可达五十万,那么O(n^2)的排序算法就不要再想了,归 ...

随机推荐

  1. 响应式WEB设计

    近期在学习有关响应式设计的内容,对此做了些整理,图片来源于网络,附上自己做的简单demo,没有js,只用CSS做了简单的搭建http://y.zhso.net/. 1.为什么需要响应式web设计 出于 ...

  2. Adb工具常用操作-转(二)

    一. PC与模拟器或真机交换文件(adb pull和adb push) 在开发阶段或其他原因,经常需要将PC上的文件复制到模拟器或真机上,或将模拟机和真机上的文件复制到PC上.使用adb pull和a ...

  3. ui线程和后台线程异步

    private void Button_Click(object sender, RoutedEventArgs e) { CreateElementOnSeperateThread(() => ...

  4. Log4j配置的经典总结,打印日志文件,日志存库

        一.介绍 Log4j是Apache的一个开放源代码项目,通过使用Log4j,我们可以控制 日志信息输送的目的地是控制台.文件.GUI组件.甚至是套接口服务 器.NT的事件记录器.UNIX Sy ...

  5. JSONModel的基本使用

    JSONModel 是一个库,它能智能并且快速的创建出数据 model,你可以在你的 iOS 项目或者 OSX 项目上使用它. 使用前准备 添加 JSONModel 到你的工程中 1.需要的环境: A ...

  6. (JAVA)从零开始之--打印流PrintStream记录日志文件

    这里的记录日志是利用打印流来实现的. 文本信息中的内容为String类型.而像文件中写入数据,我们经常用到的还有文件输出流对象FileOutputStream. File file = new Fil ...

  7. corejava_chap02

    //单行注释  --不能用在一行代码的中间/**/多行注释 --任何地方/** */文档注释  文档注释用在:package.class.member variables.member method. ...

  8. 【ZOJ2112】【整体二分+树状数组】带修改区间第k大

    The Company Dynamic Rankings has developed a new kind of computer that is no longer satisfied with t ...

  9. iPhone、iPad默认按钮样式问题

    iPhone.iPad默认按钮样式问题 解决方法给按钮元素添加一个-webkit-appearance: none;具体代码 input[type="button"], input ...

  10. android中使用intent来实现Activity带数据跳转

    大家都知道startActivity()是用来切换跳转Activity的.如果想要在另个Activity中出书数据的话.只需要在源activity中使用intent.putExtra()方法传出数据. ...