The problem of NYOJ-745 ants (ii)

Source: Internet
Author: User

This question and ask the field and the requirements of almost, is that is a chain, this is a ring, about so ring, just start in accordance with the chain that way to push down the state transfer equation, but did not write out, later to see the problem, only to see the original or converted to ordinary single chain to do, many problems are not converted into simple to do Still have to think more ah, ran into the problem don't want to move the brain, really can not learn anything AH

Ideas: A total of 2, the first is the single-chain maximum (that is, end-to-end), this kind of ordinary DP can be found out, there is a single chain of minimum value, with the sum minus this is in addition to the first case, remember, in addition to the first case can be found outside the maximum value, just at the beginning I was Obviously the second kind I can find the counter-example, why, and later found there is a first situation, so finally to compare among them who larger, choose a bigger one

The following illustration helps to understand the maximum value of four cases.

    First Kind The second Kind Third Kind Fourth Type                           

The first three cases can be a single-stranded way to find out, because the selection of the end-to-end, is not the largest, so the first three kinds of cases is the primary, the last one is the root of the problem, the first set of data, 3, 1, 2, constitute the largest at both ends, so this time to use the second method The maximum value of the first is not the largest, but the minimum value must be the smallest, so, the total minus the smallest, is the largest, so that the problem becomes simple

The code is as follows:

1#include <iostream>2#include <cstdio>3#include <algorithm>4#include <cstring>5 using namespacestd;6 Const Long LongINF =999999999999999;7 Const intN =50000+Ten;8 intA[n];9 Long LongMin_num, Max_num;//Save the maximum and minimum values for a fieldTen intMain () One { A     intN; -      while(~SCANF ("%d", &N)) -     { theMin_num = INF;//Initialize -Max_num =-INF; -         Long LongT1 =0, t2 =0; -         Long Longsum =0; +          for(inti =0; I < n; i++) -         { +scanf"%lld", &a[i]); ASum + =A[i]; at             if(T1 >0) -T1 + =A[i]; -             Else -T1 =A[i]; -             if(T1 >max_num) -Max_num =T1; in             if(T2 <0) -T2 + =A[i]; to             Else +t2 =A[i]; -             if(T2 <min_num) theMin_num =T2; *         } $         if(Max_num < sum-min_num)Panax NotoginsengMax_num = Sum-Min_num; -printf"%lld\n", max_num); the     } +     return 0; A}

The problem of NYOJ-745 ants (ii)

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.