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 (≤10​5​​), the total number of diamonds on the chain, and M (≤10​8​​), 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 i=1,⋯,N) 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 >M with (Di + ... + Dj −M) 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

开始用两层循环超时,后来看别人的博客学会了用二分查找解决。

由于序列中全为整数,所以在以下标i开始的序列中,随着序列最后一个下标j的增长,这个i~j的序列的总和是递增的,所以在输入时记录从开始到当前元素的和,就可用二分查找解决。

二分查找时,当和大于等于M时,让end等于mid,循环条件为begin < end,这样begin,end最终将指向和大于等于M的最小下标;或者当最大和还小于M时,begin与end将指向最后一个元素。

 #include <iostream>
#include <vector>
using namespace std; vector<int> sum, ans;
void func(int i, int& j);
int N, M; int main()
{
int i, j, min;
cin >> N >> M;
sum.resize(N + );
for (i = ; i <= N; i++)
{
cin >> sum[i];
sum[i] += sum[i - ];
}
int minSum = sum[N];
for (i = ; i <= N; i++)
{
func(i, j);
int tempSum = sum[j] - sum[i - ];
if (tempSum > minSum) continue;
if (tempSum >= M)
{
if (tempSum < minSum)
{
ans.clear();
minSum = tempSum;
}
ans.push_back(i);
ans.push_back(j);
}
}
for (i = ; i < ans.size(); i += )
printf("%d-%d\n", ans[i], ans[i + ]);
return ;
} void func(int i, int& j)
{
int begin = i, end = N;
while (begin < end)
{
int mid = (begin + end) / ;
if (sum[mid] - sum[i - ] >= M)
end = mid;
else
begin = mid + ;
}
j = end;
}

pat甲级1044二分查找的更多相关文章

  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 甲级 1044 Shopping in Mars

    https://pintia.cn/problem-sets/994805342720868352/problems/994805439202443264 Shopping in Mars is qu ...

  3. PAT甲级1010踩坑记录(二分查找)——10测试点未过待更新

    题目分析: 首先这题有很多的坑点,我在写完之后依旧还有第10个测试点没有通过,而且代码写的不优美比较冗长勿喷,本篇博客用于记录写这道题的一些注意点 1.关于两个不同进制的数比大小一般采用将两个数都转化 ...

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

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

  5. 1044 Shopping in Mars (25分)(二分查找)

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

  6. PAT甲级考前整理(2019年3月备考)之一

       转载请注明出处:https://www.cnblogs.com/jlyg/p/7525244.html 终于在考前,刷完PAT甲级131道题目,不容易!!!每天沉迷在刷题之中而不能超脱,也是一种 ...

  7. PAT甲级题解(慢慢刷中)

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  8. PAT甲级目录

    树(23) 备注 1004 Counting Leaves   1020 Tree Traversals   1043 Is It a Binary Search Tree 判断BST,BST的性质 ...

  9. PAT甲级题分类汇编——杂项

    本文为PAT甲级分类汇编系列文章. 集合.散列.数学.算法,这几类的题目都比较少,放到一起讲. 题号 标题 分数 大意 类型 1063 Set Similarity 25 集合相似度 集合 1067 ...

随机推荐

  1. Socket通信常见问题

    1.检查服务器防火墙入站规则,是否允许对应端口通过.如果是云服务器,还需要通过对应账户去设置安全规则 2.服务端监听或绑定端口时,最好使用IPAddress.Any监听所有网口的改端口,创建socke ...

  2. FUI- 我离钢铁侠还差几步?

    本文来自网易云社区 作者:马宝 什么是FUI本文不累赘的可以自行Google,喜欢科幻的同学们都看一张图就能感受到FUI的魅力. 本文算是一篇所见即所的,可边学边干的原创教程.总结全文就一句话,&qu ...

  3. [Xcode 实际操作]四、常用控件-(18)MKMapView地图,将地理坐标转换为实际地名

    目录:[Swift]Xcode实际操作 本文将演示将地理坐标转换为实际地名. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIKit //首先往视 ...

  4. 前端三部曲之Css-- 1(常见的居中方式)

    下面来介绍一下web端页面最常见的居中方式 页面的基本结构:一个简单的div <!DOCTYPE html> <html lang="en"> <he ...

  5. 编写自定义GenericServlet

    用途: 编写自定义GenericServlet类后,子类只需继承这个类,就可以直接使用ServletConfig的config对象和ServletConfig接口的方法,而不需要init()方法获取c ...

  6. 清除input框对浏览器保存的用户名和密码自动填充问题

    清除input框对浏览器保存的用户名和密码自动填充问题: type类型写如下写法,聚焦的时候type类型为“password” <input ng-model="getpwd" ...

  7. AT2045 Salvage Robots

    传送门 这个题只要想到移动机器人和移动出口是等价的就好做了 考虑设\(f[i][j][k][t]\)为最远向左移动\(i\),向右移动\(j\),向上移动\(k\),向下移动\(t\),这个矩形内最多 ...

  8. Java基础笔记(十三)——面向对象

    类和对象 类是具有相同属性和方法的一组对象的集合,好比一个虚拟的轮廓(抽象的概念),确定对象将会拥有的特征(属性)和行为(方法). 对象是类的实例表现,是一个具体的东西(一个看得到.摸得着的具体实体) ...

  9. std::ref() 与 &

    引言 之前因为调整样式把博客园的样式毁了,所以一直在自己的另一个博客上更新,有兴趣的可以去观望一下:http://blog.yunlambert.top/最近还是把博客园拾起来吧..... 最近看到一 ...

  10. 【ACM】喷水装置

    喷水装置(一) 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 现有一块草坪,长为20米,宽为2米,要在横中心线上放置半径为Ri的喷水装置,每个喷水装置的效果都会让以 ...