题目
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: , the total number of diamonds on the chain, and , the amount that the customer has to pay. Then the next line contains N positive numbers 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:
1 | 16 15 |
Sample Output 1:
1 | 1-5 |
Sample Input 2:
1 | 5 13 |
Sample Output 2:
1 | 2-4 |
题解
思路
-
首先容易想到暴力解
-
即遍历每一个数
- 先设sum为0
- 从这个数再遍历
- sum不断加上下一个数,使sum刚好大于或等于要付的钱
- 判断sum和之前的min_cost来确定是否要更新答案
-
输出
-
容易写出这样的代码
-
num_diamonds, pay = list(map(int, input().split())) chain = list(map(int, input().split())) ans = [] min_cost = 99999 for i in range(num_diamonds): sum = 0 for j in range(i, num_diamonds): sum += chain[j] # 判断要不要更新ans if pay <= sum: if sum < min_cost: ans = [[i, j]] min_cost = sum elif sum == min_cost: ans.append([i, j]) min_cost = sum break for i in ans: print("-".join(list(map(lambda x: str(x + 1), i)))) <!--hexoPostRenderEscape:<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">+ 但是这样会有大量的重复计算,造成严重的超时。</span><br><span class="line"></span><br><span class="line">+ 更好的方式是二分查找</span><br><span class="line"></span><br><span class="line">+ 首先把从第一个数开始求和的结果放在一个列表里</span><br><span class="line"></span><br><span class="line">+ 即`[a[0], a[0]+a[1], a[0]+a[1]+a[2],...]`</span><br><span class="line"></span><br><span class="line">+ 为了方便起见,前面再加一个0,即`0, a[0], a[0]+a[1], a[0]+a[1]+a[2],...]` 这个0待会要用到</span><br><span class="line"></span><br><span class="line">+ 此时这个新建的列表,我们称为`_sum` ,是一个有序的,从小到大排列的。</span><br><span class="line"></span><br><span class="line">+ 如果我们找,从第一个数字开始,加到正好能使总和大于等于要付的钱的第一个数</span><br><span class="line"></span><br><span class="line">+ 就相当于在`_sum`找第一个大于等于要付的钱的数</span><br><span class="line"></span><br><span class="line">+ 这就可以使用二分查找了</span><br><span class="line"></span><br><span class="line">+ 可是从第二个数字开始呢?难道要再算走一遍`_sum`吗?</span><br><span class="line"></span><br><span class="line">+ 不必。</span><br><span class="line"></span><br><span class="line">+ 对于第二个数字开始,它的`_sum` 其实就是原来的`_sum` 减去`_sum[1]`。因为`_sum[1]`表示的就是第一个数。</span><br><span class="line"></span><br><span class="line">+ 同理,对于第N个数,它的`_sum`和列表就是最初的`_sum`剪去`_sum[N-1]`。因为从N到K的和就是从1到K的和减去从1到N的和。</span><br><span class="line"></span><br><span class="line">+ 那么对每个数走一遍二分查找即可。</span><br><span class="line"></span><br><span class="line">+ 因为我们是沿着下标顺序找的,所以最后不用排序。</span><br><span class="line"></span><br><span class="line">+ 还有一种方法是**双指针法**,没有二分查找帅,就不用了。</span><br><span class="line"></span><br><span class="line">## 数据结构</span><br><span class="line"></span><br><span class="line">+ _sum 记录从0开始的累加和</span><br><span class="line">+ l 是二分查找左边界</span><br><span class="line">+ r 是二分查找右边界</span><br><span class="line">+ mid 是二分查找的中间</span><br><span class="line">+ num临时变量,记录对i这个数来说,比要付钱略微大的数</span><br><span class="line">+ ans 是一个列表,记录要输出的答案</span><br><span class="line">+ ans的一项代表一组答案,是一个列表,包含两个数,即下标。</span><br><span class="line"></span><br><span class="line">## 算法</span><br><span class="line"></span><br><span class="line">+ 都写在思路里了。</span><br><span class="line"></span><br><span class="line">## 代码</span><br><span class="line"></span><br><span class="line">+ 因为此题对Python歧视,不管暴力解还是二分查找都有三个点超时。建议使用C++。原理都是一样的。</span><br><span class="line"></span><br><span class="line">### Python</span><br><span class="line"></span><br><span class="line">```python</span><br><span class="line">num_diamonds, pay = list(map(int, input().split()))</span><br><span class="line">chain = list(map(int, input().split()))</span><br><span class="line">_sum = [0] + [sum(chain[:i + 1]) for i in range(num_diamonds)]</span><br><span class="line">ans = []</span><br><span class="line">min_cost = _sum[-1]</span><br><span class="line">for i in range(num_diamonds): # 二分查找</span><br><span class="line"> l = i</span><br><span class="line"> r = num_diamonds - 1</span><br><span class="line"> while l < r:</span><br><span class="line"> mid = (l + r) // 2</span><br><span class="line"> if _sum[mid + 1] - _sum[i] < pay:</span><br><span class="line"> l = mid + 1</span><br><span class="line"> else:</span><br><span class="line"> r = mid</span><br><span class="line"> num = _sum[l + 1] - _sum[i] # 看情况更新答案</span><br><span class="line"> if num >= pay:</span><br><span class="line"> if num < min_cost:</span><br><span class="line"> ans = [[i + 1, l + 1]]</span><br><span class="line"> min_cost = num</span><br><span class="line"> elif num == min_cost:</span><br><span class="line"> ans.append([i + 1, l + 1])</span><br><span class="line"> min_cost = num</span><br><span class="line"></span><br><span class="line">for i in ans:</span><br><span class="line"> print("-".join(list(map(str, i))))</span><br><span class="line"></span><br></pre></td></tr></table></figure>:hexoPostRenderEscape-->
C++
1 |
|