Open Credit System

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

Output for Sample Input

80
3
-1

题目大意:开放式学分制。给定一个长度为n的整数序列A0,A1,...,An-1,找出两个整数Ai和Aj(i<j),使得Ai-A尽量大。

分析:使用二重循环会超时。对于每个固定的j,我们应该选择的是小于j且Ai最大的i,而和Aj的具体数值无关。这样,我们从小到大枚举j,顺便维护Ai的最大值即可。

代码如下:

 #include<cstdio>
#include<algorithm>
using namespace std;
int A[], n;
int main() {
int T;
scanf("%d", &T);
while(T--) {
scanf("%d", &n);
for(int i = ; i < n; i++) scanf("%d", &A[i]);
int ans = A[]-A[];
int MaxAi = A[]; // MaxAi动态维护A[0],A[1],…,A[j-1]的最大值
for(int j = ; j < n; j++) { // j从1而不是0开始枚举,因为j=0时,不存在i
ans = max(ans, MaxAi-A[j]);
MaxAi = max(A[j], MaxAi); //MaxAi晚于ans更新。想一想,为什么
}
printf("%d\n", ans);
}
return ;
}

UVA 11078 Open Credit System(扫描 维护最大值)的更多相关文章

  1. UVa 11549 Open Credit System

    题意:给出n个数,找出两个整数a[i],a[j](i < j),使得a[i] - a[j]尽量大 从小到大枚举j,在这个过程中维护a[i]的最大值 maxai晚于ans更新, 可以看这个例子 1 ...

  2. Open Credit System(UVA11078)

    11078 - Open Credit System Time limit: 3.000 seconds Problem E Open Credit System Input: Standard In ...

  3. UVA Open Credit System Uva 11078

    题目大意:给长度N的A1.....An 求(Ai-Aj)MAX 枚举n^2 其实动态维护最大值就好了 #include<iostream> #include<cstdio> u ...

  4. 【UVA 11078】BUPT 2015 newbie practice #2 div2-A -Open Credit System

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=102419#problem/A In an open credit system, the ...

  5. uva11078 - Open Credit System(动态维护关键值)

    这道题使用暴力解法O(n*n)会超时,那么用动态维护最大值可以优化到O(n).这种思想非常实用. #include<iostream> #include<cstdio> #in ...

  6. Open Credit System

    Open Credit SystemInput: Standard Input Output: Standard Output In an open credit system, the studen ...

  7. Uva----------(11078)Open Credit System

    Open Credit System Input:Standard Input Output: Standard Output In an open credit system, the studen ...

  8. 线段树(维护最大值):HDU Billboard

    Billboard Time Limit: 20000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  9. HDU.5692 Snacks ( DFS序 线段树维护最大值 )

    HDU.5692 Snacks ( DFS序 线段树维护最大值 ) 题意分析 给出一颗树,节点标号为0-n,每个节点有一定权值,并且规定0号为根节点.有两种操作:操作一为询问,给出一个节点x,求从0号 ...

随机推荐

  1. Android强制设置横屏或竖屏

    全屏 在Activity的onCreate方法中的setContentView(myview)调用之前添加下面代码 requestWindowFeature(Window.FEATURE_NO_TIT ...

  2. [原]ubuntu下制作ubuntu源

    ubuntu下可以用debmirror来下载ubuntu的所有源: 配置ubuntu12.04_mirror.sh ########################################## ...

  3. CF_402B 想法题

    题目链接:http://codeforces.com/problemset/problem/402/B /**算法分析: 题意太大意,positive没注意这个问题 考察等差数列,由An=A1+(n- ...

  4. Android文字的阴影效果

    <!-- android:shadowDx 阴影的水平偏移量 即往右移的距离 --> <!-- android:shadowDy 阴影的垂直偏移量 即往下移的距离--> < ...

  5. ip_conntrack缓存neighbour

    在我的ip_conntrack版本中,它目前已经可以缓存路由,filter规则等,还可以平滑生效最新配置的NAT,它越来越像真正的SDN了,唯一有待完善的就是将5元组的tuple进化成N元组的tupl ...

  6. golang中赋值string到array

    要把一个string赋值给一个array,哥哥遇到一个纠结的困难,研究一番,发现主要原因是array和slice在golang里不是一个东西,本文提供两种解决方案. 在网络编程中network pac ...

  7. iOS 开发 NSLog调试小技巧

    NSLog其实是一个非常损耗性能的东西,当你在开发了很长时间的一个项目中,想必为了方便调试,里面会有很多的NSLog输出,为了优化性能,同时又方便调试,可以在pch中定义一个宏,既可以替换原有的NSL ...

  8. Mysql常见问题及优化

    本文将就以下三个问题进行展开: 1.库表设计 2.慢 SQL 问题 3.误操作.程序 bug 时怎么办 一.库表设计 1.1 引擎选择 在 mysql 5.1 中,引入了新的插件式存储引擎体系结构,允 ...

  9. C#- Winform最小化到托盘

    实现前先拉一个notifyIcon控件,在Icon属性中加入一个ICON小图标,然后具体的代码实现如下: using System; using System.Collections.Generic; ...

  10. linux中配置maven环境

    一 .  下载maven http://maven.apache.org/download.cgi 二.   将maven解压到你的工具文件夹下 如我是解压到:  /home/urc/tool下 三. ...