ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

1044 Shopping in Mars (25分)(二分查找)

2019-12-12 20:57:12  阅读:280  来源: 互联网

标签:25 Shopping 1044 int sum Dia diamonds 15 include


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 (≤), the total number of diamonds on the chain, and M (≤), the amount that the customer has to pay. Then the next line contains N positive numbers D​1​​⋯D​N​​ (D​i​​≤10​3​​ for all ,) 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 > with (Di + ... + Dj −) 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

题目分析 :刚开始写了个暴力求解 3个点超时了
搜了答案才知道时要利用二分法查找
用sum[i]记录下从1到i的值 然后从1开始 2分查找
这个做法很灵性的使用了2分查找的特点 即最后找到的那个值是最接近我们需要值且至少大于我们需要的值 这就与题目相契合了

暴力做法 直接从每个点开始向后搜索
 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include <climits>
 3 #include<iostream>
 4 #include<vector>
 5 #include<queue>
 6 #include<map>
 7 #include<set>
 8 #include<stack>
 9 #include<algorithm>
10 #include<string>
11 #include<cmath>
12 using namespace std;
13 int Dia[100000];
14 struct Node
15 {
16     int begin, end, sum;
17 };
18 int main()
19 {
20     vector<Node> V;
21     int N, M;
22     cin >> N >> M;
23     for (int i=0; i < N; i++)
24         cin >> Dia[i];
25     for (int i = 0; i < N; i++)
26     {
27         int sum = 0;
28         for (int j = i; j < N; j++)
29         {
30             sum += Dia[j];
31             if (sum >= M)
32             {
33                 if (!V.size())
34                     V.push_back({ i,j,sum });
35                 else if (sum < V[0].sum)
36                 {
37                     V.clear();
38                     V.push_back({ i,j,sum });
39                 }
40                 else if (sum == V[0].sum)
41                     V.push_back({ i,j,sum });
42             }
43         }
44     }
45     for (auto it : V)
46         cout << it.begin+1<< "-" << it.end+1<< endl;
47 }
View Code

 

二分查找 正确代码

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include <climits>
 3 #include<iostream>
 4 #include<vector>
 5 #include<queue>
 6 #include<map>
 7 #include<set>
 8 #include<stack>
 9 #include<algorithm>
10 #include<string>
11 #include<cmath>
12 using namespace std;
13 int Dia[100000];
14 int N, M;
15 struct Node
16 {
17     int begin, end, sum;
18 };
19 vector<Node> V;
20 void BinarySearch(int i,int&j,int&sum)
21 {
22     int begin = i, end = N;
23     while (begin<end)
24     {
25         int mid = (begin + end) / 2;
26         if (Dia[mid] - Dia[i-1] >= M)  //一直与i-1比较 因要从i开始计算
27             end = mid;
28         else
29             begin = mid + 1;
30     }
31     j = end;
32     sum = Dia[end] - Dia[i - 1];
33 }
34 int main()
35 {
36     cin >> N >> M;
37     for (int i = 1; i <=N; i++)
38     {
39         cin >> Dia[i];
40         Dia[i] += Dia[i - 1];
41     }
42     for (int i = 1; i <= N; i++)
43     {
44         int sum = 0,j;
45         BinarySearch(i, j, sum);
46         if(sum>=M)
47             if (!V.size())
48                 V.push_back({ i,j,sum });
49             else
50             {
51                 if (sum < V[0].sum)
52                 {
53                     V.clear();
54                     V.push_back({ i,j,sum });
55                 }
56                 else if (sum == V[0].sum)
57                     V.push_back({ i,j,sum });
58             }
59     }
60     for (auto it : V)
61         cout << it.begin << "-" << it.end << endl;
62 }
View Code

 

标签:25,Shopping,1044,int,sum,Dia,diamonds,15,include
来源: https://www.cnblogs.com/57one/p/12031513.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有