zoukankan      html  css  js  c++  java
  • 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

    题目分析 :刚开始写了个暴力求解 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
  • 相关阅读:
    Handlebars模板引擎简单使用
    SpringMVC学习笔记001-服务器端获取JSON字符串并解析
    EL表达式的使用
    SpringMVC学习笔记001
    ExtJS学习之路碎碎念
    Microsoft Word 使用技巧总结
    驼峰命名法
    视图生命周期
    git命令
    git的使用1[转]
  • 原文地址:https://www.cnblogs.com/57one/p/12031513.html
Copyright © 2011-2022 走看看