A1044. Shopping in Mars

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 (<=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

 1 #include<cstdio>
 2 #include<iostream>
 3 using namespace std;
 4 long long diamond[100001], sum[100001];
 5 int binSearch1(long long diamond[], long long sum[], int low, int high, long long x){
 6     int mid, start = low;
 7     long long pay;
 8     while(low <= high){
 9         mid = low + (high - low) / 2;
10         pay = sum[mid] - sum[start] + diamond[start];
11         if(pay == x)
12             return mid;
13         else if(pay > x)
14             high = mid - 1;
15         else low = mid + 1;
16     }
17     return -1;
18 }
19 int binSearch2(long long diamond[], long long sum[], int low, int high, long long x, long long &ans){
20     int mid, start = low;
21     long long pay = 0;
22     while(low < high){
23         mid = low + (high - low) / 2;
24         pay = sum[mid] - sum[start] + diamond[start];
25         if(pay >= x)
26             high = mid;
27         else low = mid + 1;
28     }
29     pay = sum[low] - sum[start] + diamond[start];
30     ans = pay;
31     return low;
32 }
33 int main(){
34     long long N, M, temp = 0, ans, min = 999999999999;
35     int cut;
36     scanf("%lld%lld", &N, &M);
37     for(int i = 0; i < N; i++){
38         scanf("%lld", &diamond[i]);
39         temp += diamond[i];
40         sum[i] = temp;
41     }
42     int find = 0;
43     for(int i = 0; i < N; i++){
44         cut = binSearch1(diamond, sum, i, N - 1, M);
45         if(cut != -1){
46             printf("%d-%d\n", i + 1, cut + 1);
47             find = 1;
48         }
49     }
50     if(find == 0){
51         for(int i = 0; i < N; i++){
52             cut = binSearch2(diamond, sum, i, N, M, ans);
53             if (ans < min && cut != N)
54                 min = ans;
55         }
56         for(int i = 0; i < N; i++){
57             cut = binSearch2(diamond, sum, i, N, M, ans);
58             if(sum[cut] - sum[i] + diamond[i] == min)
59                 printf("%d-%d\n", i + 1, cut + 1, ans);
60         }
61     }
62     cin >> N;
63     return 0;
64 }
View Code

总结:

1、题意:给出一串数字,找出它们的一个子序列使得这个子序列的和刚好等于M。如果找不到,则找一个序列使得它的和大于M但又比其它大于M的序列的和小,如果这个序列有多个,则全部输出。 可以发现暴力破解会超时,只能二分解决。由于二分要求查找的序列是有序的,可以用diamond数组记录这些数字,sum数组记录这个序列的和,其中sum[ i ]表示diamond[0] 到 diamond[ i ]的和。在计算 i 到 j 的和时,直接sum[ j ] - sum[ i ] + diamond[ i ] 即可。且sum序列为递增。

2、二分法查找第一个满足某条件的元素,最后返回的是 low,有效结果也是low而非mid!

3、可以使用p、q双指针法来求和。pq之间序列即为所求。当和过大时,ans - num[p], p++; 当和太小时, q++, ans + num[q];

posted @ 2018-02-03 14:14  ZHUQW  阅读(119)  评论(0编辑  收藏  举报