Hdu 4193 Non-negative Partial Sums (Data Structure _ monotonous Queue)

Source: Internet
Author: User

Given a circular sequence with a length of n, starting from n different positions, I would like to ask how many locations make the situation true: the sum of all prefixes is equal to 0 (n <= 1 million ).

Solution: In the afternoon, I made a swerc G question. I thought that the line segment tree had been knocked for more than 10 minutes. Then, I knocked on the first question Polya and got a FB.
The reason why a line segment tree does T is that its complexity is O (nlogn). In this way, when n is equal to 1 million, it may take about 8 million operations, but with a monotonous queue, As long as O (n ), the operand is about 2 million. The result line segment tree is T, and the monotonous queue is over.
The reason why I think of the Line Segment tree to do this is because, following the topic's thinking, I simulated the n-cycle operations of the question, and each time I updated the minimum value of a segment query, I really want to write it very well. In this way, the topic will be bound to the Train of Thought. In many cases, it is often because the train of thought is bound, so there should be no issues.
This requires that all prefixes and values are equal to or greater than 0. If the previous one is rolled to the end, it seems to be updated, but it is not updated at all, we only need to add sum [I] to 0 to represent the sum of the first I number. This value is called Minval. What we need to do is to maintain this Minval, calculate the minimum sum [I] Of all prefixes from I bit to I + n-1 bit, and mark it as Minsum. If Minsum> = Minval, add 1 to the legal answer. this Minval is actually the sum of prefix I (1 <= I <= n ).
Then the minimum value of the fixed interval length is obtained. This is the most suitable for monotonous queues. After thinking about this question, the line segment tree should be the first choice for Interval query. However, when the restrictions and conditions increase, you should select a simpler and more efficient data structure to complete the operation. For this question, the restriction condition is that the range is fixed, and the array does not change dynamically, but Minval is changed. Therefore, it is better to use monotonous queues.
Based on the above analysis, the rest is coding. Recently, it seems to be called coders. I have been playing the game by myself in every game, and I often do not have to think about it many times.

Test data:
4
3-1-1 1
3
-1 1 1
3
1 2 3

C producer code:
[Cpp]
# Include <stdio. h>
# Include <string. h>
# Deprecision MAX 2100000
 
 
Struct node {
 
Int sum, id;
} Qu [MAX];
Int n, head, tail, ans;
Int sum [MAX], arr [MAX];
 
 
Int main ()
{
Int I, j, k;
 
 
While (scanf ("% d", & n )! = EOF ){
 
If (n = 0) break;
Ans = head = tail = 0;
For (I = 1; I <= n; ++ I ){

Scanf ("% d", & arr [I]);
Sum [I] = sum [I-1] + arr [I];
}
For (I = 1; I <= n; ++ I)
Sum [I + n] = sum [I + n-1] + arr [I];
 
 
For (I = 1; I <= 2 * n; ++ I ){
 
While (tail Head --;
Qu [head]. sum = sum [I], qu [head]. id = I, head ++;
If (I> n & qu [tail]. sum> = sum [I-n]) ans ++;
While (tail }
 
 
Printf ("% d \ n", ans );
}
}

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.