2024蓝桥杯省赛保奖突击班-Day2-前缀和、差分、尺取_笔记_练习题解
3月25日-课堂笔记
- 前缀和预处理 O ( n ) \mathcal{O}(n) O(n)
s[1] = a[1];
for(int i = 2; i <= n; ++ i)s[i] = s[i - 1] + a[i];
- 利用前缀和查询区间和 O ( 1 ) O(1) O(1)
long long calc(int l, int r) {return l == 1 ? s[r] : s[r] - s[l - 1];
}
- 差分序列的求法
c[1] = a[1];
for(int i = 2; i <= n; ++ i) c[i] = a[i] - a[i - 1];
- 原序列上区间[l, r]修改相当于差分序列上两个单点修改
c[l] += v;
c[r + 1] -= v;
- 区间加等差数列对应二次差分序列上常数个单点修改
- 一般等差数列: …, 0 , a , a + d , a + 2 d , … , a + ( k − 1 ) d , 0 , 0 , … 0, a, a+d, a+2 d, \ldots, a+(k-1) d, 0,0, \ldots 0,a,a+d,a+2d,…,a+(k−1)d,0,0,…
- 一次差分之后: …, 0 , a , d , d , … , d , − a − ( k − 1 ) d , 0 , … 0, a, d, d, \ldots, d,-a-(k-1) d, 0, \ldots 0,a,d,d,…,d,−a−(k−1)d,0,…
- 二次差分之后: …, 0 , a , d − a , 0 , … , 0 , − a − k d , a + ( k − 1 ) d , … 0, a, d-a, 0, \ldots, 0,-a-k d, a+(k-1) d, \ldots 0,a,d−a,0,…,0,−a−kd,a+(k−1)d,…
- 尺取法
for(int l = 1, r = 0; r <= n; ++ l) {while(num < m && r < n) ...;if(...) break;...
}
- 双栈法维护尺取
插入/删除 -> 插入/合并
练习题解
B3612 求区间和
题目链接:B3612
参考思路
前缀和模板题。
C++参考代码
#include <iostream>
#include <vector>using namespace std;int main() {int n, m;cin >> n;vector<int> a(n), prefix_sum(n + 1, 0);for (int i = 0; i < n; ++i)cin >> a[i];for (int i = 1; i <= n; ++i)prefix_sum[i] = prefix_sum[i - 1] + a[i - 1];cin >> m;while (m--) {int l, r;cin >> l >> r;cout << prefix_sum[r] - prefix_sum[l - 1] << endl;}return 0;
}
Java参考代码
import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);int n = scanner.nextInt();int[] a = new int[n];int[] prefixSum = new int[n + 1];for (int i = 0; i < n; i++)a[i] = scanner.nextInt();for (int i = 1; i <= n; i++)prefixSum[i] = prefixSum[i - 1] + a[i - 1];int m = scanner.nextInt();while (m-- > 0) {int l = scanner.nextInt();int r = scanner.nextInt();System.out.println(prefixSum[r] - prefixSum[l - 1]);}}
}
Python参考代码
n = int(input())
a = list(map(int, input().split()))prefix_sum = [0] * (n + 1)
for i in range(1, n + 1):prefix_sum[i] = prefix_sum[i - 1] + a[i - 1]m = int(input())
for _ in range(m):l, r = map(int, input().split())print(prefix_sum[r] - prefix_sum[l - 1])
P2367 语文成绩
题目链接:P2367
参考思路
C++参考代码
#include <iostream>
#include <vector>using namespace std;int main() {int n, p;cin >> n >> p;vector<int> scores(n), diff(n, 0);for (int i = 0; i < n; ++i)cin >> scores[i];while (p--) {int x, y, z;cin >> x >> y >> z;diff[x - 1] += z;if(y < n) diff[y] -= z;}for (int i = 1; i < n; ++i)diff[i] += diff[i - 1];int min_score = scores[0] + diff[0];for (int i = 1; i < n; ++i) {scores[i] += diff[i];min_score = min(min_score, scores[i]);}cout << min_score << endl;return 0;
}
Java参考代码
import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);int n = scanner.nextInt();int p = scanner.nextInt();int[] scores = new int[n];int[] diff = new int[n];for (int i = 0; i < n; i++)scores[i] = scanner.nextInt();while (p-- > 0) {int x = scanner.nextInt();int y = scanner.nextInt();int z = scanner.nextInt();diff[x - 1] += z;if (y < n)diff[y] -= z;}int minScore = scores[0] + diff[0];for (int i = 1; i < n; i++) {diff[i] += diff[i - 1];scores[i] += diff[i];minScore = Math.min(minScore, scores[i]);}System.out.println(minScore);}
}
Python参考代码
n, p = map(int, input().split())
scores = list(map(int, input().split()))
diff = [0] * nfor _ in range(p):x, y, z = map(int, input().split())diff[x - 1] += zif y < n:diff[y] -= zmin_score = scores[0] + diff[0]
for i in range(1, n):diff[i] += diff[i - 1]scores[i] += diff[i]min_score = min(min_score, scores[i])print(min_score)
P3406 海底高铁
题目链接:P3406
思路:本题可以使用差分数组和前缀和求出每一段需要经过的次数 再用贪心策略在2种买票方式的最优中选择。
C++参考代码
#include <iostream>
#include <algorithm>using namespace std;
long long c[100005] = {0};
int main() {int n, m;cin >> n >> m;long long sum = 0, ans = 0;int p1 = 0, p2 = 0, a, b, c1;if (m > 0) {cin >> p1;}for (int i = 2; i <= m; i++) {cin >> p2;if (p1 < p2) {c[p1]++;c[p2]--;} else {c[p2]++;c[p1]--;}p1 = p2;}for (int i = 1; i < n; i++) {sum += c[i];cin >> a >> b >> c1;if (sum != 0) {ans += min(a * sum, b * sum + c1);}}if (m <= 1) {ans = 0;}cout << ans;return 0;
}
Java参考代码
import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);int n = scanner.nextInt();int m = scanner.nextInt();long[] c = new long[100005];long sum = 0, ans = 0;int p1 = 0, p2 = 0, a, b, c1;if (m > 0) {p1 = scanner.nextInt();}for (int i = 2; i <= m; i++) {p2 = scanner.nextInt();if (p1 < p2) {c[p1]++;c[p2]--;} else {c[p2]++;c[p1]--;}p1 = p2;}for (int i = 1; i < n; i++) {sum += c[i];a = scanner.nextInt();b = scanner.nextInt();c1 = scanner.nextInt();if (sum != 0) {ans += Math.min(a * sum, b * sum + c1);}}if (m <= 1) {ans = 0;}System.out.println(ans);scanner.close();}
}
Python参考代码
n, m = map(int, input().split())
c = [0] * 100005
sum = 0
ans = 0
P = list(map(int,input().split()))
p1 = P[0]
for i in range(1,len(P)):p2 = P[i]if p1 < p2:c[p1] += 1c[p2] -= 1else:c[p2] += 1c[p1] -= 1p1 = p2for i in range(1, n):sum += c[i]a, b, c1 = map(int, input().split())if sum != 0:ans += min(a * sum, b * sum + c1)if m <= 1:ans = 0print(ans)
P4552 IncDec Sequence
题目链接:P4552
参考思路:(这是一个比较困难的题)
题中要使所有数都一样。那么,也就是说,在差分的数组中,除了第一个数字外,其他的数字必须为0。
那么我们要做的,就是使除了第一个数字外,其他的数字必须为0。
我们知道差分的公式为 c [ l ] + = v ; c [ r + 1 ] − = v c[l]+=v;c[r+1]-=v c[l]+=v;c[r+1]−=v;
那么我们可以得出结论:
最少次数就是在差分序列中的正数相加的值和负数相加的绝对值的较大值。
那么,如何解决方法的种数呢?这又是转换法。
思考,差分后的第一个数字的种数是不是就是题目要求的方法数量。
那么要改变差分的第一个数字,是不是以 c [ 1 ] + + , c [ i ] − − 或 c [ 1 ] − − , c [ i ] + + c[1]++,c[i]--或c[1]--,c[i]++ c[1]++,c[i]−−或c[1]−−,c[i]++的方法来改变。
由于要求步数最少,要在差分数组中所有的正数或负数已经和其他数相互抵消完后,才能用 c [ 1 ] c[1] c[1]来勾兑。
那么答案就是正数和负数的绝对值的最大值减去正数和负数的绝对值的最小值。
C++参考代码
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll n,sum1=0,sum2=0;
int main()
{cin >> n;vector<ll>a(n+1,0);for(int i = 1 ; i <= n ; i++){cin >> a[i];}vector<ll>C(n+1,0);for(int i = 2 ; i <= n ; i++){C[i] = a[i] - a[i - 1];if(C[i] > 0) sum1 += C[i];else sum2 -= C[i];}cout << max(sum1 , sum2) << endl ;cout << abs(sum1 - sum2) + 1 << endl ;return 0;
}
Java参考代码
import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);int n = scanner.nextInt();long[] a = new long[n + 1]; for (int i = 1; i <= n; i++) {a[i] = scanner.nextLong();}long[] C = new long[n + 1]; long sum1 = 0, sum2 = 0;for (int i = 2; i <= n; i++) {C[i] = a[i] - a[i - 1];if (C[i] > 0) sum1 += C[i];else sum2 -= C[i];}System.out.println(Math.max(sum1, sum2));System.out.println(Math.abs(sum1 - sum2) + 1);scanner.close();}
}
Python参考代码
n = int(input())
a = [0] * (n + 1)
for i in range(1, n + 1):a[i] = int(input())C = [0] * (n + 1)
sum1 = sum2 = 0
for i in range(2, n + 1):C[i] = a[i] - a[i - 1]if C[i] > 0:sum1 += C[i]else:sum2 -= C[i] print(max(sum1, sum2))
print(abs(sum1 - sum2) + 1)相关文章:
2024蓝桥杯省赛保奖突击班-Day2-前缀和、差分、尺取_笔记_练习题解
3月25日-课堂笔记 前缀和预处理 O ( n ) \mathcal{O}(n) O(n) s[1] a[1]; for(int i 2; i < n; i)s[i] s[i - 1] a[i];利用前缀和查询区间和 O ( 1 ) O(1) O(1) long long calc(int l, int r) {return l 1 ? s[r] : s[r] - s[l - 1]; }差分序列的求法 c[1] a[…...
C++基础之虚函数(十七)
一.什么是多态 多态是在有继承关系的类中,调用同一个指令(函数),不同对象会有不同行为。 二.什么是虚函数 概念:首先虚函数是存在于类的成员函数中,通过virtual关键字修饰的成员函数叫虚函数。 性质&am…...
快速入门Kotlin①基本语法
前言 23年底读了一遍“Kotlin官方文档”,官方文档大而全,阅读下来,大有裨益。 此系列文章的目的是记录学习进程,同时,若能让读者迅速掌握重点内容并快速上手,那就再好不过了。 函数 带有两个 Int 参数、…...
【理解指针(四)】
文章目录 一、指针数组二、指针数组来模拟二维数组三、字符指针变量注意: 字符串的例子(曾经的一道笔试题) 四、数组指针变量1、什么是数组指针变量2、数组指针怎么初始化 五、二维数组传参的本质六、函数指针1、什么是函数指针变量2、函数的…...
Ribbon简介
目录 一 、概念介绍 1、Ribbon是什么 2、认识负载均衡 2.1 服务器端的负载均衡 2.2 客户端的负载均衡 3、Ribbon工作原理 4、Ribbon的主要组件 IClientConfig ServerList ServerListFilter IRule Iping ILoadBalancer ServerListUpdater 5、Ribbon支持…...
【感悟《剑指offer》典型编程题的极练之路】02字符串篇!
个人主页:秋风起,再归来~ 文章所属专栏:《剑指offer》典型编程题的极练之路 个人格言:悟已往之不谏,知来者犹可追 克心守己,…...
通过 Docker 实现国产数据库 OpenGauss 开发环境搭建
通过 Docker 实现国产数据库 OpenGauss 开发环境搭建 一 前置准备 2.1 下载镜像 docker pull enmotech/opengauss:5.0.1构建镜像的 Dockerfile,方便后期实现个性化定制: FROM ubuntu:22.04 as builderARG TARGETARCHWORKDIR /warehouseRUN set -eux;…...
【Java】LinkedList模拟实现
目录 整体框架IMyLinkedList接口IndexNotLegalException异常类MyLinkedList类成员变量(节点信息)addFirst(头插)addLast(尾插)在指定位置插入数据判断是否存在移除第一个相等的节点移除所有相等的节点链表的长度打印链表释放回收链表 整体框架 IMyLinkedList接口 这个接口用来…...
ubuntu下mysql常用命令
1. 登录数据库 mysql -u root -p 2.创建数据库 create database 数据库名字 mysql> create database yourdb; Query OK, 1 row affected (0.03 sec)3.显示数据库 show databases; 实操结果如下 mysql> show databases; -------------------- | Database | ---…...
燃气官网安全运行监测系统-阀井燃气监测仪-旭华智能
近年来,燃气爆炸事故频发,造成了重大人员伤亡和财产损失。这也再次为我们敲响警钟,燃气是我们日常生活中不可或缺的能源,但其潜在的危险性也是不容小觑。因此在重要节点加装燃气阀井气体监测仪,并将数据上传到系统平台…...
vue 文件预览(docx、.xlsx、pdf)
1.ifream <iframe src"" ></iframe> 注: src里面是文件地址 2.vue-office 支持vue2和vue3提供docx、.xlsx、pdf多种文档的在线预览方案 2.1安装 #docx文档预览组件 npm install vue-office/docx vue-demi#excel文档预览组件 npm install vue-office…...
云架构(二) 大使模式
Ambassador pattern (https://learn.microsoft.com/en-us/azure/architecture/patterns/ambassador) 简单描述 创建一个助手服务,这个服务代表消费服务或者应用程序发送网络请求。大使服务可以看做是与客户机同一个位置的进程外代理。 这种…...
.NET Path类库的特殊方法
在.NET中Path类库是非常常用的一个类库,包含很多我们常用的方法,常用的方法这里就不详细说明了,这里记录下几个非常规的方法。 获取随机文件名: //将返回随机的文件名Console.WriteLine(Path.GetRandomFileName()); 获取禁止在路…...
【JVM】JVM常用性能调优参数详细介绍
JVM常用性能调优参数详细介绍 一、何时进行JVM调优二、性能调优三、JVM调优的基本原则四、JVM调优目标五、JVM调优的步骤六、JVM参数七、JVM参数解析及调优八、JVM参数使用手册8.1 内存相关8.2 GC策略相关8.3 GC日志相关8.4 异常相关8.5 问题定位及优化相关九、参考文档一、何时…...
React中的受控组件与非受控组件
受控组件与非受控组件 受控组件 组件(input, select)的状态与state的值绑定,组件的状态全程响应外部数据 class TestComponent extends React.Component {constructor (props) {super(props);this.state { username: lindaidai };}render () {return <input …...
uniapp实现u-datetime-picker时间选择器的默认日期定位,解决default-value不生效问题
uniapp实现u-datetime-picker,设置默认定位日期,解决default-value不生效问题 想实现的效果是点开时间选择器默认显示当前日期,而不是该选择器最早的日期 给选择器添加ref属性,如下: <u-datetime-picker :show&q…...
react native 使用ScrollView实现下拉更新,上拉加载更多
在React Native中,要实现下拉更新和上拉加载更多的功能,你需要自定义ScrollView组件,监听滚动事件并根据滚动的位置来判断何时触发更新和加载更多的操作。以下是一个基本的实现思路: 监听滚动事件:使用ScrollView的on…...
vue2完结
笔记 关于不同版本的Vue: 1.vue.js与vue.runtime.xxx.js的区别:(1)vue.js是完整版的Vue,包含:核心功能模板解析器(2)vue.runtime.xxx.js是运行版本的Vue,只包含核心功能,没有模板解析器 2.因为…...
前端网页之间传递参数
在多页面应用中,我们可能面临着前端页面之间传递参数的情况,在一个页面获取到一些参数信息后,到另一个页面去进行后续处理,需要将前一个页面得到的一些参数带到第二个页面。当参数较少时,可以在跳转第二个页面时通过se…...
【常见面试题】Golang中,协程数最多可以开多少个?
参考: Goroutine 究竟可以开多少? 一、先说结论: 能开多少个协程,取决于单个协程处理方法所占用的CPU和内存资源(也就是看你计算机运行的应用程序的具体代码逻辑)。 二、具体来说: 如果是C…...
【根据当天日期输出明天的日期(需对闰年做判定)。】2022-5-15
缘由根据当天日期输出明天的日期(需对闰年做判定)。日期类型结构体如下: struct data{ int year; int month; int day;};-编程语言-CSDN问答 struct mdata{ int year; int month; int day; }mdata; int 天数(int year, int month) {switch (month){case 1: case 3:…...
1.3 VSCode安装与环境配置
进入网址Visual Studio Code - Code Editing. Redefined下载.deb文件,然后打开终端,进入下载文件夹,键入命令 sudo dpkg -i code_1.100.3-1748872405_amd64.deb 在终端键入命令code即启动vscode 需要安装插件列表 1.Chinese简化 2.ros …...
Python 实现 Web 静态服务器(HTTP 协议)
目录 一、在本地启动 HTTP 服务器1. Windows 下安装 node.js1)下载安装包2)配置环境变量3)安装镜像4)node.js 的常用命令 2. 安装 http-server 服务3. 使用 http-server 开启服务1)使用 http-server2)详解 …...
yaml读取写入常见错误 (‘cannot represent an object‘, 117)
错误一:yaml.representer.RepresenterError: (‘cannot represent an object’, 117) 出现这个问题一直没找到原因,后面把yaml.safe_dump直接替换成yaml.dump,确实能保存,但出现乱码: 放弃yaml.dump,又切…...
JDK 17 序列化是怎么回事
如何序列化?其实很简单,就是根据每个类型,用工厂类调用。逐个完成。 没什么漂亮的代码,只有有效、稳定的代码。 代码中调用toJson toJson 代码 mapper.writeValueAsString ObjectMapper DefaultSerializerProvider 一堆实…...
RLHF vs RLVR:对齐学习中的两种强化方式详解
在语言模型对齐(alignment)中,强化学习(RL)是一种重要的策略。而其中两种典型形式——RLHF(Reinforcement Learning with Human Feedback) 与 RLVR(Reinforcement Learning with Ver…...
用 FFmpeg 实现 RTMP 推流直播
RTMP(Real-Time Messaging Protocol) 是直播行业中常用的传输协议。 一般来说,直播服务商会给你: ✅ 一个 RTMP 推流地址(你推视频上去) ✅ 一个 HLS 或 FLV 拉流地址(观众观看用)…...
Java多线程从入门到精通
一、基础概念 1.1 进程与线程 进程是指运行中的程序。 比如我们使用浏览器,需要启动这个程序,操作系统会给这个程序分配一定的资源(占用内存资源)。 线程是CPU调度的基本单位,每个线程执行的都是某一个进程的代码的某…...
Modbus转ETHERNET IP网关:快速冷却系统的智能化升级密钥
现代工业自动化系统中,无锡耐特森Modbus转Ethernet IP网关MCN-EN3001扮演着至关重要的角色。通过这一技术,传统的串行通讯协议Modbus得以在更高速、更稳定的以太网环境中运行,为快速冷却系统等关键设施的自动化控制提供了强有力的支撑。快速冷…...
简约商务年终工作总结报告PPT模版分享
简约精致扁平化商务通用动画PPT模版,简约大气素雅商务PPT模版,商务PPT模版,商业计划书PPT模版,IOS风商务通用PPT模版,公司介绍企业宣传PPT模版,创业融资PPT模版,创意低多边形PPT模版,…...
