当前位置: 首页 > news >正文

题解:AtCoder AT_awc0003_d Consecutive Practice Days

本文分享的必刷题目是从蓝桥云课洛谷AcWing等知名刷题平台精心挑选而来,并结合各平台提供的算法标签和难度等级进行了系统分类。题目涵盖了从基础到进阶的多种算法和数据结构,旨在为不同阶段的编程学习者提供一条清晰、平稳的学习提升路径。

欢迎大家订阅我的专栏:算法题解:C++与Python实现!

附上汇总贴:算法竞赛备考冲刺必刷题(C++) | 汇总


【题目来源】

AtCoder:D - Consecutive Practice Days

【题目描述】

Takahashi is the manager of the track and field club, and he manages the practice records of the club members.
高桥是田径俱乐部的经理,负责管理俱乐部会员的练习记录。

The track and field club has a practice period ofN NNdays, and each day is numbered from1 11toN NN. On dayi ii( 1 ≤ i ≤ N ) (1 \leq i \leq N)(1iN), a positive integerA i A_iAirepresenting the “practice intensity” for that day is recorded.
田径俱乐部的练习期为N NN天,每天从1 11N NN编号。在第i ii天(1 ≤ i ≤ N 1 ≤ i ≤ N1iN),记录了一个表示当天"练习强度"的正整数A i A_iAi

According to the club’s rules, a period consisting ofr − l + 1 r - l + 1rl+1consecutive days from dayl llto dayr rris called an “achievement period” and is specially recognized if its length is at leastK KKdays and the total practice intensity within the period is at least the target valueM MM.
根据俱乐部规定,由第l ll天到第r rr天的连续r − l + 1 r-l+1rl+1天组成的时间段称为"成就期",如果其长度至少为K KK天且该时间段内的总练习强度至少达到目标值M MM,则会被特别认可。

Takahashi wants to find out how many achievement periods qualify for recognition.
高桥想知道有多少个成就期符合认可条件。

Specifically, find the number of integer pairs( l , r ) (l, r)(l,r)that satisfy all of the following conditions:
具体而言,求满足以下所有条件的整数对( l , r ) (l, r)(l,r)的数量:

  • 1 ≤ l ≤ r ≤ N 1 \leq l \leq r \leq N1lrN
  • r − l + 1 ≥ K r - l + 1 \geq Krl+1K(the length of the period is at leastK KKdays)
  • A l + A l + 1 + ⋯ + A r ≥ M A_l + A_{l+1} + \cdots + A_r \geq MAl+Al+1++ArM(the total practice intensity within the period is at leastM MM)

【输入】

N NNK KKM MM
A 1 A_1A1A 2 A_2A2⋯ \cdotsA N A_NAN

  • The first line contains an integerN NNrepresenting the number of days in the practice period, an integerK KKrepresenting the minimum length of a period, and an integerM MMrepresenting the target value for the total practice intensity, separated by spaces.
  • The second line containsN NNintegersA 1 , A 2 , … , A N A_1, A_2, \ldots, A_NA1,A2,,ANrepresenting the practice intensity for each day, separated by spaces.

【输出】

Print the number of integer pairs( l , r ) (l, r)(l,r)that satisfy the conditions, on a single line.

【输入样例】

5 2 10 3 5 4 2 6

【输出样例】

6

【解题思路】

【算法标签】

#前缀和#

【代码详解】

#include<bits/stdc++.h>usingnamespacestd;#defineintlonglongconstintN=200005;intn,k,m,ans;// n: 数组长度,k: 最小长度,m: 目标和inta[N],sa[N];// a: 原始数组,sa: 前缀和数组signedmain(){cin>>n>>k>>m;// 读入数组长度、最小长度和目标总和for(inti=1;i<=n;i++){cin>>a[i];// 读入数组元素sa[i]=sa[i-1]+a[i];// 计算前缀和}// 双指针算法,计算满足条件的子数组数量for(inti=1,j=1;i<=n;i++)// i为子数组左端点{// 移动右端点j,直到找到满足条件的子数组while(j<=n&&(sa[j]-sa[i-1]<m||j-i+1<k)){j++;}// 调试输出// cout << "i j " << i << " " << j << endl;// 如果找到了满足条件的子数组,计算以i为左端点的子数组个数// 从j到n的所有位置都可以作为右端点ans+=(n-j+1);}cout<<ans<<endl;// 输出满足条件的子数组个数return0;}

【运行结果】

5 2 10 3 5 4 2 6 6
http://www.jsqmd.com/news/694773/

相关文章:

  • NCMDump终极解密指南:3分钟解锁网易云音乐NCM加密格式
  • ArcGIS Pro连接Excel受阻?一文详解Microsoft驱动安装与静默部署
  • 从手机APP反推ESP32-C3蓝牙开发:看懂这些GATT数据,你就能改任何例程
  • Silvaco Athena实战:从零搭建一个0.8微米NMOS管,手把手教你调阈值电压和提取关键参数
  • 别再只复制Key了!高德地图Geocoder.getLocation本地调用完整避坑指南
  • YOLOv5训练避坑指南:batch-size设为8的倍数真的更快?聊聊数据对齐与显存‘浪费’的那些事
  • 【电液伺服执行器与PI控制器】带有PI控制器的电液伺服执行器的模拟研究(Simulink仿真实现)
  • 别再手动改PR了!教你写个ABAP报表,一键批量处理采购申请审批与信息更新
  • 分布式变分量子求解器在电力调度中的应用与优化
  • 从一次下载失败,聊聊TLS协议演进和那些被淘汰的‘老朋友’(附实战排查命令)
  • 如何从 iPhone 转移到 Realme:4 种简单方法
  • 保姆级拆解:用一张图看懂Wire Bonding的球焊与楔焊全流程(附常见缺陷图)
  • PyTorch音频处理实战:用torchaudio构建可微分的梅尔谱特征提取管道(适配GPU训练)
  • 反射半导体光放大器(RSOA)模型研究(Matlab代码实现)
  • FPGA加速TFHE全同态加密处理器的设计与优化
  • 移动端H5悬浮按钮避坑指南:React中实现拖拽吸附时,如何兼顾iOS Safari与微信浏览器?
  • 别光看强化学习!用PyQt5给YOLOv5检测结果做个实时可视化桌面助手
  • SAP ABAP表控件(Table Control)实战:从向导生成到手工打造可编辑数据表格
  • COMSOL和Matlab联仿报错?从‘mphload’到‘mphglobal’,这些函数调用细节和避坑点你注意了吗?
  • Wand-Enhancer:3分钟免费解锁WeMod专业版的神器!告别订阅烦恼
  • 保姆级教程:用Python和PyTorch搞定Semantic Drone Dataset的预处理与加载
  • Simulink参数管理进阶:手把手教你用Excel超链接处理数组型标定量(含二维数组案例)
  • 从AM到VSB:揭秘模拟调制技术的演进与实战解调
  • Python实战:用ffmpeg和moviepy合并B站下载的m4s音视频文件(附完整代码)
  • 免费音乐解锁工具:3分钟搞定QQ音乐、网易云加密文件解密
  • Real-Anime-Z参数详解:高度宽度1024×1024最佳实践,超分后细节保留率实测报告
  • 缝纫黑科技:泉州誉财对齐型旋转缝纫机专利抢先看
  • 终极指南:ncmdumpGUI如何快速解密网易云音乐NCM格式文件
  • 告别迷茫!ESP8266 WiFiClient库实战:从连接百度到收发数据的保姆级代码拆解
  • MARS算法原理与Python实现详解