PAT.1044 Shopping in Mars(双指针)

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 (≤), 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 D1​​DN​​ (Di​​103​​ 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

这题,要用一些精妙的方法做才行,直接暴力会超时。

我的做题思路是:输入的时候先把前n项sn和算出来,接着用low和high双指针指向我们要寻找答案的首和尾,分别初始化为0和1.
原因等会会讲。由于sum[i -> j] = sum[j] - sum[i - 1]。因此我们只需要假设low为左 - 1,high就是右,因此初始化是0和1.
接着我们移动high,当sum[i -> j] 大于 pay时,说明中间有可能有答案,那我们不动high,low + 1,然后继续循环。如果
sum[i -> j] 等于pay,我们直接输出此时的low和high,并且
同时向后移动low和high,如果sum[i -> j] 小于pay,那我们high += 1。
找出所有答案即可。如果第一轮没找到,那就再从头到尾找一遍,找那个最小花费的值。
 
 
 1 #include <iostream>
 2 #include <vector>
 3 
 4 using std::cout;
 5 using std::cin;
 6 using std::vector;
 7 using std::endl;
 8 
 9 int min(int a, int b) {
10     return a < b ? a : b;
11 }
12 
13 const int inf = 0x3f3f3f3f;
14 
15 vector <int> vec, sum;
16 
17 int main() {
18     int n, k, diamonds, temp;
19     cin >> n >> k;
20     sum.push_back(0);
21     vec.push_back(0);
22     for(int i = 1; i <= n; i ++) {
23         cin >> diamonds;
24         vec.push_back(diamonds);
25         temp = sum[i - 1] + diamonds;
26         sum.push_back(temp);
27     }
28     int low = 0, min_cost = inf;
29     for(int i = 1; i <= n; i ++) {
30         if(sum[i] - sum[low] >= k) {
31             min_cost = min(min_cost, sum[i] - sum[low]);
32             if(sum[i] - sum[low] == k) {
33                 cout << low + 1 << '-' << i << endl;
34                 low ++;
35             } else {
36                 i --;
37                 low ++;
38             }
39         }
40     }
41     low = 0;
42     if(min_cost != k) {
43         for(int i = 1; i <= n; i ++) {
44             if(sum[i] - sum[low] == min_cost) {
45                 cout << low + 1 << '-' << i << endl;
46                 low ++;
47             } else if(sum[i] - sum[low] > min_cost) {
48                 low ++;
49                 i --;
50             }
51         }
52     }
53     return 0;
54 }

猜你喜欢

转载自www.cnblogs.com/bianjunting/p/12976879.html
今日推荐