• Uva----------(11078)Open Credit System


    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'thstudent.

     All these integers have absolute values less than 150000. If i < j, then i'thstudent 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 

     这道题,就是给出一系列的有序的数,比如a[0] a[1],a[2],a[3].....a[i]....a[n]. 要你求出a[i]-a[j]的最大值(注意i<j )

    而且数据规模也是十万加的.....,可想而知,朴树的算法是会超时..

    采用动态规划的思想...

        先逐渐求出区间的最大值,注意每一次扩展时,都进行一次比较...

    代码如下:

      

     1 #include<cstdio>
     2 #include<cstring>
     3 const int maxn=100000;
     4 int aa[maxn];
     5 inline int max(int a,int b){
     6     return a>b?a:b;
     7  }
     8 
     9 int main(){
    10   int n,m;
    11   //freopen("test.in","r",stdin);
    12   scanf("%d",&n);
    13   while(n--){
    14     scanf("%d",&m);
    15     for(int i=0;i<m;i++)
    16        scanf("%d",aa+i);
    17     int cnt=aa[0]-aa[1];
    18     int maxc=aa[0];
    19     for(int i=1;i<m;i++){
    20       cnt=max(cnt,maxc-aa[i]);
    21       maxc=max(maxc,aa[i]);
    22     }
    23     printf("%d
    ",cnt);
    24   }
    25   return 0;
    26 }

     进一步优化之后的

    代码:

     1 #include<cstdio>
     2 #include<cstring>
     3 const int maxn=100000;
     4 inline int max(int a,int b){
     5     return a>b?a:b;
     6  }
     7 int main(){
     8   int n,m;
     9   int b,c;
    10   int ans;
    11   freopen("test.in","r",stdin);
    12   scanf("%d",&n);
    13   while(n--){
    14       scanf("%d",&m);
    15       scanf("%d",&b);
    16       ans =-200000;
    17     for(int i=1;i<m;i++){
    18       scanf("%d",&c);
    19       ans=max(ans,b-c);
    20       b=max(b,c);
    21     }
    22     printf("%d
    ",ans);
    23   }
    24   return 0;
    25 }
  • 相关阅读:
    常见优化函数
    排序算法
    小米编程题
    leetcode 刷题
    beam_search 和 viterbi算法的区别
    快速排序
    vitrebi算法进行分词的案例
    python 进行视频剪辑
    keras实现MobileNet
    HMM、CTC、RNN-T训练时所有alignment的寻找方法
  • 原文地址:https://www.cnblogs.com/gongxijun/p/3917407.html
Copyright © 2020-2023  润新知