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 ij 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 ij 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

分析请参考【PAT1044】Shopping in Mars 二分法

注意:该题对时间有限制,需要用改进的算法实现,之前实现了一个O(N^2)级的,三个测试点没过,采用二分法即可解决。

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std; static const long int MAX = ; long int n, pay;
int M[MAX];
int D[MAX];
vector<long int> v; int findBestsum(int i, int n, int ddl){
int left = i+;
int right = n;
while(left<right){
int mid = (left+right)/;
if(M[mid]-M[i]>=ddl){
right = mid;
}
else{
left = mid+;
}
}
D[i] = right;
return M[right] - M[i];
} int main(){
cin>>n>>pay;
M[] = ;
for(long int i=;i<=n;i++){
cin>>M[i];
M[i] = M[i] + M[i-];
}
int res;
int temp = ;
for(int i=;i<n;i++){
res = findBestsum(i, n, pay);
if(res>=pay){
if(res<temp){
temp = res;
v.clear();
v.push_back(i);
}
else if(res==temp){
v.push_back(i);
}
}
}
for(vector<long int>::iterator it=v.begin();it!=v.end();it++){
cout<<*it+<<"-"<<D[*it]<<endl;
}
return ;
}

1044 Shopping in Mars的更多相关文章

  1. 1044 Shopping in Mars (25 分)

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

  2. PAT 1044 Shopping in Mars[二分][难]

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

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

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

  4. PAT 甲级 1044 Shopping in Mars

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

  5. PTA(Advanced Level)1044.Shopping in Mars

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

  6. 1044 Shopping in Mars (25 分)

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

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

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

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

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

  9. 1044. Shopping in Mars (25)

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

随机推荐

  1. python2.6更改为Python2.7

    文中为Python2.6.6,改为Python2.6即可,因为没有/usr/bin/python2.6.6,只有/usr/bin/python2.6 http://blog.csdn.net/jcjc ...

  2. Scrum Meeting 11.05

    成员 今日任务 明日计划 用时 徐越 代码移植 学习ListView+simpleAdapter,actionBar.阅读并修改前端代码 4h 赵庶宏 服务器配置,代码移植  构建后端数据库,进行完善 ...

  3. Tomcat提高并发

    Centos7环境下Tomcat 启动慢的解决方案1.增加熵值(本质增加random)安装软件 >> Yum –y install rng-tools 启动熵服务 >> Sys ...

  4. JavaScript实现大整数减法

    继上一篇博文写了大整数加法之后,我又模拟上篇博文的算法,自己实现了大整数减法. 大整数减法相对于加法来说,稍微复杂一点.由于要考虑一些情况: 1. 两个数相减,可能会出现结果为正.负和0三种情况: 2 ...

  5. 《UML大战需求分析》-读后感一

    UML英文全拼是unified modeling language 就是统一建模语言. UML就是一种软件开发中帮助我们设计的标准,虽然说是建模语言但是它是图形,图形能更清楚的表达我们对软件的想法.U ...

  6. "1001. A+B Format (20)" 解题报告

    Github : git@github.com:Circlecos/object-oriented.git PDF Of Markdown : "1001. A+B Format (20)& ...

  7. Beta 冲刺 (6/7)

    队名:Boy Next Door 燃尽图 代码写入 https://github.com/mangoqiqi/paybook/tree/master/Desktop/Web%E8%B4%A6%E5%8 ...

  8. 对WEB url 发送POST请求

    package com.excellence.spark; import java.util.List; import com.excellence.spark.test.test; import c ...

  9. ejb与javabean的区别总结

    EJB的英文全称是企业级的JavaBean 两者是完全不同的 JavaBean是一个组件,而EJB就是一个组建框架 JavaBean面向的是业务逻辑和表示层的显示,通过编写一个JavaBean,可以将 ...

  10. cropper.js 超级好用的裁剪图片工具

    最近要做一个照片裁剪功能.就选用了cropper.js 代码如下:贴出来 <div class="container"> <div class="row ...