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​​⋯DN​​ (Di​​≤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 + ... + DjM) 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 注意点:1.注意边界值n+1;
    2.注意下标j和j+1;
 #include<bits/stdc++.h>
using namespace std; const int maxn=;
const int inf=0x3f3f3f3f; int sum[maxn]; int n,S,nearS=inf; int upper_bound(int low,int high,int x){
int left=low,right=high,mid; if(sum[high]<=x)
return high+; while(left<right){
mid=(left+right)/; if(sum[mid]>x){
right = mid;
}else{
left = mid+;
}
} return left;
} int main(){
cin>>n>>S; for(int i=;i<=n;i++){
cin>>sum[i];
sum[i]+=sum[i-];
} for(int i=;i<=n;i++){
int j=upper_bound(i,n,sum[i-]+S); if(sum[j-]-sum[i-]==S){
nearS=S;
break;
}else if(sum[j]-sum[i-]>S&&sum[j]-sum[i-]<nearS){
nearS=sum[j]-sum[i-];
} } for(int i=;i<=n;i++){
int j = upper_bound(i,n,sum[i-]+nearS); if(sum[j-]-sum[i-]==nearS)
cout<<i<<"-"<<j-<<endl;
} return ; }

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. 1044 Shopping in Mars (25分)(二分查找)

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

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

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

  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. PAT (Advanced Level) 1044. Shopping in Mars (25)

    双指针. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...

  7. PAT甲题题解-1044. Shopping in Mars (25)-水题

    n,m然后给出n个数让你求所有存在的区间[l,r],使得a[l]~a[r]的和为m并且按l的大小顺序输出对应区间.如果不存在和为m的区间段,则输出a[l]~a[r]-m最小的区间段方案. 如果两层fo ...

  8. A1044 Shopping in Mars (25 分)

    一.技术总结 可以开始把每个数都直接相加当前这个位置的存放所有数之前相加的结果,这样就是递增的了,把i,j位置数相减就是他们之间数的和. 需要写一个函数用于查找之间的值,如果有就放返回大于等于这个数的 ...

  9. pat1044. Shopping in Mars (25)

    1044. Shopping in Mars (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Shop ...

随机推荐

  1. MySQL strcmp 函数

    STRCMP(str1, str2) 比较两个字符串,如果这两个字符串相等返回0,如果第一个参数是根据当前的排序小于第二个参数顺序返回-1,否则返回1. mysql> SELECT STRCMP ...

  2. MySQL-8.0填坑

    Client does not support authentication protocol 或 Authentication plugin 'caching_sha2_password' cann ...

  3. 86、使用Tensorflow实现,LSTM的时间序列预测,预测正弦函数

    ''' Created on 2017年5月21日 @author: weizhen ''' # 以下程序为预测离散化之后的sin函数 import numpy as np import tensor ...

  4. 关于jsp:include 动态引入的值传递问题(数据共享问题)

    <jsp:include page="search.jsp" flush="true"> <jsp:param name="gh&q ...

  5. Git 设置和取消代理(SOCKS5代理)

    设置代理 git config --global http.proxy 'socks5://127.0.0.1:1080' git config --global https.proxy 'socks ...

  6. JavaScript实现10大算法可视化

    参考博客: https://www.cnblogs.com/Unknw/p/6346681.html#4195503 十大经典算法 一张图概括: 名词解释: n:数据规模 k:“桶”的个数 In-pl ...

  7. PAT甲级——A1151 LCA_in_a_BinaryTree【30】

    The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U ...

  8. Spring Cloud配置中心内容加密

    从配置获取的配置默认是明文的,有些像数据源这样的配置需要加密的话,需要对配置中心进行加密处理. 下面使用对称性加密来加密配置,需要配置一个密钥,当然也可以使用RSA非对称性加密,但对称加密比较方便也够 ...

  9. PAT程序设计

    VS2013中自行对齐的快捷键操作: CTRL+K+F 1.定义二维数组 ]=]; 2.绝对值函数 int abs(int i) 返回整型参数i的绝对值 double cabs(struct comp ...

  10. 【目录】Asp.NETCore轻松学系列

    随笔分类 - Asp.NETCore轻松学系列 Asp.NETCore轻松学系列阅读指引目录 摘要: 耗时两个多月,坚持写这个入门系列文章,就是想给后来者更好更快的上手体验,这个系列可以说是从入门到进 ...