Hdu Non-negative Partial Sums (monotonous Queue)

Source: Internet
Author: User
Non-negative Partial SumsTime Limit: 6000/3000 ms (Java/Other) Memory Limit: 65536/32768 K (Java/Other) Total Submission (s): 6 Accepted Submission (s): 1 Font: times New Roman | Verdana | GeorgiaFont Size: Regular → Problem DescriptionYou are given a sequence of n numbers a0 ,..., an-1. A cyclic shift by k positions (0 <= k <= N-1) results in the following sequence: akak + 1 ,..., an-1, a0, a1 ,..., ak-1. how
Outcome of the n cyclic shifts satisfy the condition that the sum of the fi rst I numbers is greaterthan or equal to zero for all I with 1 <= I <= n? InputEach test case consists of two lines. the fi rst contains the number n (1 <= n <= 106), the number of integers in the sequence. the second contains n integers a0 ,..., an-1 (-1000 <= ai <= 1000) representing the sequence
Numbers. The input will finish with a line containing 0. OutputFor each test case, print one line with the number of cyclic shifts of the given sequence which satisfy the condition stated above. Sample InputSample Output
320
The first monotonous queue question made by SourceSWERC 2011 is to record the implementation of the monotonous queue: the dual-end queue can be used to compare the elements at the end of the queue whenever an element is inserted, if <= (minimum first element) is the element at the end of the team, the element at the end of the team is discarded, and then the modified element is inserted at the end of the team. Whenever the first element is used, first, judge whether the subscript of the first element of the team is "obsolete". If it is "obsolete", it is discarded and then used. This is a straightforward implementation of monotonous queues. The significance of doing so is that when you encounter the problem of finding the maximum or minimum value in the dynamic interval, the simple algorithm can be compared from start to end each time. However, this is a waste, because every time we calculate the minimum value of the previous interval, we have actually compared the n-1 elements of the next interval, therefore, in order to take advantage of these work results, we need to design a clever way. Therefore, we have a monotonous data structure such as queue. Imagine what problems should we solve if we design a structure on our own? 1. the uncertainty of the most valuable position in the previous range (you can record the most worthwhile position each time, and then discuss the most worthwhile position in the next range in minutes, but I personally feel this is very troublesome and I have not tried it, in addition, the implementation of monotonous queues is very elegant, so there is no need to consider a better way), resulting in a poor use of the expected work results. 2. Constantly updating data requires you to record which data is out of date and which ones are not out of date, so that you can easily determine which one of the values obtained from the previous interval belongs. Let's look at how monotonous queues solve these problems: 1. each time a <= element is encountered, it is discarded because these elements are definitely not the most value of this interval, so they have no effect on finding the maximum value of the next interval. 2. The new elements are compared from the end of the team, ensuring that the elements from the end of the queue to the first of the team must be from the latest to the oldest. This solves the uncertainty of Problem 1's most valuable position. 3. Each time you use the first element of the team, you must first determine whether the team is out of date. If it is out of date, it will be discarded. This solves problem 2. In general, the idea of monotonous queue is to ensure the New and Old Order of elements by inserting at the end of the team. during insertion, only elements that may affect the next section are saved. When using the first element of the team, first determine whether the queue is outdated, delete the element that has no effect on the value of the next segment. Let's talk about some valuable aspects of this question: 1. sum (x ~ Y) = sum (0 ~ Y)-sum (0 ~ X), so that as long as the sum array is pre-processed, the sum of any segment range can be easily obtained, and a large number of duplicates are not required each time from start to end. 2. The processing method of the cyclic sequence is generally to directly copy the original sequence and expand it to the end by a factor. 3. the problem is converted to the sum () Minimum value in the interval. In fact, it captures the necessary and sufficient condition of the problem: if the sum () Minimum value in the interval can be true, the interval must be satisfied. If it cannot be true, this cannot be met.
# Include <stdio. h> # include <string. h ># include <deque> using namespace std; struct Node {int a; int I;} sum [2000005]; deque <struct Node> d; int a [2000005]; int ans; int n; void Insert (struct Node a) {while (! D. empty () & d. back (). a> = a. a) {d. pop_back ();} d. push_back (a); while (! D. empty () &. i-d. front (). i> = n) {// note that here is> =, rather than> d. pop_front () ;}} int main () {int I; for (I = 0; I <2000005; I ++) {sum [I]. I = I;} while (scanf ("% d", & n) {ans = 0; while (! D. empty () {d. pop_back () ;}for (I = 0; I <n; I ++) {scanf ("% d", & a [I]); a [I + n] = a [I];} sum [0]. a = a [0]; for (I = 1; I <2 * n-1; I ++) {sum [I]. a = sum [I-1]. a + a [I] ;}for (I = 0; I <n-1; I ++) {Insert (sum [I]) ;}for (I = n-1; I <2 * n-1; I ++) {Insert (sum [I]); if (d. front (). a-sum [I-n]. a> = 0) ans ++;} printf ("% d \ n", ans);} return 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.