【PAT】A1044. Shopping in Mars(二分查找)

PAT-A1044题解 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... 阅读详情

【PAT】A1044. Shopping in Mars(二分查找)

@(PAT)

链接:https://www.patest.cn/contests/pat-a-practise/1044

思路:
1. 题目的意思总结来就是:在一个序列中,找到一个和比目标数大,相差最小的子序列。
2. 该题目中,可以记录累加和,然后子序列的和用减法得出。比如:
序列nums:4, 3, 2, 1, 5
累加和sums:0, 4, 7, 9, 10, 15
子序列[1, 2]的和:sums[2]- sums[1- 1]= 7- 0= 7
子序列[i, j]的和:sums[j]- sums[i- 1]

3. 找到每个子序列的方法是,遍历sums,固定子序列的i,然后通过二分查找的方法找到j。
4. 找到j之后,记录最小的差值min,然后将i对应的j记录进res。
5. 最后通过res,判断找到符合差值最小的输出。

My AC code:

#define _CRT_SECURE_NO_DEPRECATE

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <map>

#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>

#define INF 999999999

using namespace std;

int main()
{
    int n, m;
    scanf("%d%d", &n, &m);

    vector<int> nums(n);
    vector<int> sums(n + 1);
    vector<int> res(n + 1);
    int sum = 0;
    sums[0] = 0;
    for (int i = 0; i < n; i++) {
        scanf("%d", &nums[i]);
        sum += nums[i];
        sums[i+ 1] = sum;
    }
    int min = INF;
    for (int i = 1; i <= n; i++) {
        int left = i;
        int right = n;
        while (left < right) {
            int mid = (left + right) / 2;
            int t = sums[mid] - sums[i- 1];
            if (t >= m) {
                right = mid;
            }
            else {
                left = mid+ 1;
            }
        }
        int temp = sums[right] - sums[i - 1];
        if (temp >= m) {
            if (temp < min) {
                min = temp;
            }
            res[i] = right;
        }

    }
    for (int i = 1; i <= n; i++) {
        if (sums[res[i]] - sums[i - 1] == min) {
            printf("%d-%d\n", i, res[i]);
        }
    }
    return 0;
}
PAT甲级 1044 Shopping in Mars 【代码】PAT 1044 Shopping in Mars。 阅读详情

相关推荐

车载测试实战:adb logcat日志高效抓取与问题定位技巧

本文详细介绍了车载测试中adb logcat日志的高效抓取与问题定位技巧。作为车载测试的“第一现场”,adb logcat能实时、精准地抓取系统与应用日志,是定位应用崩溃、ANR及功能异常等问题的关键工具。文章从环境搭建、核心命令组合拳到实战分析与自动化脚本,提供了完整的操作指南,帮助测试人员快速掌握这一必备技能,大幅提升问题排查效率。

ii567的博客 1037

PAT A1044 二分查找

1044. Shopping in Mars (25) 题意给出一个序列和一个定值M,要求找出和为M的子串,如果没有这样的子串,那么要求找出和大于M且和最小的子串标准输入 16 15 3 2 1 5 4 6 8 7 16 10 15 11 9 12 14 13 标准输出 1-5 4-6 7-8 11-11 思路 其实这是一个求子列和的问题,不过有一些限制,不必列

River_Lethe的博客 735

RK3566基于Buildroot定制QT开发环境的完整指南

本文详细介绍了如何在RK3566开发板上使用Buildroot定制QT开发环境的完整流程。从基础环境搭建、Buildroot初始配置到QT模块选择与编译验证,提供了实战经验和常见问题解决方案,帮助开发者高效完成嵌入式Linux下的QT开发环境部署。

weixin_26804345的博客 358

PAT-A 1044. Shopping in Mars (25)

不知道是不是属于动态规划的题解……

Tomorrow Never Knows 629

360可视门铃双摄版数据恢复实战:BIN文件解析与视频重组

本文详细解析了360可视门铃双摄版BIN文件的数据恢复过程,包括文件结构分析、双摄像头数据存储机制及视频重组技术。通过Python开发环境和FFmpeg工具链,成功实现了H.265视频流的解析与重组,为智能家居设备数据恢复提供了实用解决方案。

weixin_29196891的博客 908

PyTorch版Unet训练后别只看loss:手把手教你计算并可视化多类别语义分割的mIoU(附完整代码)

本文详细介绍了在PyTorch中使用Unet模型进行多类别语义分割后,如何计算和可视化mIoU(mean Intersection over Union)以全面评估模型性能。通过完整的代码示例和实用技巧,帮助开发者从loss优化转向更精准的区域分割评估,特别适用于医学图像分析等关键场景。

weixin_30906185的博客 435

PAT甲级 1044 Shopping in Mars (25分)

1044 Shopping in Mars (25分) 题目链接:PAT A 1044 题目大意:输入第一行给出两个数n和k,接下来给出n个正数,要求找出序列中所有和等于k的连续片段。如果不存在这样的片段,则输出数的和大于k的最小值的连续片段(例如样例2中连续片段的和大于k的第一个值是16)。如果有多个这样的连续片段,要按照左面下标递增的顺序输出。 思路分析:有难度的一道题,注意到题中序列的和...

weixin_43684054的博客 211

材料仿真软件:Materials Studio_(14).电学性质计算

设置计算参数,包括 k 点网格、交换相关泛函等。

kkchenjj的博客 1863

PAT 1044 1044 Shopping in Mars (前缀和+二分)

【代码】PAT 1044 1044 Shopping in Mars。

weixin_50679551的博客 924

面试问题总结——关于YOLO系列(三)

接着上篇的的YOLO系列面试问题总结,加油,冲! 三.YOLOv3 1.YOLOv3主干网络BackBone——Darknet-53网络结构(因为有53个卷积层) (原论文中Darknet53的尺寸是在图片分类训练集上训练的,所以输入的图像尺寸是256x256,而在YOLOv3中输入尺寸是416×416)   ①上图中,Top-1 Accuracy和Top-5 Accuracy是指什么呢?区别在哪呢?   我们知道ImageNet有大概1000个分类,而模型预测某张图片时,会给出1000个按概率从高.

boss-dog 1万+

汇川Easy系列PLC系统PID指令编程应用(增量型)

增量式PIDS7-1200/1500增量式PID(输出归一化、支持PWM输出)_西门子1200pid算法公式-优快云博客文章浏览阅读1k次。本文介绍了S7-1200/1500 PLC中增量式PID算法的应用,包括输出归一化和PWM输出。通过详细解析,阐述了PID输出如何限制在0.0-1.0之间,并且如何将PID输出与PWM占空比关联,适用于温度等控制场景。

RXXW_Dor的博客 1422

【PAT】A1044 Shopping in Mars【二分查找】

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

elegant coder 903

166、Sim2Real中的强化学习:域随机化与鲁棒策略

第二,训练时监控每个随机化维度对策略梯度的贡献,如果某个维度对loss的方差贡献极小,说明策略已经忽略它了,要么加大范围,要么删掉。第四,如果条件允许,用真实数据来校准随机化分布——采集真机上的传感器数据,拟合出参数分布,再把这个分布作为随机化的采样依据,这比拍脑袋定范围靠谱得多。动态随机化是很多人忽略的维度。域随机化的核心思想说白了就一句话:别去精确建模现实,而是把仿真环境本身变成一个随机分布的集合,让策略在“各种乱七八糟的物理参数”里都见过世面,这样到了真机上,不管现实落在哪个参数区间,策略都能兜住。

sundong_81的博客 27

PAT-A-1044 Shopping in Mars 【二分查找】 【前缀和】

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

gdc6916的博客 2万+

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

PAT A1044:Shopping in Mars之二分查找

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

lilian的博客 185

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

PAT A1044 Shopping in Mars (25 分) 二分

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

HackingGate的博客 444

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

[PAT A1044]Shopping in Mars

[PAT A1044]Shopping in Mars 题目描述 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$). W...

欢迎来到我的编程小屋 651

PAT A1044 Shopping in Mars

PAT A1044 Shopping in Mars 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 word meaning...

qq_42347617的博客 193

PAT甲级-1044 Shopping in Mars

PAT甲级-1044 Shopping in Mars C++

weixin_74092648的博客 892

PAT 甲级 1044  Shopping in Mars

1044 Shopping in Mars (25 point(s)) 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, ...

漫浸天空的雨色的备忘录~ 657

PAT A1044 shopping in mars

1044 Shopping in Mars (25 point(s)) 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 s

m0_51711089的博客 568

PAT 1044 Shopping in Mars(二分查找)

一直以来以为自己算法虽然不行,总不至于连二分都不会了。今天看操作系统,看到文件系统那,看到二分相关的知识点,偶然想练练,没想到一练习自己吓一跳,我还真忘记怎么写二分了…但看了看还是很快知道了。 (吐槽一下,这题要扫两边,开始只扫了一遍,看二分去了忘记题目有存在不匹配的情况,牛客网那倒是过了,因为它那根本就没有不匹配的情况…但也不是啥大问题,想清楚就很好写了) 链接:PAT 1044 Shopping in Mars 题目 Shopping in Mars is quite a different exper

RobinWitch的博客 514

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

R11happy的博客 723

【PAT1044】Shopping in Mars 二分法

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

天堂人间 2993

A1044 PAT乙级JAVA题解 Shopping in Mars

【代码】A1044 PAT乙级JAVA题解 Shopping in Mars。

qq_49695680的博客 925
上一篇: 【SSAD】Lesson 8 Homework
下一篇: 【SSAD】Lesson 9 Homework
timsooo
博客等级 码龄11年 16粉丝 61原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值