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. php实现post跳转

    大家否知道php可以利用header('Location')实现get请求跳转. php利用curl可以实现模拟post请求. 但是却找不到php现成的实现post跳转.那么问题来了,如果有这个需求该 ...

  2. 小程序session_key失效解决方案、后台解密个人数据信息

    目录 一.登录会话密钥 session_key 有效性 二.解决登录session_key 的问题 案例:解决session_key 过期问题,发送个人信息后台解密 后端解密信息,存入数据库 mysq ...

  3. 华为面试题(JAVA版)

    [编程题] 扑克牌大小时间限制:10秒空间限制:131072K扑克牌游戏大家应该都比较熟悉了,一副牌由54张组成,含3~A,2各4张,小王1张,大王1张.牌面从小到大用如下字符和字符串表示(其中,小写 ...

  4. Ubuntu下搭建.Net Core环境并发布MVC项目

    支撑环境 1. Windows 10 1809 12月更新版本(其他版本应该也行,但建议不低于1809,过低的版本可能无法安装子系统ubuntu18.04 LTS) 2. ubuntu 18.04 L ...

  5. 【Weiss】【第03章】练习3.19:计算后缀表达式

    [练习3.19] 编写一个程序计算后缀表达式的值. Answer: 计算的方法书上说得很明白了,看代码行,没写错误检测[因为懒]. 测试代码: #include <iostream> #i ...

  6. Fink SQL 实践之OVER窗口

    问题场景 Flink SQL 是一种使用 SQL 语义设计的开发语言,用它解决具体业务需求是一种全新体验,类似于从过程式编程到函数式编程的转变一样,需要一个不断学习和实践的过程.在看完了 Flink ...

  7. java基本类型、数组、和枚举类型

    开始之前先吐槽一下,学艺不精,面试要吃大亏,出来混迟早要还的. 别的不说了,从零开始复习基础知识 1.标识符和关键字 意义:标识符用于对变量.类.和方法的命名.规范的标识符命名可以提高程序的可读取性. ...

  8. 同网页的WebRTC实现与源码分析

    基本按照Real time communication with WebRTC搭建(下面简称该网站为官方tutorial) 本文重视WebRTC的基于同页面通信的代码实现,主要讲述顺序是WebRTC的 ...

  9. 分享macOS平台好用的视频分割、合并视频、提取音频、分离音频、音频转码的工具CCVideo

    CCVideo 是一款运行在macOS上可分割视频(可多段分割).合并视频.提取音频.分离音频.音频转码的工具,操作方便,只需简单几步,便可轻松完成. 下载地址

  10. Mass Spectrometry-Compatible Subcellular Fractionation for Proteomics 质谱兼容的蛋白质组学的亚细胞分离(解读人:王茹凯)

    文献名:Mass Spectrometry-Compatible Subcellular Fractionation for Proteomics(质谱兼容的蛋白质组学的亚细胞分离) 期刊名:Jpor ...