Returns an integer sequence whose length is n and finds out all the sub-sequences (consecutive) in which the sum is m. If there is no sum of any sub-sequences equal to m, find the minimum sum greater than m. For example, if the length of a sequence of 8 is 3, 2, 5, 4, 6, 8, and 7, three subsequences for 15 are found: 1 ~ 5, 4 ~ 6, 7 ~ 8. This question is not difficult. It is generally possible to solve it through traversal, but it is difficult to get the full score if you do not pay attention to the details. The following solutions can be written: [cpp] # include <iostream> using namespace std; # define N 100001 // due to the memory limit of the personal computer, this value needs to be reduced during testing on the PC and changed to this value after submission # define M 100000001 int main () {int n, m, I, j; int a [N]; int sum, tmp, cnt; int B [N] [2]; // store the result while (cin> n> m) {for (I = 1; I <= n; I ++) {cin> a [I];} sum = M; cnt = 0; for (I = 1; I <= n; I ++) {tmp = 0; for (j = I; j <= n; j ++) {tmp + = a [j]; if (tmp> = m) {if (tmp <Sum) {cnt = 0; sum = tmp; B [cnt] [0] = I; B [cnt] [1] = j; cnt ++ ;} else if (tmp = sum) {B [cnt] [0] = I; B [cnt] [1] = j; cnt ++;} break ;}}} for (I = 0; I <cnt-1; I ++) {cout <B [I] [0] <'-' <B [I] [1] <endl ;} cout <B [cnt-1] [0] <'-' <B [cnt-1] [1];} return 0 ;} people who are familiar with C ++ may write a program skillfully. However, after submitting the program, three cases have timed out. Therefore, the basic "brute force" solution is not feasible. Of course, if you take the test, you can proceed first, and if you have time to go back and optimize it. However, all the shoes that use questions are aware that even if the question fails to pass, "Y" will not appear in front of the question list ", A lot of people may be upset (probably obsessive-compulsive disorder ). So try to continue optimization. (If you do not have the patience to look at the analysis of ideas, you can directly look at the following program !) The time complexity of the above program is O (n ^ 2). It is a bit difficult to find a solution for O (logn! If the time complexity of the program cannot be changed in essence, let's look at the optimization of the program details on the basis of O (n ^ 2. During the traversal process, the inner loop can skip some elements, such as when p ~ When the elements and values of q are exactly the same as m, the next time we start to look for them from p + 1, (p + 1 )~ The elements and values in q must be smaller than m, so you can add the elements from q in sequence. As a result, the following optimization programs can skip some sequences: [cpp] # include <iostream> using namespace std; # define N 100001 # define M 100000001 int main () {int n, m, I, j; int a [N]; int sum, tmp, cnt, jmp; int B [N] [2]; while (cin> n> m) {for (I = 1; I <= n; I ++) {cin> a [I];} sum = M; cnt = 0; jmp = 0; for (I = 1; I <= n; I ++) {tmp = 0; for (j = I + jmp; j <= n; j ++) {tmp + = a [j]; if (tmp> = m) {if (tmp <sum) {cnt = 0; su M = tmp; B [cnt] [0] = I; B [cnt] [1] = j; cnt ++;} else if (tmp = sum) {B [cnt] [0] = I; B [cnt] [1] = j; cnt ++;} if (tmp = m & j-I> 1) {jmp = j-I-1 ;}else {jmp = 0 ;}break ;}} if (tmp = m & j = n) break ;} for (I = 0; I <cnt-1; I ++) {cout <B [I] [0] <'-' <B [I] [1] <endl ;} cout <B [cnt-1] [0] <'-' <B [cnt-1] [1];} return 0;} but found after submission, there is still a case that times out ...... I really don't know which steps can be omitted. Remember that the input and output of C language is more efficient than that of C ++. Replace cin, cout with scanf and printf, and the result is really successful! The time limit for this question is exactly the same. The test case is designed to be "high "! Finally, the AC program is as follows: [cpp] # include <stdio. h> # define N 100001 # define M 100000001 int main () {int n, m, I, j; int a [N]; int sum, tmp, cnt, jmp; int B [N] [2]; while (scanf ("% d", & n, & m )! = EOF) {for (I = 1; I <= n; I ++) {scanf ("% d", & a [I]);} sum = M; cnt = 0; jmp = 0; for (I = 1; I <= n; I ++) {tmp = 0; for (j = I + jmp; j <= n; j ++) {tmp + = a [j]; if (tmp> = m) {if (tmp <sum) {cnt = 0; sum = tmp; B [cnt] [0] = I; B [cnt] [1] = j; cnt ++;} else if (tmp = sum) {B [cnt] [0] = I; B [cnt] [1] = j; cnt ++;} if (tmp = m & j-I> 1) {jmp = j-I-1 ;}else {jmp = 0 ;}break ;}} if (tmp = m & j = n) break ;} www.2cto.com for (I = 0; I <cnt-1; I ++) {printf ("% d-% d \ n", B [I] [0], B [I] [1]);} printf ("% d-% d", B [I] [0], B [I] [1]);} return 0 ;}