11078-open Credit System

Source: Internet
Author: User

11078-open Credit System

Time Limit: 3.000 seconds

Problem E
Open Credit System
Input:Standard Input

Output:Standard output

In an open credit system, the students can choose anycourse they like, but there is a problem. some of the students are more seniorthan other students. the specified sor of such a course has found quite a number ofsuch students who came from senior classes (as if they came to attend the prerequisite course after passing an advanced course ). but he wants to do justiceto the new students. so, he is going to take a placement test (basically an iqtest) to assess the level of difference among the students. he wants to knowthe maximum amount of score that a senior student gets more than any juniorstudent. 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. helpthe processing sor to figure out a solution.

Input
Input consists of a number of test cases T (less than 20 ). each casestarts with an integer n which is the number of students in thecourse. this value can be as large as 100,000 and as low as 2. next n linescontain n integers whereI'Th integer is thescore ofI'Th student. All these integers have absolutevalues less than 150000. IfI<J, ThenI'Th studentis senior toJ'Th student.

Output
For each test case, output the desired number in a new line. followthe format shown in sample input-output section.

Sampleinput outputfor sample input

3

2

100

20

4

4

3

2

1

4

1

2

3

4

 

80
3
-1

A sequence of integers A0, A1, A2, ,, and an-1 with the length of N is given to find two integers: AI and AJ (I <j) makes Ai-AJ the largest...

Idea: If a Direct Dual Loop is not desirable, because the time complexity O (N ^ 2) times out under N = 100000, so we can choose the largest AI smaller than J, each time ANS is recorded, the last ANS is the result. After optimization, the time and space complexity can be changed to O (N );

PS: http://uva.onlinejudge.org/index.php? Option = com_onlinejudge & Itemid = 8 & page = show_problem & problem = 2019

 

 

#include<cstdio>#include<algorithm>using namespace std;int num[100005];int maxnum,ans;int main(){    int T,N,i,j;    scanf("%d",&T);    while(T--)    {        scanf("%d",&N);        scanf("%d%d",&num[0],&num[1]);        maxnum=num[0]>num[1]?num[0]:num[1];        ans=num[0]-num[1];        for(i=2;i<N;i++)        {            scanf("%d",&num[i]);            ans=max(ans,maxnum-num[i]);            maxnum=max(maxnum,num[i]);        }        printf("%d\n",ans);    }    return 0;}

 

11078-open Credit System

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.