M-Scan method

Source: Internet
Author: User

In an open credits system, the students can choose any course they like, but there is a problem. Some of the students is more senior than other students. The professor of such a course have found quite a number of such students who came from senior classes (as if they came to Attend the pre requisite course after passing a advanced course). But he wants to does justice to the new students. So, he's 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, a senior student gets more than any junior student.

For example, if a senior student gets the and a junior student gets, then this amount is 10. Be careful that we don ' t want the absolute value. Help the professor into figure out a solution. Input input consists of a number of test cases T (less than 20).

Each case starts with a integer n which is the number of students in the course. This value can is 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 has absolute values less than 150000. If I < J, then i ' th student are senior to the J ' th student.

Output

For each test case, the output of 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

Sample Output

80

3

-1

Problem Solving Ideas:

This topic means that the n number consists of a sequence of two number of the largest difference, PS: This sequence can not change the position of the original number, that is, the first number from the front start with each of the following number of differences, but it is possible to time out, so we each find a number, with the largest number before the difference, and before the maximum difference Save the maximum difference, and then compare it with the largest number before it, save the maximum number, cycle down, as long as the loop n-1 times.

Program code:

#include <cstdio>using namespacestd;intMaxintAintb) {    returnA>b?a:b;}inta[200005];intMain () {intT,n; scanf ("%d",&t);  while(t--) {scanf ("%d",&N);  for(intI=0; i<n;i++) scanf ("%d",&A[i]); intans=-1; intf=a[0];  for(intI=1; i<n;i++) {ans=max (ans,f-A[i]); F=Max (f,a[i]); } printf ("%d\n", ans); }    return 0;}
View Code

M-Scan method

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.