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. ubunto 免输入密码 登录 putty ssh-keygen

    交互式密码不安全,现在改用 ssh 证书方式,不用输入密码使用公钥证书登录. 方法1, 此方法,仅试用于,仅使用win putty 来连接方式使用,如果双方都是 linux 如 rsync 同步等时, ...

  2. 微信小程序用setData修改数组或对象中的一个属性值,超好用,最简单的实现方法,不容错过!大神们 都 在 看 的方法!!!

    在page中 data: { info: [{ name: "yuki", tou: "../img/head.jpg", zGong: 130, gMoney ...

  3. PG归并排序算法详解

    前言 归并排序算法是连接算法中比较复杂的算法,相比嵌套循环与Hash匹配而言.本节会通过实例来说明该算法在PG中的具体实现. 在PG中,通过状态机来实现--归并-连接.当然这里的完整流程是排序--归并 ...

  4. MATLAB神经网络(7) RBF网络的回归——非线性函数回归的实现

    7.1 案例背景 7.1.1 RBF神经网络概述 径向基函数是多维空间插值的传统技术,RBF神经网络属于前向神经网络类型,网络的结构与多层前向网络类似,是一种三层的前向网络.第一层为输入层,由信号源结 ...

  5. 自定义添加$_SERVER中的变量

    如何根据自己项目中的需求自定义$_SERVER中的变量呢?比如$_SERVER['XU_TEXT_PARAM'],这个超全局变量输出肯定是会报错的.那如何自定义它,让它输出我们想要输出的值呢? 1.在 ...

  6. 解决git推不上去1

    在使用 Android Studio 对源代码进行push到码云时可出错,报错如下: error: failed to push some refs to 'https://gitee.com/文件路 ...

  7. spring boot 学习笔记(一)

    学习链接:http://www.cnblogs.com/ityouknow/category/914493.html 定义 spring boot 是由pivotal 团队提供的权限框架,设计目的是用 ...

  8. (转)解决windows live writer的段首缩进问题

    原文地址:http://blog.csdn.net/xiao_wanpeng/article/details/6381799 Windows live writer 默认是没有段首缩进的,并且不能修改 ...

  9. 微信小程序修改request合法域名不生效及解决方法

    在小程序微信公众平台修改后请求,依然在console中显示修改前的域名. 解决:在小程序开发者工具中点击“详情”后点击“域名信息”,就会自动刷新

  10. 2016 Multi-University Training Contest 1 T3

    题目要求出所有合法点对间的最短路径的平均值,因此我们应当求出所有合法最短点对的最 短路径之和,再除以合法点对个数. 题目中Guard之间有着很不自然的制约关系,每个Guard的周围和同行.列都不能有其 ...