题目

Shopping in Mars is quite a different experience. The Mars people pay by chained diamonds. Each diamond has a value (in Mars dollars M$). When making the payment, the chain can be cut at any position for only once and some of the diamonds are taken off the chain one by one. Once a diamond is off the chain, it cannot be taken back. For example, if we have a chain of 8 diamonds with values M$3, 2, 1, 5, 4, 6, 8, 7, and we must pay M$15. We may have 3 options:

  1. Cut the chain between 4 and 6, and take off the diamonds from the position 1 to 5 (with values 3+2+1+5+4=15).
  2. Cut before 5 or after 6, and take off the diamonds from the position 4 to 6 (with values 5+4+6=15).
  3. Cut before 8, and take off the diamonds from the position 7 to 8 (with values 8+7=15).

Now given the chain of diamond values and the amount that a customer has to pay, you are supposed to list all the paying options for the customer.

If it is impossible to pay the exact amount, you must suggest solutions with minimum lost.

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 numbers: $N$ ( $\le 10^5$ ), the total number of diamonds on the chain, and $M$ ( $\le 10^8$ ), the amount that the customer has to pay. Then the next line contains $N$ positive numbers $D_1 \cdots D_N$ ( $D_i\le 10^3$ for all $i=1, \cdots , N$ ) which are the values of the diamonds. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print i-j in a line for each pair of i $\le$ j such that $D$ i + … + $D$ j = $M$ . Note that if there are more than one solution, all the solutions must be printed in increasing order of i.

If there is no solution, output i-j for pairs of i $\le$ j such that $D$ i + … + $D$ j $> M$ with ( $D$ i + … + $D$ j $- M$ ) minimized. Again all the solutions must be printed in increasing order of i.

It is guaranteed that the total value of diamonds is sufficient to pay the given amount.

Sample Input 1:

1
2
16 15
3 2 1 5 4 6 8 7 16 10 15 11 9 12 14 13

Sample Output 1:

1
2
3
4
1-5
4-6
7-8
11-11

Sample Input 2:

1
2
5 13
2 4 5 7 9

Sample Output 2:

1
2
2-4
4-5

思路

这是一个子数列问题,具体要得到子数列和尽量接近某个值的所有可能情况。

用不太严谨的数学语言说就是要:

\[\left\{(i,j)\vert\sum_{n=i}^{j}D_n=min\left\{\sum_{n=m}^{l}{D_n}\vert\sum_{n=m}^{l}{D_n}\ge M\right\}\right\}\]

类似地题目参考b1030和a1007两题,遍历部分是可以达到$\mathcal{O}(N)$的时间复杂度。

本题则需要额外记录目前能达到的超过M的最小子列和,以及相应的所有子数列。如果按照我的方法,则需要一个和输入数列几乎一样大的数列来记录子数列的首尾。

代码

Github最新代码,欢迎交流

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <stdio.h>

int main()
{
	int N, M, D[100000] = {0}, pair[100000][2] = {{0}};

	scanf("%d %d", &N, &M);
	for (int n = 0; n < N; n++)
		scanf("%d", D + n);

	int i = 0, j = 0, sum = D[0], min = 100000000, count = 0;
	while (1) {
		if (sum <= min && sum >= M) {
			/* reset if found lower solution */
			if (sum < min) {
				min = sum;
				count = 0;
			}
			/* record */
			pair[count][0] = i;
			pair[count][1] = j;
			count ++;
		}
		/* Find closest solution */
		if (sum <= M && j < N - 1)
			sum += D[++j];
		else if (i < N - 1)
			sum -= D[i++];
		else
			break;
	}

	for (int n = 0; n < count; n++)
		printf("%d-%d\n", pair[n][0] + 1, pair[n][1] + 1);

	return 0;
}