前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Uva----------(11078)Open Credit System

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

作者头像
Gxjun
发布2018-03-26 10:26:16
6460
发布2018-03-26 10:26:16
举报
文章被收录于专栏:mlml

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 )

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

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

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

代码如下:

代码语言:javascript
复制
 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\n",cnt);
24   }
25   return 0;
26 }

 进一步优化之后的

代码:

代码语言:javascript
复制
 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\n",ans);
23   }
24   return 0;
25 }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2014-08-17 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档