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. Git使用ssh公钥

    Git使用ssh公钥 一.  何谓公钥 1.很多服务器都是需要认证的,ssh认证是其中的一种.在客户端生成公钥,把生成的公钥添加到服务器,你以后连接服务器就不用每次都输入用户名和密码了. 2.很多gi ...

  2. unittest测试框架详解

    单元测试的定义 1. 什么是单元测试? ​ 单元测试是指,对软件中的最小可测试单元在与程序其他部分相隔离的情况下进行检查和验证的工作,这里的最小可测试单元通常是指函数或者类,一般是开发来做的,按照测试 ...

  3. 在 centos6 上安装 LAMP

    LAMP 代表的是 Linux, Apache, MySQL, 以及 PHP.   第一步,安装 Apache 使用 yum 安装 sudo yum install httpd 启动 httpd 服务 ...

  4. python基础学习day7

    基础数据类型的补充:编码的进阶 str capitalize() 首字母(第一个单词)大写,其余变小写 s1 = 'I LIVE YOU' print(s1.capitalize()) >> ...

  5. mysql的那些事之架构

    MySQL架构的那些事 此篇博客为原创,欢迎转载,转载时请注明出处,谢谢 最近深入学习了一下mysql的内容,想把自己的理解分享出来. mysql架构 逻辑架构 Connectors:连接器 Mana ...

  6. CentOS7采用tar.gz包方式安装Mysql5.7

    软件:VMware Linux版本:CentOS 7 一.安装mysql(采用tar.gz包安装Mysql5.7) 1.安装开发工具包 [root@localhost ~]# yum groups m ...

  7. H5页面,输入框的光标,如果页面上下滑动光标停留在页面上,除了输入框外,松手过了一段时间才跑回输入框里面

    有点类似这种情况 其中一个博主描述得比较详细,主要还有图 我是直接在App.vue主文件那里添加一下代码,主要是添加一个监听器,如果touchmove的时候就会触发让其失焦,就会消失那个光标,需要再次 ...

  8. 如何使用域名访问自己的Windows服务器(Java web 项目)

    如何使用域名访问自己的Windows服务器(Java web 项目) 写在前面 前段时间在阿里云弄了个学生服务器,就想着自己搭建一个网站试一试,在网上查阅相关资料时发现大部分都是基于服务器是Linux ...

  9. asp.net core 3.1 引用的元包dll版本兼容性问题解决方案

    自从.netcore 3.1出来后,大家都想立马升级到最新版本.我也是如此,微软也对.netcore 3.1的官方组件不断升级,几乎每隔几天就会有部分元包可以升级.每次打开Nuget包管理器,“更新” ...

  10. SSL/TLS 安全测试

    本文介绍了使用半自动化工具执行SSL&TLS安全性评估的过程,以及如何使用手动及工具的测试方法验证并发现问题.目的是优化TLS和SSL安全测试流程,帮助信息安全顾问在渗透测试时在TLS / S ...