At first, see this question, think is the largest continuous sub-sequence and the problem, write out the code, submitted, WR, found some test data, the results found that the algorithm does not solve the sequence of the solution, just to meet a part of the sequence.
Baidu a bit, know to use the monotone queue to solve.
A monotone queue, that is, the queue must be monotonically decreasing or incrementing. This problem uses a monotonically increasing queue.
The monotone queue uses a two-way queue, and the tail team head can delete the element, and only the element is inserted from the end of the team.
For example, the length of the longest increment sequence of a series {1, 2, 5, 3, 4, 6} is solved.
First, 1 are queued, with 1 in the queue. Next 2:1, 2, queue 1, 2
Next 5:2, 5, queue 1, 2, 5.
Next 3:5 small, delete 5,3 and then compared with the new team tail, 3:2 large, will be 3 queue, queues for 1, 2, 3.
And so on: The last queue is 1, 2, 3, 4, 6.
and Hangzhou Electric 3415 This problem, is to find a ring series of the largest continuous sequence of and, sequence length is not greater than K. The first and last items of a sequence are adjacent.
A lot of people at the beginning of the idea is DP, but DP does not solve, this I have already mentioned.
The length of the input sequence is n, and the length of the maximum sequence is K.
Because the sequence is annular, the array is enlarged one time, a[i] = A[i + N] (1 <= i <= N), which solves the ring problem.
The title is the sum of the maximum sequential sequence, so the sum of the previous i is used with an array of sums (1 <= i <= 2 * N), sum[i] = Sum[i-1] + a[i].
The maximum continuous subsequence and (ans) of this problem solution becomes the solution in the sum array in the case of a length not exceeding k, ans = sum "j"-Sum "i". Sum "I" is the relatively smallest sum, sum "j" is the relative maximum, j-i + 1 <= K (because of the length limit). This keeps updating ans. The queue here is not monotonically incrementing, it is equivalent to take each sum array of each with its front each subtraction to update ans, time complexity is very large, must time out, and using the monotone queue, you can avoid some unnecessary steps.
Here is the AC code, look at the comments plus the above should be understandable.
#include <iostream> #include <cstdio> #include <queue> #include <algorithm>using namespace std; int a[200005], sum[200005];int main () {int T, N, K, i;scanf ("%d", &t), while (t--) {scanf ("%d%d", &n, &k); int N = N Backup nsum[0] = 0; for (i = 1; I <= N; i++)//input {scanf ("%d", &a[i]), A[n + i] = a[i];sum[i] = sum[i-1] + a[i]; The sequence of the preceding I and (1 <= i <= N)}for (i = n + 1; I <= 2 * N; i++) sum[i] = Sum[i-1] + a[i]; The sequence of the first I term and (n + 1 <= i <= 2 * N) deque<int> que; Defines a bidirectional queue int ans = -10000000;int start, end; n = n + K-1; The sequence length does not exceed K, only the preceding N + K-1 entries are sufficient, que.clear (); for (i = 1; I <= N; i++)//The monotone queue is persisted as an ascending sequence. {while (!que.empty () && sum[i-1] < Sum[que.back ()])//insert a number smaller than the tail of the team, delete the tail, and then compare until it is larger than the tail of the team. Que.pop_back (); The value of the deleted element is less than the value of the inserted element. Because to sequence and Max//The following ans to be constantly updated,The elements of the team head are relatively minimal and the solution is optimal. while (!que.empty () && I-que.front () > K)//Queue length greater than K, delete the team header. Que.pop_front (); Keep the length not greater than K, constantly updated ansque.push_back (i-1); Join Team Tail if (Sum[i]-Sum[que.front ()] > ans)//judgment sequence and whether it is greater than ans, constantly updated Ans{ans = Sum[i]-Sum[que.front ()];st Art = Que.front () + 1;end = i;}} if (end > N)//end position exceeds n, subtract n. End-= n;printf ("%d%d%d\n", ans, start, end);} return 0;}
For the monotonous queue is also a beginner, if there are errors, welcome to correct, learn from each other. ~~
Hangzhou Electric Acm3415--max Sum of Max-k-sub-sequence