版本比较
笔记
正文
增加 111 行删除 40 行
1123456789101112131415161718192021222324252627345620 行未改动2728293056313233343590 行未改动12612712812915515613013113215813313416013513616216313713813914014116716816917014214314414514614714814917415015115215315415515618 行未改动175176177178202179180181182183231 行未改动全部展开41541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444545044644744844945054 行未改动505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583快读快写模板:几个做题的小技巧:
```cppint read(){ int x = 0,f = 1; char c = getchar_unlocked(); while(c < '0' || c > '9'){ if(c == '-')f = -1; c = getchar_unlocked(); } while(c >= '0' && c <= '9'){ x = x * 10 + c - '0'; c = getchar_unlocked(); } return x * f;}void write(int x){ if(x < 0)putchar('-'),x = -x; if(x > 9)write(x / 10); putchar(x % 10 + '0');}```
## 枚举 & 模拟
**重点**!
- **通过挖掘题目的特殊性质,找到分析问题的切入点**- **更换统计答案的方式**- **特殊性质优化**- **模型等效替换**20 行未改动> 在搜索的过程中,及时对当前状态进行检查,如果发现分支已经无法到达递归边界,就执行回溯。这就好比我们在路上走时,远远看到前方是一个死胡同,就应该立即折返绕路,而不是走到路的尽头再返回。> 2. **上下界剪枝**\> 在最优化问题的搜索过程中,如果当前花费的代价已经超过当前搜到的最优解,那么无论采取多么优秀的策略到达递归边界,都不可能更新答案。此时可以停止对当前分支的搜索,执行回溯,本质上就是考虑极端情况。
例题:[P1219 八皇后](https://www.luogu.com.cn/problem/P1219)例题:[P1219 (USACO1.5) 八皇后 Checker Challenge](https://www.luogu.com.cn/problem/P1219)
:::success[AC code]{open}```cpp#include<bits/stdc++.h>90 行未改动> 队列:>> 1. **顺序队列**\> 顺序队列的基本操作:\> (1) **初始化**:head = tail = 0,这时候队列中没有元素,为空。\> (2)**x 元素入队**:q\[tail++]=x;\> (1) **初始化**:`head = tail = 0`,这时候队列中没有元素,为空。\> (2)**x 元素入队**:`q[tail++]=x;`\> (3) **队首元素赋值给 a 并出队**:\> a=q\[head++];\> `a=q[head++];`\> (4) **队列为空的条件**:\> head==tail\> `head==tail`\> (5) **队列中元素个数**:\> tail-head\> 当然也可以使用 STL 中的 queue 来实现。> `tail-head`\> PS:也可以使用 STL 中的 queue 来实现哦!> 2. **循环队列**\> 具体循环队列的基本操作如下:\> **“队列初始化”与“判断队列是否为空”与顺序队列是一样的**;\> **进队 (插入元素 x)**:如果队列未满则执行 q \[tail]=x;tail=(tail + 1)% m;\> **出队**:如果队列不为空,则返回队首元素 q \[head],同时 head=(head + 1)% m。\> **队列中元素个数**:(tail-head+m)% m;> 3. **单调队列**\> **进队 (插入元素 x)**:如果队列未满则执行> `q[tail]=x;tail=(tail + 1)% m`;> **出队**:如果队列不为空,则返回队首元素 q[head],同时 `head=(head + 1)% m`。\> **队列中元素个数**:`(tail-head+m)% m`;> 4. **单调队列**\> 单调队列是一种**特殊的队列**,它能在队列**两端进行删除操作**,并始终维护队列保持一种**单调性**。\> 以单调递增队列为例,它要求队列中的元素始终保持单调递增,新元素入队时,**先将队尾比新元素值更大的元素剔除出队列后,再将新元素入队**。这保证了队列中**所有元素单调递增**。\> 例如:队列中元素 $10$ $98$ $100$ $105$,则 $99$ 入队时,先将 $100$ 和 $105$ 剔除出队,再将 $99$ 入队,队列中的元素为:$10$ $98$ $99$。> 顺序队列模板题:[B3616 【模板】队列](https://www.luogu.com.cn/problem/B3616)
顺序队列模板题:[B3616 【模板】队列](https://www.luogu.com.cn/problem/B3616)
:::success[AC Code(STL)]{open}```cpp#include<bits/stdc++.h>using namespace std;18 行未改动}```:::
单调队列例题:[P1886【模板】单调队列 / 滑动窗口](https://www.luogu.com.cn/problem/P1886)单调队列模板题:[P1886【模板】单调队列 / 滑动窗口](https://www.luogu.com.cn/problem/P1886)
:::success[AC Code]{open}```cpp#include<bits/stdc++.h>231 行未改动全部展开```cppfor(int i=1;i<=n;i++) pre[i]=pre[i-1]+a[i];
```前缀和模板题[B3612 【深进1.例1】求区间和](https://www.luogu.com.cn/problem/B3612):::success[AC Code]{open}```cpp#include<bits/stdc++.h>using namespace std;int n,a[100005],m,l,r,pre[100005];int main(){ cin >> n; for(int i = 1;i <= n;++i)cin >> a[i]; for(int i = 1;i <= n;++i)pre[i] = pre[i - 1] + a[i]; cin >> m; while(m--){ cin >> l >> r; cout << pre[r] - pre[l - 1] << endl; } return 0;}```:::
- ### 差分
```cppfor(int i = 1;i <= n;++i)d[i] = a[i] - a[i - 1];```
可快速修改一个区间里的数,使这个区间里的数字同时增加 $k$。\例题:[P2367 语文成绩](https://www.luogu.com.cn/problem/P2367)差分模板题:[P2367 语文成绩](https://www.luogu.com.cn/problem/P2367)
:::success[AC Code]{open}```cpp#include<bits/stdc++.h>54 行未改动 return 0;}```:::
经典问题:括号匹配。 [B2165 括号匹配](https://www.luogu.com.cn/problem/B2165)
:::success[AC Code]{open}```cpp#include<bits/stdc++.h>using namespace std;
stack<char>stk;string s;int n;char ch[128];bool check(string s){ while(!stk.empty())stk.pop(); for(char c:s){ if(ch[c]==0){//是左半 stk.push(c); }else if(!stk.empty() && stk.top() == ch[c]){//匹配上了 stk.pop(); }else {//没匹配上 return false; } } return stk.empty();}int main(){ cin>>n; ch[')']='('; ch[']']='['; ch['}']='{'; while(n--){ cin>>s; puts(check(s)?"YES":"NO"); } return 0;}```:::
- ### 链表1. 查询序列中第 i 个元素:时间复杂度 $O(n)$
```cppint GetPos(int pos)//求链表第pos个元素的存储位置{ int hd=Head; for(int j=1;j<pos;j++)hd=nxt[hd]; return hd;}```
2. 在第 i 个元素前加入新的元素 v:时间复杂度 $O(n)$(查找到第 i 个元素需要 $O(n)$)
```cppvoid InsertValue(int pos,int val)//在第pos个元素前插入新元素val{ int p=GetPos(pos-1); Value[++n]=val; nxt[n]=nxt[p]; nxt[p]=n;}```
3. 删除序列中的第 i 个元素:时间复杂度 $O(n)$(查找到第 i 个元素需要 $O(n)$)
```cppvoid DeleteValue(int pos)//删除第pos个元素{ int p=GetPos(pos-1); nxt[p]=nxt[nxt[p]];}```当然也可以使用数组模拟。 PS:也可以使用 STL 中的 list 哦!