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分查找的特点 即最后找到的那个值是最接近我们需要值且至少大于我们需要的值 这就与题目相契合了 暴力做法 直接从每个点开始向后搜索
 #define _CRT_SECURE_NO_WARNINGS
#include <climits>
#include<iostream>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<algorithm>
#include<string>
#include<cmath>
using namespace std;
int Dia[];
struct Node
{
int begin, end, sum;
};
int main()
{
vector<Node> V;
int N, M;
cin >> N >> M;
for (int i=; i < N; i++)
cin >> Dia[i];
for (int i = ; i < N; i++)
{
int sum = ;
for (int j = i; j < N; j++)
{
sum += Dia[j];
if (sum >= M)
{
if (!V.size())
V.push_back({ i,j,sum });
else if (sum < V[].sum)
{
V.clear();
V.push_back({ i,j,sum });
}
else if (sum == V[].sum)
V.push_back({ i,j,sum });
}
}
}
for (auto it : V)
cout << it.begin+<< "-" << it.end+<< endl;
}

二分查找 正确代码

 #define _CRT_SECURE_NO_WARNINGS
#include <climits>
#include<iostream>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<algorithm>
#include<string>
#include<cmath>
using namespace std;
int Dia[];
int N, M;
struct Node
{
int begin, end, sum;
};
vector<Node> V;
void BinarySearch(int i,int&j,int&sum)
{
int begin = i, end = N;
while (begin<end)
{
int mid = (begin + end) / ;
if (Dia[mid] - Dia[i-] >= M) //一直与i-1比较 因要从i开始计算
end = mid;
else
begin = mid + ;
}
j = end;
sum = Dia[end] - Dia[i - ];
}
int main()
{
cin >> N >> M;
for (int i = ; i <=N; i++)
{
cin >> Dia[i];
Dia[i] += Dia[i - ];
}
for (int i = ; i <= N; i++)
{
int sum = ,j;
BinarySearch(i, j, sum);
if(sum>=M)
if (!V.size())
V.push_back({ i,j,sum });
else
{
if (sum < V[].sum)
{
V.clear();
V.push_back({ i,j,sum });
}
else if (sum == V[].sum)
V.push_back({ i,j,sum });
}
}
for (auto it : V)
cout << it.begin << "-" << it.end << endl;
}

1044 Shopping in Mars (25分)(二分查找)的更多相关文章

  1. PAT 甲级 1044 Shopping in Mars (25 分)(滑动窗口,尺取法,也可二分)

    1044 Shopping in Mars (25 分)   Shopping in Mars is quite a different experience. The Mars people pay ...

  2. PAT Advanced 1044 Shopping in Mars (25) [⼆分查找]

    题目 Shopping in Mars is quite a diferent experience. The Mars people pay by chained diamonds. Each di ...

  3. 1044 Shopping in Mars (25 分)

    Shopping in Mars is quite a different experience. The Mars people pay by chained diamonds. Each diam ...

  4. 【PAT甲级】1044 Shopping in Mars (25 分)(前缀和,双指针)

    题意: 输入一个正整数N和M(N<=1e5,M<=1e8),接下来输入N个正整数(<=1e3),按照升序输出"i-j",i~j的和等于M或者是最小的大于M的数段. ...

  5. 1044. Shopping in Mars (25)

    分析: 考察二分,简单模拟会超时,优化后时间正好,但二分速度快些,注意以下几点: (1):如果一个序列D1 ... Dn,如果我们计算Di到Dj的和, 那么我们可以计算D1到Dj的和sum1,D1到D ...

  6. A1044 Shopping in Mars (25 分)

    一.技术总结 可以开始把每个数都直接相加当前这个位置的存放所有数之前相加的结果,这样就是递增的了,把i,j位置数相减就是他们之间数的和. 需要写一个函数用于查找之间的值,如果有就放返回大于等于这个数的 ...

  7. PAT (Advanced Level) 1044. Shopping in Mars (25)

    双指针. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...

  8. PAT甲题题解-1044. Shopping in Mars (25)-水题

    n,m然后给出n个数让你求所有存在的区间[l,r],使得a[l]~a[r]的和为m并且按l的大小顺序输出对应区间.如果不存在和为m的区间段,则输出a[l]~a[r]-m最小的区间段方案. 如果两层fo ...

  9. pat1044. Shopping in Mars (25)

    1044. Shopping in Mars (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Shop ...

随机推荐

  1. CVPR 2020 全部论文 分类汇总和打包下载

    CVPR 2020 共收录 1470篇文章,根据当前的公布情况,人工智能学社整理了以下约100篇,分享给读者. 代码开源情况:详见每篇注释,当前共15篇开源.(持续更新中,可关注了解). 算法主要领域 ...

  2. 「从零单排HBase 05」核心特性region split

    HBase拥有出色的扩展性,其中最依赖的就是region的自动split机制. 1.split触发时机与策略 前面我们已经知道了,数据写入过程中,需要先写memstore,然后memstore满了以后 ...

  3. 3L-最好、最坏、平均、均摊时间复杂度

    关注公众号 MageByte,设置星标点「在看」是我们创造好文的动力.后台回复 "加群" 进入技术交流群获更多技术成长. 本文来自 MageByte-青叶编写 上次我们说过 时间复 ...

  4. Simulink仿真入门到精通(一) Simulink界面介绍

    Simulink提供了一个动态系统建模.仿真和综合分析的集成环境,是MATLAB最重要的组件之一. 以模块为功能单位,通过信号线进行连接 通过GUI调配每个模块的参数 仿真结果以数值和图像等形象化方式 ...

  5. 测试必知必会系列- Linux常用命令 - tar

    21篇测试必备的Linux常用命令,每天敲一篇,每次敲三遍,每月一循环,全都可记住!! https://www.cnblogs.com/poloyy/category/1672457.html 压缩一 ...

  6. webpack资料,还需整理

    参考地址: https://github.com/ruanyf/webpack-demos#demo01-entry-file-source http://www.jianshu.com/p/4df9 ...

  7. .net 4.0 以下HttpWebRequest Header 修改hosts方法

    .net 4.0 以下HttpWebRequest Header 修改hosts方法 特此记录 public class CusteredHeaderCollection : WebHeaderCol ...

  8. golang socket编程 net.Conn IO.EOF解读

    结论 首先,先定义下我的理解,当在Read时,收到一个IO.EOF,代表的就是对端已经关闭了发送的通道,通常来说是发起了FIN. 那么根据自己的实际业务,就可以进行判断,这里的IO.EOF到底该怎么利 ...

  9. Python异常处理,将异常写入到一个文件

    '''定义一个函数func(urllist) urllist:为URL的列表,例如:['http://xx.com','http://www.xx.com','http://www.xxx.com'. ...

  10. session生命周期,与cookie的区别

    sessinon在用户访问第一次访问服务器时创建. Session什么时候失效? 1. 服务器会把长时间没有活动的Session从服务器内存中清除,此时Session便失效.Tomcat中Sessio ...