• PAT 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).

    1. Cut before 5 or after 6, and take off the diamonds from the position 4 to 6 (with values 5+4+6=15).
    2. 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 (<=10^5^), the total number of diamonds on the chain, and M (<=10^8^), 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 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 D~i~ + ... + D~j~ = 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 D~i~ + ... + D~j~ > M with (D~i~ + ... + D~j~ - 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
    
     
    题目大意: 找出和等于目标数的连续子集, 或者这找出和比目标数大但是差值最小的连续子集;
    思路:开始用暴力方法解决,有两个点不能通过测试; 原因在于进行很多重复不必要的加法运算。 改进后,用queue记录连续子集的每一个元素的值和位置; 
        如果连续子集的和等于目标数就添加到答案中, 并且弹出queue最前面的子集元素, 更新子集的和
        如果连续子集的和大于目标数, 就需要一直弹出queue前面的子集元素,直到子集和不大于目标和; 在这个过程中更新和比目标数大且差值最小的子集;
        需要注意的是,存在这种情况,弹出一个或者多个元素和子集的和刚好等于目标数, 我们需要把遍历次数i减一,才能保证答案的正确性
     1 #include<iostream>
     2 #include<vector>
     3 #include<algorithm>
     4 #include<queue>
     5 using namespace std;
     6 struct node{ int s, e;};
     7 struct node1{ int val, index;};
     8 int cmp(node a, node b){return a.s<b.s;}
     9 
    10 int main(){
    11   int n, m, i, j;
    12   cin>>n>>m;
    13   vector<int> v(n);
    14   queue<node1> q;
    15   vector<node> ans, ans1;
    16   for(i=0; i<n; i++) scanf("%d", &v[i]);
    17   int minn=99999999, sum=0;19   for(i=0; i<n; i++){
    20     node nnode;
    21     node1 nnode1;
    22     nnode1.index=i+1; nnode1.val=v[i];
    23     if(sum!=m){
    24         q.push(nnode1);
    25         sum += v[i];
    26     }
    27     if(sum==m){
    28       nnode.s=q.front().index; nnode.e=q.back().index;
    29       ans.push_back(nnode);
    30       sum -= q.front().val;
    31       q.pop();
    32       continue;
    33     }else if(sum>m){
    34         while(sum>m){
    35           if(sum<=minn){
    36               if(sum<minn) ans1.clear();
    37               nnode.s=q.front().index; nnode.e=q.back().index;
    38               minn = sum;
    39               ans1.push_back(nnode);
    40           }
    41           sum -= q.front().val;
    42           q.pop();
    43       }
    44       if(sum==m) i -= 1;
    45     continue;
    46     }
    47   }
    48   
    49   if(ans.size()){
    50     sort(ans.begin(), ans.end(), cmp);
    51     for(i=0; i<ans.size(); i++) printf("%d-%d
    ", ans[i].s, ans[i].e);
    52   }else{
    53     sort(ans1.begin(), ans1.end(), cmp);
    54     for(i=0; i<ans1.size(); i++) printf("%d-%d
    ", ans1[i].s, ans1[i].e);
    55   }
    56   return 0;
    57 }
    有疑惑或者更好的解决方法的朋友,可以联系我,大家一起探讨。qq:1546431565
  • 相关阅读:
    [Android Pro] Android源码编译之Nexus5真机编译
    [设计模式] 策略模式(Strategy)
    [Android] Anreoid repo 切换分支
    [Android] repo 下载Android源码(国内镜像)
    [Android] osx下如何使用SublimeText阅读Android系统源码
    [Ubuntu] ubuntu的tty下挂载移动硬盘拷贝数据
    Elasticsearch
    Flink简介
    SQL中instr和like的使用区别
    count(1) 与 count(*) 比较
  • 原文地址:https://www.cnblogs.com/mr-stn/p/9215172.html
Copyright © 2020-2023  润新知