[Solution report] Ural 1010 discrete function

Source: Internet
Author: User

 

Original question address: http://acm.timus.ru/problem.aspx? Space = 1 & amp; num = 1010

The topic is defined in {1, 2, 3, 4 ..... the discrete function on n}, n <= 100000, and the function value is of the long int type. Find out two points, so that all the points between the two points are connected under the two points, and find such two points, maximum slope.


After reading the questions, I naturally want to enumerate the connections between every two points. The time complexity is O (n square ).

<Competition in algorithm art and informatics> How does the time complexity of this question reach O (n ). we need to improve the enumeration method. assume that there are three points x1, x2, X3, where X1 and X3 are two matching points, and X2 is connected to X1 and X3. obviously X2 and X3 meet the conditions and X2, X3 is better than X1 and X3. therefore, the original question only needs to enumerate the slope between adjacent points, and the time complexity is reduced to O (n)


At the beginning, WA searched other people's code online and found that the AC code is the absolute value of the two-point difference. for example, for 2, 3, 1, 3, 2, and 2, the slope is 1, and 3, and 1, the slope is-2. According to my understanding, the output is 1 2, however, because the AC code compares the absolute values of the slope, the output is 2 3. later I thought about the incorrect understanding of the word inclination, but what should I translate if it is not a slope or a tilt angle ...? Is it an angle ?... I don't know. Let's go to the code ..

 

Import Java. util. extends; <br/> public class main {<br/> Public static void main (string [] ARGs) {<br/> New Main (); <br/>}< br/> public main () {<br/> Using SC = new using (system. in); <br/> while (SC. hasnext () {<br/> int T = SC. nextint (); <br/> long ar [] = new long [T]; <br/> long max = 0; <br/> int Maxi = 0; <br/> ar [0] = SC. nextlong (); <br/> for (INT I = 1; I <t; I ++) {<br/> ar [I] = SC. nextlong (); <br/> long temp = math. ABS (AR [I]-ar [I-1]); <br/> If (temp> MAX) {<br/> max = temp; <br/> Maxi = I; <br/>}< br/> system. out. println (Maxi + "" + (Maxi + 1); <br/>}< br/>} 

 

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.