(PAT 1044)Shopping in Mars (二分法lower_bound)

系统管理员 2022-03-27 12:38 218阅读 0赞

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 ij 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 ij 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:

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

Sample Output 1:

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

Sample Input 2:

  1. 5 13
  2. 2 4 5 7 9

Sample Output 2:

  1. 2-4
  2. 4-5

解题思路:

这题做了好久才做出来

因为是取中间某一段数列的和,所以可以把数列按段加起来

这样就得到一串单调递增的数列,由于是有序的,我们可以考虑使用二分法防止超时

依然是从数组中最小的元素开始遍历,每遍历一个元素,使用二分法查找在数列中是否有对应的元素使该 元素+对应元素>=目标和

对于测试用例2中的情况,我们可以使用lower_bound,找到第一个大于目标和的元素(那就是最小的),我们建立一个vector用来保存即可。

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. using namespace std;
  5. const int MAXN = 100010;
  6. int des[MAXN];
  7. int Dimonds[MAXN];
  8. int N, M;
  9. int lowerBound(int i, long long x) {
  10. int mid;
  11. int left = i + 1, right = N;
  12. while (left < right) {
  13. mid = (left + right) / 2;
  14. if (Dimonds[mid] - Dimonds[i] >= x) {
  15. right = mid;
  16. }
  17. else {
  18. left = mid + 1;
  19. }
  20. }
  21. des[i] = right; //将左下标保存到数组中
  22. return Dimonds[right] - Dimonds[i]; //返回子段的和
  23. }
  24. int main() {
  25. scanf("%d %d", &N, &M);
  26. Dimonds[0] = 0;
  27. for (int i = 1; i <= N; ++i) {
  28. int num;
  29. scanf("%d", &num);
  30. Dimonds[i] = Dimonds[i - 1] + num;
  31. }
  32. long long MM = 9999999999;
  33. vector<int> res;
  34. for (int i = 0; i < N; ++i) {
  35. int sha = lowerBound(i, (long long)M);
  36. if (sha >= M) {
  37. if (sha == MM) { //如果存在等同于MM的数列和,则将下标保存
  38. res.push_back(i);
  39. }
  40. else if(sha < MM) {
  41. res.clear(); //否则,清空vector,保存更小的下标和
  42. res.push_back(i);
  43. MM = sha;
  44. }
  45. }
  46. else {
  47. break;
  48. }
  49. }
  50. for (int i = 0; i < res.size(); ++i) {
  51. printf("%d-%d\n", res[i]+1, des[res[i]]);
  52. }
  53. system("PAUSE");
  54. return 0;
  55. }

发表评论

表情:
评论列表 (有 0 条评论,218人围观)

还没有评论,来说两句吧...

相关阅读

    相关 PAT乙级1044

    1044 火星数字 (20 分) 火星人是以 13 进制计数的: 地球人的 0 被火星人称为 tret。 地球人数字 1 到 12 的火星文分别为:jan, f