pat甲级1044二分查找

北慕城南 提交于 2020-01-27 02:43:11
1044 Shopping in Mars(25 分)

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

开始用两层循环超时,后来看别人的博客学会了用二分查找解决。

由于序列中全为整数,所以在以下标i开始的序列中,随着序列最后一个下标j的增长,这个i~j的序列的总和是递增的,所以在输入时记录从开始到当前元素的和,就可用二分查找解决。

二分查找时,当和大于等于M时,让end等于mid,循环条件为begin < end,这样begin,end最终将指向和大于等于M的最小下标;或者当最大和还小于M时,begin与end将指向最后一个元素。

 1 #include <iostream>
 2 #include <vector>
 3 using namespace std;
 4 
 5 vector<int> sum, ans;
 6 void func(int i, int& j);
 7 int N, M;
 8 
 9 int main()
10 {
11     int i, j, min;
12     cin >> N >> M;
13     sum.resize(N + 1);
14     for (i = 1; i <= N; i++)
15     {
16         cin >> sum[i];
17         sum[i] += sum[i - 1];
18     }
19     int minSum = sum[N];
20     for (i = 1; i <= N; i++)
21     {
22         func(i, j);
23         int tempSum = sum[j] - sum[i - 1];
24         if (tempSum > minSum) continue;
25         if (tempSum >= M)
26         {
27             if (tempSum < minSum)
28             {
29                 ans.clear();
30                 minSum = tempSum;
31             }
32             ans.push_back(i);
33             ans.push_back(j);
34         }
35     }
36     for (i = 0; i < ans.size(); i += 2)
37         printf("%d-%d\n", ans[i], ans[i + 1]);
38     return 0;
39 }
40 
41 void func(int i, int& j)
42 {
43     int begin = i, end = N;
44     while (begin < end)
45     {
46         int mid = (begin + end) / 2;
47         if (sum[mid] - sum[i - 1] >= M)
48             end = mid;
49         else
50             begin = mid + 1;
51     }
52     j = end;
53 }

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!