UVA之11078 - Open Credit System

简介:

【题目】

Problem E
Open Credit System
Input: 
Standard Input

Output: Standard Output

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                             Output for Sample Input

3
2
100
20
4
4
3
2
1
4
1
2
3
4
 

80
3
-1


Problemsetter: Mohammad Sajjad Hossain

Special Thanks: Shahriar Manzoor

 

【分析】

【思路一】

最简单的一种就是二重循环。但是Time limit exceeded。         这种算法的时间复杂度为O(n^2)。在n 很大规模的时候就无能为力了。

【思路二】

对于每个固定的j,我们应该选择的是小于j且Ai最大的i,而和Aj的具体数值无关。这样,我们从小到大枚举j,顺便维护Ai的最大值即可。时间复杂度降为O(n)

【代码】

【代码1】

/*********************************
*   日期:2014-5-1
*   作者:SJF0115
*   题号: 11078 - Open Credit System
*   地址:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=22&page=show_problem&problem=2019
*   来源:UVA
*   结果:Time limit exceeded。
*   总结:
**********************************/
#include <iostream>
#include <stdio.h>
using namespace std;

int A[100001];

int main(){
    int T,i,j,n;
    scanf("%d",&T);
    //T组测试数据
    while(T--){
        scanf("%d",&n);
        //输入数据
        for(i = 0;i < n;i++){
            scanf("%d",&A[i]);
        }
        //初始化 不要初始化为0因为最终答案可能小于0
        int max = A[0] - A[1];
        //计算最大的A[i]-A[j]
        for(i = 0;i < n;i++){
            for(j = i+1;j < n;j++){
                if(max < A[i] - A[j]){
                    max = A[i] - A[j];
                }//if
            }//for
        }//for
        printf("%d\n",max);
    }
    return 0;
}

【代码2】

/*********************************
*   日期:2014-5-1
*   作者:SJF0115
*   题号: 11078 - Open Credit System
*   地址:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=22&page=show_problem&problem=2019
*   来源:UVA
*   结果:Accepted
*   总结:
**********************************/
#include <iostream>
#include <stdio.h>
using namespace std;

int A[100001];

int main(){
    int T,i,j,n;
    scanf("%d",&T);
    //T组测试数据
    while(T--){
        scanf("%d",&n);
        //输入数据
        for(i = 0;i < n;i++){
            scanf("%d",&A[i]);
        }
        //初始化 不要初始化为0因为最终答案可能小于0
        int max = A[0] - A[1];
        //maxAi动态维护A[0],A[1]....A[j-1]的最大值
        int maxAi = A[0];
        //计算最大的A[i]-A[j]
        for(i = 1;i < n;i++){
            //更新最大A[i]-A[j]
            if(max < maxAi - A[i]){
                max = maxAi - A[i];
            }
            //更新最大元素
            if(maxAi < A[i]){
                maxAi = A[i];
            }
        }//for
        printf("%d\n",max);
    }
    return 0;
}


目录
相关文章
|
弹性计算 Go 数据安全/隐私保护
基于已有阿里云服务器搭建雾锁王国
如果你已经在阿里云购买了云服务器,觉得服务器闲置有点浪费的话,你可以参照本文步骤,使用计算巢迁移模板快速部署雾锁王国。
1238 1
|
自然语言处理 API 索引
ElasticSearch实战教程PostMan版(超级详细版)
ElasticSearch实战教程PostMan版(超级详细版)
ElasticSearch实战教程PostMan版(超级详细版)
|
10月前
|
缓存 编译器 C++
第十五问:volatile是什么?有什么用?
本文深入探讨了C/C++中的`volatile`关键字,解释了其防止编译器不当优化、保证多线程间可见性和确保硬件状态正确读写的作用。同时,文章也指出了使用`volatile`可能带来的性能影响,并强调了它在多线程同步中的局限性。通过具体示例,帮助读者更好地理解和应用这一强大工具。
730 0
|
安全 Java 容器
Java一分钟之-并发编程:并发容器(ConcurrentHashMap, CopyOnWriteArrayList)
【5月更文挑战第18天】本文探讨了Java并发编程中的`ConcurrentHashMap`和`CopyOnWriteArrayList`,两者为多线程数据共享提供高效、线程安全的解决方案。`ConcurrentHashMap`采用分段锁策略,而`CopyOnWriteArrayList`适合读多写少的场景。注意,`ConcurrentHashMap`的`forEach`需避免手动同步,且并发修改时可能导致`ConcurrentModificationException`。`CopyOnWriteArrayList`在写操作时会复制数组。理解和正确使用这些特性是优化并发性能的关键。
193 1
|
存储 监控 算法
XXL-JOB内部机制大揭秘:让任务调度飞起来
【8月更文挑战第14天】在大数据时代,高效的任务调度系统是支撑业务稳定运行与快速迭代的基石。XXL-JOB,作为一款轻量级、分布式任务调度平台,凭借其灵活的配置、强大的扩展性和高可用特性,在众多任务调度框架中脱颖而出。今天,我们就来深入揭秘XXL-JOB的内部机制,看看它是如何让任务调度“飞起来”的。
712 0
|
存储 安全 算法
JAVA泛型:编译时的“守护神”,你值得拥有!
【6月更文挑战第28天】Java泛型,自Java 5起,是编程的得力助手。它引入类型参数,提升代码安全性和重用性。通过泛型,编译时即检查类型,减少运行时错误,简化类型转换,如示例中泛型ArrayList确保只存String。泛型,是代码的忠诚卫士,助力编写更健壮、易读的Java代码。
72 0
|
JSON 缓存 Java
修改Request与Response中的内容
修改Request与Response中的内容
226 0
|
SQL 存储 分布式计算
Hive企业级性能优化
Hive作为大数据平台举足轻重的框架,以其稳定性和简单易用性也成为当前构建企业级数据仓库时使用最多的框架之一。
417 0
Hive企业级性能优化
|
vr&ar Python
Python 用ARIMA、GARCH模型预测分析股票市场收益率时间序列2
Python 用ARIMA、GARCH模型预测分析股票市场收益率时间序列
|
开发者
Font-Awesome如何引入矢量字体图标
在开发网页的过程中,我们会经常需要用到一些小图标来进行形象地说明解释或者装饰网页,但是传统的图片引用方式引入的的是图像图标,不易修改,而矢量字体图标则能很好地解决这一问题,因为矢量字体图标的本质是字体,可以使用“<style>”标签对其属性进行修改,非常方便,已经被广泛应用于网页开发中!本文主要介绍font-awesome-4.7.0的引入和使用
598 0
Font-Awesome如何引入矢量字体图标