UVA 11078 Open Credit System(扫描 维护最大值)
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-Aj 尽量大。
分析:使用二重循环会超时。对于每个固定的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(扫描 维护最大值)的更多相关文章
- UVa 11549 Open Credit System
题意:给出n个数,找出两个整数a[i],a[j](i < j),使得a[i] - a[j]尽量大 从小到大枚举j,在这个过程中维护a[i]的最大值 maxai晚于ans更新, 可以看这个例子 1 ...
- Open Credit System(UVA11078)
11078 - Open Credit System Time limit: 3.000 seconds Problem E Open Credit System Input: Standard In ...
- UVA Open Credit System Uva 11078
题目大意:给长度N的A1.....An 求(Ai-Aj)MAX 枚举n^2 其实动态维护最大值就好了 #include<iostream> #include<cstdio> u ...
- 【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 ...
- uva11078 - Open Credit System(动态维护关键值)
这道题使用暴力解法O(n*n)会超时,那么用动态维护最大值可以优化到O(n).这种思想非常实用. #include<iostream> #include<cstdio> #in ...
- Open Credit System
Open Credit SystemInput: Standard Input Output: Standard Output In an open credit system, the studen ...
- Uva----------(11078)Open Credit System
Open Credit System Input:Standard Input Output: Standard Output In an open credit system, the studen ...
- 线段树(维护最大值):HDU Billboard
Billboard Time Limit: 20000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU.5692 Snacks ( DFS序 线段树维护最大值 )
HDU.5692 Snacks ( DFS序 线段树维护最大值 ) 题意分析 给出一颗树,节点标号为0-n,每个节点有一定权值,并且规定0号为根节点.有两种操作:操作一为询问,给出一个节点x,求从0号 ...
随机推荐
- java 关于extends 和implement的区别
在java中extends用于继承父类,只要父类不是声明为final或者为abstract类就可以,但是java不支持多重继承.可以使用接口实现多重继承implements,继承只能继承一个类,但im ...
- 教程-Delphi源代码--后延函数
说明: 1)TTtimer控件 TTtimer控件的实质是调用WindowsAPI定时函数SetTimer和KillTimer来实现的,并简化了对WM_TIMER消息的处理过程.通过设置OnTimer ...
- nyoj 678 最小K个数之和
最小K个数之和 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 输入n个整数,输出其中最小的K个数之和.例如输入4,5,1,1,6,2,7,3,3这9个数字,当k=4 ...
- myeclipse断点调试
(转) 作为开发者,掌握开发环境下的调试技巧十分有必要.去年就想把关于Eclipse断点调试总结下了.因为对时间的掌控程度仍需极大提高,结果拖到今年才写了此篇博文.关于java调试技术还有非常多.如J ...
- [React] React Fundamentals: Using Refs to Access Components
When you are using React components you need to be able to access specific references to individual ...
- GROUP BY,WHERE,HAVING之间的差别和使用方法
having子句与where有类似之处但也有差别,都是设定条件的语句.在查询过程中聚合语句(sum,min,max,avg,count)要比having子句优先运行.而where子句在查询过程中运行优 ...
- 从本地上传整个目录到hdfs的java程序
首先在网上找了好久没有找到从本地文件系统上传整个目录到hdfs文件系统的程序,权威指南上也没有,都是单个文件上传,所以这里自己编写了一个程序,封装成jar包执行能够复制. 先说明一下代码:须要手动输入 ...
- bash shell for循环1到100 .
前言 用bash shell写程序时,经常会用到for循环,特别是从1到100这种需求,这里记录几种shell中从1到100的循环方法 方法 类c语言 for ((i=1; i<=100; ...
- last_9t's_ramsey
cannot finish his face
- POJ3974 Palindrome
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...