[Dynamic Route Planning]

Source: Internet
Author: User
Question Description: You don't know. goleenuoer, a target guy, can beat any fish on the river, but he is really bad at it, no matter how you hit the stone, it will only Jump twice on the river and it will be "Flushed. on this day, he again. this article is W meters wide, and every other meter will have a fish, each of which has its beautiful value. he wants to know how to obtain the largest sum of appearance values between two mackerel. if you are new to Oi, could you help him ??? The input file contains N + 1 integers. The first row is an integer n (n <= 10000 ). from the number of workers N in the second line, the I integer indicates the beautiful value of the I fish (circumference:-500 .. 500 ). when All integers are negative, 0 is output. the output file in the output format contains two lines. The starting point and point of the first row are separated by spaces. the second act is an integer that represents the sum of the aesthetic values between the obtained mackerel. sample Input 6-2 11-4 13-5-2 sample output 2 4 20

The maximum value of a region in a single-dataset column.

First, extract the elements that have passed the period from the left end of the period column (this problem does not exist). Then, start from the right end, extract all elements that are no longer greater than the elements that are to be imported into the queue. Then, import the elements before the queue. In this case, the element at the beginning is the first and foremost solution. As a result, you only need to obtain one of the most optimal solutions.

Accode:

#include <cstdio>#include <cstring>#include <cstdlib>#include <bitset>const char fi[] = "rqnoj145.in";const char fo[] = "rqnoj145.out";const int maxN = 10010;const int MAX = 0x3fffff00;const int MIN = -MAX;int q[maxN];int sum[maxN];int w[maxN];int n, f, r, ans, s, t;  void init_file()  {    freopen(fi, "r", stdin);    freopen(fo, "w", stdout);  }      void readdata()  {    scanf("%d", &n);    bool pos = false;    for (int i = 1; i < n + 1; ++i)    {      scanf("%d", w + i);      if (w[i] >= 0) pos = true;    }    if (!pos)      {printf("0"); exit(0); }    for (int i = 1; i < n + 1; ++i)      sum[i] = sum[i - 1] + w[i];  }    void work()  {    f = r = 0;    ans = 0;    for (int i = 1; i < n + 1; ++i)    {      while (f < r && sum[q[r - 1]] >= sum[i]) --r;      q[r++] = i;      if (sum[i] - sum[q[f]] > ans)      {        ans = sum[i] - sum[q[f]];        s = q[f] + 1;        t = i;      }    }    printf("%d %d\n%d", s, t, ans);  }  int main(){  init_file();  readdata();  work();  exit(0);}

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.