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. 把Excel数据导入到数据库

    引入命名空间 using System.IO; using System.Data; using System.Data.OleDb; 引入命名空间 首先要把Excel上传到服务器 //上传Excel ...

  2. asp.net模态窗口返回值

    个人感觉模态窗口在做网站的时候,使用到的比较少,前段时间在做项目时要实现以模态窗口传值和接收返回值, 模态窗口传值实现比较简单,但是做好后发现在Chrome浏览器中接收不到返回值,修改好Chrome浏 ...

  3. Android开发--去掉标题栏

    Android开发中为了尽可能美观,会去掉标题栏.去掉标题栏有三种方法. 一.在Activity代码里实现 在代码中实现以下方法: this.requestWindowFeature(Window.F ...

  4. Nagios新添加的hosts和services有时显示,有时不显示问题解决

    在nagios配置文件hosts.cfg和services.cfg中添加了新服务器和服务列表,重启nagios服务后刷新监控页面,新添加的服务器和服务列表有时能显示出来,有时又显示不出来. 解决方法: ...

  5. 安卓学习之ListView和GridView

    ListView 和 GridView是安卓中显示信息的两个很基本也最常用的控件.他们的用法很相似,但是他俩也是有区别的. ListView显示的数据会将他的item放在一行显示,而且根据内容给出it ...

  6. 主成份分析PCA

    Data Mining 主成分分析PCA 降维的必要性 1.多重共线性--预测变量之间相互关联.多重共线性会导致解空间的不稳定,从而可能导致结果的不连贯. 2.高维空间本身具有稀疏性.一维正态分布有6 ...

  7. SGU 155.Cartesian Tree

    时间限制:0.25s 空间限制:6M 题意: 给出n(n< 50000)个含双关键字(key,val)的节点,构造一颗树使该树,按key值是一颗二分查找树,按val值是一个小根堆. Soluti ...

  8. 【NOI2004】郁闷的出纳员

    [问题描述] OIER公司是一家大型专业化软件公司,有着数以万计的员工.作为一名出纳员,我的任务之一便是统计每位员工的工资.这本来是一份不错的工作,但是令人郁闷的是,我们的老板反复无常,经常调整员工的 ...

  9. sencha touch中用来格式化日期的字符串参数

  10. VLC命令参数(转载)

    转载自: http://blog.csdn.net/bytxl/article/details/6613449 http://www.cnblogs.com/MikeZhang/archive/201 ...