1044 Shopping in Mars (25 分)

96 篇文章 7 订阅
49 篇文章 1 订阅

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:

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).
Cut before 5 or after 6, and take off the diamonds from the position 4 to 6 (with values 5+4+6=15).
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

这个题就是求连续子序列和,然后输出满足条件的子序列,如果没有,就输出子序列和值最接近目的金额的子序列。需要注意的是这道题对时间复杂度的要求很高,需要用到二分查找。

#include <bits/stdc++.h>
using namespace std;
int INF=0x3f3f3f3f;
int main()
{
    int n,m;
    int a[100010];
    a[0]=0;
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        a[i]+=a[i-1];
    }
    for(int i=1;i<=n;i++){
        int j=upper_bound(a+i,a+n+1,a[i-1]+m)-a;
        if(a[j-1]-a[i-1]==m){
            INF=m;
            break;
        }
        else if(j<=n&&a[j]-a[i-1]<INF)INF=a[j]-a[i-1];
    }
    for(int i=1;i<=n;i++){
        int j=upper_bound(a+i,a+n+1,a[i-1]+INF)-a;
        if(a[j-1]-a[i-1]==INF){
            cout<<i<<'-'<<j-1<<endl;
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这是一本零基础就能读懂的算法书籍,读者不需要因为自己没有语言基础而畏惧。书籍的第2章便是一个C语言的入门教程,内容非常易懂,并且十实用,阅读完这章就可以对本书需要的C语言基础有一个较好的掌握。本书已经覆盖了大部基础经典算法,不仅可以作为考研机试和PAT的学习教材,对其他的一些算法考试(例如CCF的CSP考试)或者考研初试的数据结构科目的学习和理解也很有帮助,甚至仅仅想学习经典算法的读者也能从本书中学到许多知识,本书还有配套的《算法笔记上机训练实战指南》本书的作者是同样经历过考研机试和各类算法考试的专家型学长,知晓这类考试中的痛点,以及考生在学习算法时容易产生困惑的地方,因此可以把本书看作是学长为你奉献的满满的经验干货,这是最有价值的东西。本书的最个试印版本献给了浙大考研学子,并令当年的浙大考研机试平均增加了十多,收获了考生的大量好评。但作者并没有止步于此,经过了半年多时间的内容完善和补充之后,新的版本在新一年的考研机试中再次获得了考生的一致赞美。最后,在经过精心整理之后,书籍终于定稿,并编撰成书。我们知道,纸质书籍的一个弱点就在于不能像软件一样随时更新内容,但本书采用了与二维码相结合的方式,使得本书变为能够随时更新内容的书籍,读者也可以随时从二 维码中找到勘误。这种作者和读者能够相互沟通的方式让书籍变“活”了,也能够帮助提升读者对知识的理解。 本书内容包括:C/C++快速入门、入门模拟、算法初步、数学问题、C++标准模板库(STL)、数据结构专题(两章)、搜索专题、图算法专题、动态规划专题、字符串专题、专题扩展。书中每小节的末尾均印有二维码,用以实时更新或补充书籍的内容及发布本书的勘误。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值