1044 Shopping in Mars (25 point(s))
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:
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).
Cut before 5 or after 6, and take off the diamonds from the position 4 to 6 (with values 5+4+6=15).
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 (≤105), the total number of diamonds on the chain, and M (≤108), the amount that the customer has to pay. Then the next line contains N positive numbers D1⋯DN (Di≤103 for all i=1,⋯,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 ≤ j such that Di + … + Dj = 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 ≤ j such that Di + … + Dj >M with (Di + … + Dj −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:
16 15
3 2 1 5 4 6 8 7 16 10 15 11 9 12 14 13
Sample Output 1:
1-5
4-6
7-8
11-11
Sample Input 2:
5 13
2 4 5 7 9
Sample Output 2:
2-4
4-5
题目大意
给定一个数列,代表你所拥有的钱币面值,现在你要购买一件商品M,需要选一个连续子序列付钱,输出你所有可用于付钱的子序列的下标范围;如果没有,则输出子列和sum且要满足sum-M最小的下标范围(这句话不通顺,懒得改了)
解题思路
再输入序列的时候,就累加其和,所以arr[i]存的是从0位置到i位置的累计和,其中arr[0]初始化为0,先用二分查找arr[i]-M的值是否存在,如果存在,则打印其下标;如果不存在则将左下标存入idx数组,并记录最小差值MIN。该循环结束后,如果不存在,则遍历idx数组,依次取出左下标k,并累加arr[k]-arr[k-1],判断其和是否与MIN相等,如相等则打印,若大于直接退出里层循环
代码
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
long long arr[100005] = { 0 };
int idx[100005] = { 0 };//存左下标
int main()
{
int N, M, i, num, cnt = 0;
int ret = scanf("%d%d", &N, &M);
for (i = 0; i < N; ++i) {
ret = scanf("%d", &num);
arr[i + 1] = arr[i] + num;
}
int flag = 0, MIN = 100000000;//flag代表是否存在恰好付钱的范围的标志
for (i = 0; i <= N; ++i) {
if (arr[i] >= M) {
//寻找arr[i]-M是否存在,如存在则求出其位置
int left = 0, right = i;
while (left <= right) {
int mid = (left + right) >> 1;
if (arr[mid] > arr[i] - M) right = mid - 1;
else left = mid + 1;
}
if (left > 0 && arr[left - 1] == arr[i] - M) {
printf("%d-%d\n", left, i);
flag = 1;
}
else if (arr[left] > arr[i] - M) {
if (MIN >= arr[i] - arr[left - 1]) {//必须有=
MIN = arr[i] - arr[left - 1];
idx[cnt++] = left;
}
}
}
}
if (!flag) {
for (int j = 0; j < cnt; ++j) {
int sum = 0;
for (int k = idx[j]; k <= N; ++k) {
sum += arr[k] - arr[k - 1];//累加原始序列
if (sum == MIN) {//如等于最小值
printf("%d-%d\n", idx[j], k);
}
else if (sum > MIN) break;//如大于则直接退出
}
}
}
return 0;
}