Sequence
[Title] Description
of a given sequence of length n is an integer of a [0], a [1 ], a [2], ... a [n-1] and the integer S. Obtaining the sum of less than the minimum length of a continuous subsequence S. If the solution does not exist, 0 is output.
[Input format
of the first row comprises two integers n (1 <= n <= 1000000) and S (1 <= S <= 10 ^ 8).
The second row there are n integers. Each integer is the range of [1, 10 ^ 3].
[] Output format
output sum less than the minimum length of a continuous subsequence S.
Sample input] [
10 15
. 5. 1. 3. 5 10. 4. 9. 7. 8 2
[output] Sample
2
[Analysis]
This question can be regarded as a problem extended queue.
In order to explain the subject convenience, we assume that a [] array index starts from 1, which are n elements a [1], a [2 ], a [3], ......, a [n].
First we need to open an array sum [], sum [i] represents a [1] + a [2 ] + ... + a [i] and.
SUM [] array is solved easily: sum [i] = a [ i] + sum [i-1].
Then we solve a range interval [L, R] and the element is more convenient (i.e., solving a [L] + a [L + 1] + ...... + a [R], we use the following sum [L , R & lt] to represent the result), namely: sum [L, R] = sum [R] - sum [L-1] ( note that this sum [L, R] represents the element within an interval range, sum [L] and the sum [R] represents the element SUM [] array)
Then that is our focus, and we open a subscript is used to indicate a variable j, a start j = 1.
We answer that we need to use ans, ans start to set a relatively large value.
We then loop from 1 to n another variable i, then each cycle, we determine:
long sum [j, i]> = S, I update answer ans = min (ans, i- j + 1), j ++ ; until the sum [j, i] <S .
Last updated ans out what we want to answer.
We look to achieve queue, codes are as follows:
#include <bits/stdc++.h> using namespace std; #define INF (1<<29) int n, S, a, sum = 0, cnt = INF; queue<int> que; int main() { cin >> n >> S; for (int i = 0; i < n; i ++) { cin >> a; que.push(a); sum += a; while (sum >= S) { cnt = min(cnt, (int) que.size()); sum -= que.front(); que.pop(); } } cout << (cnt == INF ? 0 : cnt) << endl; return 0; }