1044 Shopping in Mars (25 point(s))

李大勇matlab密码,基于MatlabSimulink的太阳能热水系统仿真研究 沈超.doc 基于MatlabSimulink的太阳能热水系统仿真研究 沈超中国工程热物理学会 工程热力学与能源利用 编号:141090基于Matlab/Simulink的太阳能热水系统仿真研究沈 超1 1 余 鹏1 李洪欣1 魏新利2郑州大学土木工程学院,郑州 450001郑州大学化工与能源学院,郑州 45... 阅读详情

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

题意分析:给出一个数组,使得区间和恰好等于某个给定的数值。如果不存在,则找出区间和大于该区间和但是尽可能小的。

方法1:二分查找

根据给定数组,求出前i项和数组(一定是不递减数组),然后使用二分查找。注意寻找的是第一个大于或者等于key的数值。

易错点:在查询过程find()中,由于数组sum[]可能不存在大于sum[i]+M(会返回最后一个元素的下标N),由此main()中必须要重新确定找到的这个下标是满足sum[j]-sum[i]>=M。也可以改进在find()中去判断,如果不满足则返回-1。

#include<iostream>
#include<vector>
#include<cstring>
#define MIN 0x3f3f3f3f
using namespace std; 
const int MAX = 1e5+7;
int N,M;
int a[MAX]={0};
int sum[MAX]={0};//前i个元素的和 
void init(){
	for(int i=1;i<=N;i++) sum[i]=sum[i-1]+a[i];
}
struct Pair{
	int a;int b;
	Pair(int x,int y):a(x),b(y){};
};
int find(int sum[],int left,int right,int key){//二分查找 
	int mid;
	while(left<right){
		mid = left+(right-left)/2;
		if(sum[mid]>=key) right = mid;
		else left=mid+1;
	}
	return left;
}
vector<Pair> v;
int main(void){
	cin>>N>>M;//数组元素个数、目标金额 
	for(int i=1;i<=N;i++) cin>>a[i];
	init(); v.clear();
	int min = MIN;
	for(int i=0;i<N;i++){
		int j = find(sum,i+1,N,sum[i]+M);
		if(sum[j]-sum[i]>=M&&sum[j]-sum[i]<min){
			min = sum[j]-sum[i];
			v.clear();
			v.push_back(Pair(i+1,j));
		}
		else if(sum[j]-sum[i]>=M&&sum[j]-sum[i]==min){
			v.push_back(Pair(i+1,j));
		}
	}
	for(int i=0;i<v.size();i++) cout<<v[i].a<<"-"<<v[i].b<<endl; 
	return 0; 
}

方法2:双指针

设定两个分别指向区间首尾元素的指针i和j,用sum表示该区间和。

当i确定时,如果sum<M,则说你区间还不够长,因此应当向右扩大范围;如果sum>=M,则说明这是在i确定的情况下,满足基本要求(sum>=M且最接近M),再进一步判断,然后i自增。直到i和j都是N了。

心得:变量命名和含义一定要符合自己的编码习惯。

#include<iostream>
#include<vector>
#define MIN 0x3f3f3f3f
using namespace std; 
const int MAX = 1e5+7;
int N,M;
int a[MAX]={0};
struct Pair{
	int a;int b;
	Pair(int x,int y):a(x),b(y){};
};
vector<Pair> v;
int main(void){
	cin>>N>>M;//数组元素个数、目标金额 
	for(int i=1;i<=N;i++) cin>>a[i];
	int sum = a[1];//当前元素i(含)到j的和(含) 
	int min = MIN;
	for(int i=1,j=1;i<=N&&j<=N;){
		if(sum<M){
			j++;
			sum+=a[j];
		}
		else{
			if(sum<min){
				min = sum;
				v.clear();
				v.push_back(Pair(i,j));
			}
			else if(sum==min){
				v.push_back(Pair(i,j));
			}
			sum-=a[i]; 
			i++;
		}
	} 
	for(int i=0;i<v.size();i++) cout<<v[i].a<<"-"<<v[i].b<<endl; 
	return 0; 	
} 

参考链接:https://blog.youkuaiyun.com/richenyunqi/article/details/79323796

YOLOv8【第十章:多任务扩展深度篇·第16节】车道线检测——基于 YOLOv8-Seg 的车道线分割与拟合! 🏆 本文收录于 《YOLOv8实战:从入门到深度优化》 专栏。该专栏系统复现并梳理全网各类 YOLOv8 改进与实战案例(当前已覆盖分类 / 检测 / 分割 / 追踪 / 关键点 / OBB 检测等方向),坚持持续更新 + 深度解析,质量分长期稳定在 97 分以上,可视为当前市面上 覆盖较全、更新较快、实战导向极强 的 YOLO 改进系列内容之一。 阅读详情

相关推荐

在PLC中将十六进制数据转换为浮点数(通常遵循IEEE 754标准)需要理解数据存储格式,并按以下步骤操作:

通过以上步骤,您可以在PLC中实现十六进制到浮点数的转换。T DBD20 // 将数据传送到浮点数变量(如DB1.DBD20)指数位(8位):实际指数 = 存储值 - 127(偏移量)。:必须确认PLC的存储顺序(大端或小端),否则结果错误。将两个16位寄存器合并为32位数据,需注意。尾数位(23位):隐含前导1,实际值为。:确保32位数据在寄存器中连续且对齐。符号位(1位):0表示正,1表示负。:高位寄存器在前(如西门子PLC)。:低位寄存器在前(如三菱FX系列)。,误差因截断导致)。

K11mvp的博客 4419

1044. Shopping in Mars (25)-PAT甲级真题

这题暴力用前缀和搜会超时,有两种解法,二分查找和滑动窗口。

m0_61868996的博客 361

使用BK3432芯片进行蓝牙开发的详细指南:编程手册与实际应用

BK3432是一款蓝牙4.2双模式芯片,支持蓝牙低功耗(BLE)和经典蓝牙(BR/EDR)模式。它集成了高性能的RF收发器、基带处理器、ARM9E内核、丰富的功能外设单元,并支持BLE应用的可编程协议和概要文件。蓝牙版本:4.2数据速率:最高支持2Mbps内核:ARM9ERF收发器:高性能、低功耗外设单元:包括UART、SPI、I2C、GPIO等功耗:超低功耗设计,适合电池供电的应用。

m0_57781768的博客 4453

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的博客 840

工业场景实战:基于YOLO与多格式标注的传送带异物检测数据集构建指南

本文详细介绍了基于YOLO与多格式标注的传送带异物检测数据集构建方法,涵盖数据采集、标注技巧(YOLO、COCO、VOC格式)、数据增强策略及实战案例。通过工业级解决方案,有效提升异物检测准确率与效率,适用于各类工业场景。

weixin_29168393的博客 464

基于Zemax的干涉仪仿真——Fizeau球面干涉仪

最近在用Zemax仿真干涉仪,在这之前查资料都是关于一些平面干涉仪的仿真,很少有关于Fizeau型的,尤其是球面干涉仪的仿真,看到很多关于这方面的问题回复都说利用多重结构仿照Zemax马赫-泽德干涉仪去做,但是也没有具体的过程,因此在这里分享一下我仿真的干涉仪步骤方法,具体是利用多重结构完成的仿真。

weixin_46657737的博客 6410

1044 Shopping in Mars(25分)

如果大于等于M,就判断这个和最小能支付的值m之间的关系,若小于m,就将map清空,然后插入;例如,如果我们有一个钻石链条,上面有 8 颗钻石,价值分别为 3,2,1,5,4,6,8,7元,而我们需要支付的价格为 15 元。在 4 和 6 之间剪断链条,然后取走前 55 颗钻石,价值为 3+2+1+5+4=15。在 5 之前或 6 之后剪断链条,然后取走第 4∼6 颗钻石,价值为 5+4+6=15。付款时,只能在链条的某一位置上切割一次,然后一些钻石就会一个接着一个的从链条上脱落。

a131529的博客 199

在 SAP ABAP 生态中偏爱面向对象,而不是过程式堆叠

本文探讨了ABAP开发中过程式与面向对象编程的共存现状及优化方向。文章指出,虽然过程式编程在ABAP中仍广泛存在,但随着业务复杂度提升,面向对象编程在可维护性、可测试性和重构便利性方面展现出明显优势。作者建议将过程式代码作为"适配层",仅负责输入输出转换,而将核心业务逻辑封装在类中。文章通过实际案例展示了这种架构如何提高交付效率,并提供了将Function Module转化为"薄包装"的具体实现方案,强调面向对象设计在SAP Gateway和RAP等现代架构中的重要性

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 137

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

手把手教你学Simulink——基于经典PID控制的电机系统场景实例:永磁同步电机(PMSM) PID转速控制仿真

*永磁同步电机(PMSM)因其高效、高功率密度和良好的动态响应特性,在现代工业自动化、电动汽车驱动系统等领域得到了广泛应用。为了实现对PMSM的速度精确控制,通常采用矢量控制(Field Oriented Control, FOC)**策略,其中PID控制器是实现速度环和电流环调节的关键组件。本教程将带你使用构建一个基于PID控制的永磁同步电机转速控制系统仿真模型PMSM电机建模矢量控制(FOC)策略实现PID控制器设计系统仿真与调试控制效果分析本文介绍了如何使用构建一个。

MHD0815的博客 6952

使用ChatGPT工具阅读文献的实战教程

本文介绍核心内容为使用ChatGPT工具阅读文献,希望对学习和使用ChatGPT的同学们有所帮助。为了兼顾质量和速度,本专栏的更新频率为一周一到两更。 文章目录 1. 前言 2. 实战教程 2.1 下载工具并登录 2.2 提取文献中的关键信息

herosunly的博客 15万+

浙大PAT_1044:Shopping in Mars 解题报告

题目大意:给出长度为n的一个整数序列,找出其中所有和为m的子序列(连续的),如果没有任何子序列的和等于m,那么找出大于m的最小和。如长度为8的序列3,2,1,5,4,6,8,7,找出和为15的子序列有三个:1~5,4~6,7~8。 这道题思路并不难,一般都能想到通过遍历来解决,但是如果不注意细节是很难拿到满分的。 以下是大家通常能够写出的解决方法: #include using nam

跳出温水的青蛙 2179

区块链技术概述:从比特币到Web3.0

区块链技术正重塑数字世界的信任机制。本文系统梳理了区块链的发展路径:从比特币的PoW共识和UTXO模型,到以太坊的智能合约和账户体系,直至Web3.0的去中心化互联网愿景。文章详细解析了区块链的核心特性(去中心化、不可篡改等)和数据结构,通过流程图展示了比特币交易生命周期和智能合约执行流程,并对比了Web2.0与Web3.0的关键差异。最后,用Python代码实现了包含区块类、交易处理和挖矿机制的简易区块链系统,为理解区块链底层原理提供了实践参考。

闲人编程的博客 457

1044 Shopping in Mars (25 point(s))

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

ayouayouwei 242

STM32F407中文手册

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

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

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

学习为主,偶尔扯淡 236

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

1044 Shopping in Mars (25point(s))

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

佳迪斯的博客 124

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

1044 Shopping in Mars (25 point(s)) - C语言 PAT 甲级

设计思路: 思路 2(二分查找)思路 1(窗口滑动)

有三只土豆 291

PAT A1044 Shopping in Mars (25point(s))

题目链接 本题我先用最直接的遍历法对每一个数遍历寻找满足条件的子串,代码如下: #include<cstdio> #include<algorithm> using namespace std; int chain[100010]; int main(){ int n,m; scanf("%d%d",&n,&m); for(int i...

皮皮瓜的博客 218

1044 Shopping in Mars (25 point(s))__二分(需要回看)

题目大意: 题目给出一串正整数num[1-n],和一个既定和M,让你输出数组中连续的、和等于M 的部分数字。 如果没有部分和恰好等于M的连续数字,则输出和大于M且与M最接近的数字。 如果答案不唯一,则按照左端升序输出。 注意: 双层枚举会超时,用二分。 因为求的是部分和,所以直接存储sum比较方便。如:num[i]到num[j]的部分和可以写成 sum[j]-sum[i-1]. 用二分返回的...

a7701935的博客 198

PAT(甲级)1044 Shopping in Mars (25point(s))

题目 题目链接 思路 题目大意:找出数组片段和大于等于目标值的片段; 因为题目要求如果没有片段和等于目标值的话输出大于目标值的最小片段,所以第一步要计算出与目标值相差最小的near是多少; 枚举i 从1到n,用二分找到大于等于 sum[i - 1] + m 的下标 j,如果某一个i满足 sum[i - 1] + m = sum[j],则说明可以找到差值为m的位置,直接中断; 找到这个最小差值后,再一次二分查找一次,找到 大于等于 sum[i - 1] + near 的第一个位置,如果正好等于的话,就输出 i

hjyp的博客 152

1044. Shopping in Mars (25)

题目链接:http://www.patest.cn/contests/pat-a-practise/1044 题目: 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Sho

Apie_CZX的专栏 2675

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

小木匠QvQ 341

1044. Shopping in Mars (25) 尺取法

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

test 364

1044. Shopping in Mars (25)解题报告

#define _CRT_SECURE_NO_WARNINGS #include <cstdio> #include <cstdlib> #include <vector> #include <climits> #include <algorithm> using namespace std;struct solution { int begin, end; };void find(vect

信道者 388

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

1044Shopping 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 makin...

蔡军帅 719

*PAT_甲级_1044 Shopping in Mars (25分) (C++)【双指针】

1,题目描述 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 题目大意 在火星...

&再见萤火虫&的博客 587
上一篇: 1108 Finding Average (20 point(s))
下一篇: 1075 PAT Judge (25 point(s))
coderwait
博客等级 码龄9年 20粉丝 210原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值