PAT 1044 Shopping in Mars (25分)

ardupilot 为什么要采样四元数姿态控制 本节主要说明清楚ardupilot姿态控制为什么要用到四元数,欢迎批评指正!!! 阅读详情

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

 

题目大意:给定n个整数,让你在n个整数之间进行划分,使划分出的整数的和为m(从第i个数到第j个数),按i从递增的顺序输出每一种划分情况(格式为i-j),若不能无论如何划分出的整数的和也不为m,那么则输出整数和大于m并最接近m的那个整数和的划分。

 

分析:使用前缀和存储数据,这样可以省去循环求和所用时间。

前缀和指的是一个数组的某项下标之前(包括此项元素)的所有数组元素的和。

设前缀和数组为sum,那么第j+1 ~~ i个元素的和为sum[i]-sum[j+1],只需要判断sum[i]-sum[j+1]能否等于m即可

大体思路就是两层循环,外层循环控制i,内层循环控制j,枚举sum[i]-sum[j+1]即可。

由于最后要求是按i递增的顺序输出,那么每次枚举的时候,j不需要第一个元素开始找,只需要接着上次的位置找,也就是说j在整个过程中从0到了n,外层循环i从1到了n,因此时间复杂度为n+n,若每次j从0开始找,那么最差情况时间复杂度是n^2,这样测试点3会超时.

 

#include<bits/stdc++.h>
using namespace std;

int main()
{
    long long sum[100010];
    int n,m,x,i,j;
    cin>>n>>m;
    sum[0]=0;
    int f=0;
    long long minn=0x3f3f3f3f;
    j=0;
    for(i=1; i<=n; i++)
    {
        scanf("%d",&x);
        if(i==1)
        {
            sum[i]=x;
        }
        else
        {
            sum[i]=sum[i-1]+x;
        }
        /*这里退出循环意味着sum[i]-sum[j],那么i++后sum[i]增大了,sum[i]-sum[1....j-1]均会大            
          于m,所以只需要判断当前sum[i]-sum[j]是否大于m即可
        */
        while(sum[i]-sum[j]>m)   
        {
            minn=min(minn,sum[i]-sum[j]);
            j++;
        }
        if(sum[i]-sum[j]==m)
        {
            f=1;
            printf("%d-%d\n",j+1,i);
        }
    }
    if(f==0)
    {
        j=0;
        for(i=1; i<=n; i++)
        {
            
            while(sum[i]-sum[j]>minn)
            {
                j++;
            }
            if(sum[i]-sum[j]==minn)
            {
                printf("%d-%d\n",j+1,i);
            }
        }
    }


}

 

【知识图谱】实践篇——基于知识图谱的《红楼梦》人物关系可视化及问答系统实践:part6基于图谱的问答实现 前序文章:本部分则是整个项目的核心内容——基于图谱的问答。需要实现的是对于“简单问题”进行解析,然后从图谱数据库中查询与该人员及其相关人员信息,返回到前端并显示该人员的基本信息。除此之外,用户可以点击返回的结果人员节点,页面能够显示该用户的个人基本信息。那么现在就开始吧。根据源码实现问答形式,其实是比较简单的。原代码中使用ltp对问题进行分词,选取具有名次词性的关键词进行处理。 ltp配置稍许麻烦,我使用jieba分词工具对问题进行分词,然后再按照原文提取问句中人物及关系,然后构建cypher neo4j查 阅读详情

相关推荐

Basler相机和pylon viewer 的使用

文章目录1 线路连接2 安装和使用pylon 1 线路连接 电源线接到电源插座,网线接到电脑(电脑没有网口的话,可用转换器转到type-c接口)。 2 安装和使用pylon 参考 Basler pylon安装及对网口相机基本设置 相机和电脑以太网IP设置参考:balser相机IP设置 关于相机参数和pylon viewer的详细设置,可以参考pylon Viewer安装路径下的文档(Documentation) ...

Deephome的博客 1万+

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

weixin_45962741的博客 839

uniapp串口通信实战:从零搭建物联网设备控制App(附完整代码)

本文详细介绍了如何使用Uniapp实现串口通信,从零搭建物联网设备控制App。内容涵盖环境配置、串口初始化、数据收发、错误处理等核心功能,并提供了完整的代码示例,帮助开发者快速掌握跨平台串口通信技术,适用于智能农业、工业自动化等物联网场景。

weixin_30369041的博客 597

PAT甲级刷题记录——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 ...

一只奋斗中的本科生 447

【Cadence】PSS+PSTB/PAC仿真在开关电源稳定性分析中的关键应用

本文深入探讨了Cadence仿真工具中PSS+PSTB/PAC方法在开关电源稳定性分析中的关键应用。针对开关电源作为时变非线性系统的特性,传统STB仿真方法会失效。文章详细阐述了如何通过PSS分析找到周期稳态,并在此基础上利用PSTB或PAC进行精确的环路增益和小信号频响分析,为电源设计提供可靠的稳定性评估与补偿网络设计依据。

weixin_29276847的博客 524

别再为PT100测温发愁了!手把手教你用ADS1247+STM32搞定高精度温度采集(附完整代码)

本文详细介绍了如何使用ADS1247与STM32构建高精度PT100测温系统,涵盖硬件设计、SPI配置、温度计算及调试技巧。通过24位ADC和内置PGA,实现0.1℃级精度,适用于工业自动化和医疗设备。附完整代码和常见问题解决方案,助你快速掌握高精度温度采集技术。

weixin_42524703的博客 344

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

fairylyt的博客 506

基于STM32、HAL库的TLV320AIC3204IRHBR音频接口芯片驱动程序设计

* 开启所有数字部分 */#define TLV320AIC3204_REG_LEFT_HEADPHONE_OUT_VOLUME 0x0C /* 左声道耳机输出音量 */#define TLV320AIC3204_REG_DIGITAL_AUDIO_PATH_CONTROL 0x05 /* 数字音频路径控制 */#define TLV320AIC3204_REG_ANALOG_HP_FILTER_CONTROL 0x34 /* 模拟耳机滤波器控制 *//* 最大音量 */

corlin6688的博客 521

【题意分析】1044 Shopping in Mars (25 分)【滑动窗口】

立志用最少的代码做最高效的表达 PAT甲级最优题解——>传送门 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

weixin_43899069的博客 733

RK3588开发板NPU加速YOLO目标检测完整实战教程

🚀 RK3588开发板NPU加速YOLO目标检测实战 本文详细介绍了如何在瑞芯微RK3588开发板上利用内置NPU加速YOLO目标检测的全流程: 1️⃣ 环境准备:配置RKNPU2 SDK和aarch64交叉编译器,搭建Ubuntu开发环境 2️⃣ 模型转换:从ONNX格式转换为专用RKNN模型,适配NPU硬件特性 3️⃣ 性能对比:NPU推理将处理时间从833ms降至50ms以下,帧率提升16倍 4️⃣ 实战应用:支持图像/视频流/摄像头实时检测,提供完整编译和运行命令 关键优势:专为边缘AI设计的6T

FJN110的博客 931

【IxNetwork】史上最全IxNetwork测试仪使用精讲(一),图文教程超赞超详细!!!

IXIA使用的是Server-client模式,Server端在测试仪表的主机上,在开机后会随着主机内的操作系统的启动而自动启动,一般情况下不需要人为的手工启动。因此在通常不需要为主机配置专用的显示器和键盘。client端包括两个测试软件: Ixia Explorer和ScriptMate。这两个软件一般安装在测试用计算机上,在仪表自带的主机中也有这两个软件。根据测试项目的不同来选择使用不同的软件。Ixia Explorer主要提供数据流的测试,针对设备的功能进行测试;

风云说通信 2055

PAT (Advanced Level) Practice 1044 Shopping in Mars (前缀和 二分)

PAT (Advanced Level) Practice 1044 Shopping in Mars (前缀和 二分)

我们都有光明的未来 381

【ComfyUI】Flux 图像扩展

本文介绍了一个基于Flux扩图技术的ComfyUI工作流,能够智能扩展图像尺寸并保持内容连贯性。该流程通过扩散模型与条件编码的结合,对图像边缘进行语义填充生成,适用于艺术创作、海报设计等场景。工作流包含图像加载、外扩填充、文本编码、扩散采样等核心节点,采用Flux1-fill-dev等专业模型确保生成质量。演示效果显示,系统能自然延伸原图内容,保持风格统一。该技术可广泛应用于插画扩展、摄影增强、游戏场景构建等领域,显著提升创意工作效率。

Mr数据杨 928

PAT甲级 1044 Shopping in Mars (25分)

PAT甲级 1044 Shopping in Mars (25分) 这种狗屎的,只卡一个点,又不知道这个点是什么数据什么情况,还要自己取揣测,真难受! 本人代码附上 采用前后指针法,暴力解题! #include <iostream> #include <map> #include <vector> #include <algorithm> #include <cstring> #include <string> using names

一棵小白菜的博客 243

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

PAT 甲级 1044 Shopping in Mars (25 分)

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 cha...

小渣渣的博客 262

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

PAT A 甲级 1044 Shopping in Mars (25分)

1044 Shopping in Mars (25分) 吐槽 怎么又是火星人?你们比小明还事多。 思路 逻辑蛮简单的,成块找可行的组合就行。用遍历的话,一个循环遍历头,里面套一个循环遍历尾,判断这个头尾能不能行,复杂度O(n2)。 然而超时。 不过解决方法也还算简单。先找到以1为头的第一个可行组合,然后移到2为头时判断去掉1是否还够大,够则成为新方法,不够则把尾往后移到够大,然后判断和已有的最小支...

虚境的博客 397

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

pat-1044 Shopping in Mars (25 分)

1044Shopping in Mars(25分) 思路: 这道题暴力肯定不行,10^5绝对超限,但是可以骗分(*^▽^*), 真正的做法是two pointers的做法, 因为求区间【l,r】内的和,所以用p代表左指针,q代表右指针,然后记录区间【p,q】内的和, 每次求出这个区间内大于等于m的值,然后更新l,再查找下一个大于等于m的r。 注意:考虑最大值比10^5*10...

积少成多,水滴石穿 207

1044 Shopping in Mars (25 分)-PAT甲级

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

神笔码农.的博客 738

【PAT甲级】1044 Shopping in Mars (25分)

解题过程的小记录,如有错误欢迎指出。 难度:? 小导航~题目分析注意点我的解题过程思路bug代码dalao的代码借鉴点 题目分析 注意点 我的解题过程 思路 bug 代码 dalao的代码 全部代码因版权原因不放出来,大家可以自行去柳神博客购买或者参考晴神的上机笔记~ 借鉴点 ...

SpicyChicken的自我修养 224

【PAT】A1044 Shopping in Mars (25分)

做题链接 思路: 对这个序列设置两个标志位,left与right 用twopoint的思想,使整体复杂度为O(n). AC代码 #include <cstdio> #include <vector> using namespace std; const int maxn = 100010; const int INF = 100000010; int n,m, a[maxn], sum = 0, min_sum = INF; int left = 1 , right = 1; //

jetpack_的博客 202

PAT-A 1044 Shopping in Mars (25 分)

思路:使用数组diamond来保存每颗钻石的价值。设置一个数组sum,sum[i]表示从第0颗钻石到第i颗钻石的价值的和,显然sum严格递增。这样从第 i 颗钻石到第 j 颗钻石之间的钻石的价值为sum[ j ] - sum[ i ] + diamon[ i ]。比如,第2颗钻石到第5颗钻石累计的价值为:sum[5] - sum[2] + diamond[2] = 21 - 6 + 1 = 16.这里从0开始计数,只在处理输出的时候将 下标加1即可。 diamond: 3     2     1    

qq_38127801的博客 248

PAT甲级-1044 Shopping in Mars (25分)

点击链接PAT甲级-AC全解汇总 题目: 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...

卷子的博客 321

PAT A1044 Shopping in Mars (25 分) 二分

题目大意:给出一个正数序列,在当中找到满足这样条件的连续子串:子串的和大于等于给定值。输出具有最小的和的子串的开始和结束位置的下标(从1开始计),如果这样的子串有两个以上,按照开始位置从小到大,输出所有子串的开始和结束位置。 一开始直接暴力遍历所有子串,并维护最小的子串和。但这样还是有两个测试点超时,开始位置肯定是需要遍历序列的每个元素的,那么减少耗时的主要方法就是如何查找结束位置,问题就转换为...

HackingGate的博客 444

PAT A 1044 Shopping in Mars (25 分)

一、思路 即求:**相加为大于M的最小和的所有连续子串**,且要求复杂度o(n),否则会出现测试点超时; 思路: 一趟遍历无回溯,l和r表示当前子串左右边界,sum为子串和,min为当前最小子串和; 若sum>=m,若同时sum==min,则{l, r}是一组解,若sum<min,则应更新min并清空之前的解(已不是最优解); 然后l左边界进位; 若sum<m,则说明子串太短,应有r右边界进位;

Siumai's Blog 193

PAT 甲级 1044 Shopping in Mars (25分)

题目 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

qq_41001689的博客 175

PAT甲级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 ...

Imagirl1的博客 462
上一篇: 1153 Decode Registration Card of PAT (25分)
下一篇: PAT 1007 Maximum Subsequence Sum (25分)
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值