
acwing
percation
快乐coding,coding快乐~
展开
-
【acwing】算法指南蓝书_位运算_a^b_64位整数乘法
整数乘法是两个数a和b之间的乘法,将最小数b化为二进制,让a分别乘以b在二进制位数为1的系数,然后相加。幂运算是res = 1,因为类似a * a * a, 除非a为0,res = 1,可以让连乘进行下去。用加法实现乘法(龟速乘,原来两个数相乘为O(1),龟速乘为O(log)两个long long 数相加,不会爆long long。将b(十进制)转化为二进制的形式:如b=5,即101.整数乘法相当于有b个a相加,次数是。b的二进制位有O(logb)位。如b = 5, b = 101。原创 2023-12-07 16:47:38 · 1061 阅读 · 0 评论 -
【acwing】5. 多重背包问题 II** & 【洛谷】 宝物筛选(二进制优化)
穿越隧道思路很妙,太绝了。将每种物品的S个数量,来进行合并(二进制的方式),最后经过二进制整合的物品,总体积和总价值不变,但数量从朴素遍历S件,优化成了遍历logS个物品被自己菜哭了Q-Q#include <iostream>#include <algorithm>#include <cstring>using namespace std;const int N = 1e4 + 10, M = 2e3 + 10;//注意数据范围,N的数据范围是咋来的呀原创 2022-03-27 14:11:53 · 268 阅读 · 0 评论 -
【acwing】1022. 宠物小精灵之收服*(二维价值的01背包)
穿越隧道三维状态表示超内存了。。哭,思路应该没问题吧。求大佬指正。#include <bits/stdc++.h>using namespace std;const int N = 1e3,M = 1e2 + 10;int cnt[M];int em[M];int f[M][N][510];// int f[N][510];int n,m,k;int main(){ scanf("%d%d%d",&n,&m,&k); for(原创 2022-03-21 15:08:14 · 311 阅读 · 0 评论 -
【acwing】1016. 最大上升子序列和*
穿越隧道和求最长上升子序列类似、不同之处在于:计数变成了求和+1变成了加a[i]#include <iostream>#include <algorithm>#include <cstring>using namespace std;const int N = 1e3 + 10;int a[N];int f[N];int g[N];int n;int main(){ cin >> n; for(int i = 1原创 2022-03-30 16:06:28 · 242 阅读 · 0 评论 -
【acwing】9. 分组背包问题*
穿越隧道分组背包和01背包的类似之处:选(只能选一次)还是不选不同之处在于:分组背包在每组中是否选其中的某样物品。01背包是对每样物品(每样物品只有一个)来进行是否选择。完全背包是对每样物品(每样物品有无限多个)来进行选择(不选或者选1次,或者2次,…,或者∞次)多重背包是对每样物品(每样物品只有S个)来进行选择(不选或者选1次,或者2次,…,或者s次)#include <iostream>#include <algorithm>#include <cs原创 2022-03-27 14:35:56 · 180 阅读 · 0 评论 -
【acwing】1113. 红与黑*(搜索|flood fill)
穿越隧道BFS听y总讲到了flood fill, 就想起来了之前的城堡问题也是求不同连通块的面积。这题是求以@为起点,求起点能经过的黑色地板,也就是联通块的面积,不过只有@一个起点。#include <iostream>#include <algorithm>#include <cstring>#include <queue>#define x first#define y secondusing namespace std;type原创 2022-03-28 21:45:45 · 569 阅读 · 0 评论 -
【acwing】789. 数的范围(二分基础)
穿越隧道原创 2022-07-06 15:54:17 · 122 阅读 · 0 评论 -
【acwing】786. 第k个数
穿越隧道原创 2022-07-05 21:41:57 · 122 阅读 · 0 评论 -
【剑指Offer】42. 栈的压入、弹出序列
题目链接原创 2022-07-05 19:28:41 · 193 阅读 · 1 评论 -
【acwing】1912. 里程表(思维|STL)
穿越隧道AC版因数据范围很大,依次遍历,按道理大概率会TLE.类似构造法、逆向思维。对于一个数字字符串:其数字元素全相同改变数字字符串的其中一个元素(除前导零),改变后的元素是否在数据范围内,若是,则答案++。atoll:字符串转为long long#include <bits/stdc++.h>using namespace std;// const int N = typedef long long ll;ll x,y;ll ans;map<int,in原创 2022-04-25 21:48:42 · 321 阅读 · 1 评论 -
【acwing】2003. 找到牛(思维)
穿越隧道O(n)只扫一遍#include <bits/stdc++.h>using namespace std;typedef pair<int,int> pii;const int N = 10;map<pair<pii,pii>,int> mp;int ans,cnt;string s;int main(){ cin >> s; int len = s.size(); // cout <&原创 2022-04-25 20:06:39 · 314 阅读 · 0 评论 -
【acwing】1135. 新年好***(dijkstra+堆优化)
穿越隧道预处理,再搜索出最小的答案。mlognmlog_nmlogn + 5!5!5!#include <cstring>#include <cstdio>#include <iostream>#include <algorithm>#include <queue>#define x first#define y secondusing namespace std;typedef pair<int,int>原创 2022-04-22 13:57:04 · 145 阅读 · 0 评论 -
【acwing】1125. 牛的旅行***(floyd)
穿越隧道1.floyd求出任意两点之间的最短距离2.求maxd[i],表示和i联通的且距离i最远的点的距离3.情况1:所有maxd[i]的最大值情况2:枚举在哪两个点之间连边。i,j,需要满足d[i,j] = INF.maxd[i] + dis[i,j] + maxd[j];#include <bits/stdc++.h>#define x first#define y second using namespace std;typedef pair<double,原创 2022-04-21 21:51:11 · 191 阅读 · 0 评论 -
【acwing】166. 数独****(DFS)
穿越隧道如何能正确的搜出所有的方案1.顺序2.剪枝搜索:1.优化搜索顺序大部分情况下,应优选搜索分支较少的节点2.排除等效冗余3.可行性剪枝4.最优性剪枝5.记忆化搜索(DP)在这题中使用了位运算优化(9位的01二进制数)求行、列与九宫格的交集状态(按位与运算)循环或lowbit运算O(1),返回最后一个11.优化搜索顺序:√(有)选择2.排序等效冗余 :×(无重复性情况)3.可行性剪枝:√(有)4.最优化剪枝:×(无)5.记忆化搜搜(DP):×(无)还原创 2022-04-21 16:48:00 · 889 阅读 · 0 评论 -
【acwing】165. 小猫爬山(DFS之剪枝)
穿越隧道搜索:1.优化搜索顺序大部分情况下,应优选搜索分支较少的节点2.排除等效冗余3.可行性剪枝4.最优性剪枝5.记忆化搜索(DP)在这题中1.优化搜索顺序:√(有)猫越重,分支可能越少。对猫的重量从大到小排序2.排序等效冗余 :×(无重复性情况)3.可行性剪枝:√(有)如果某辆车猫的重量超出了W(剪枝)4.最优化剪枝:√(有)如果新开车的数量已经大于ans,就直接退出5.记忆化搜搜(DP):×(无)#include <bits/stdc++.h>u原创 2022-04-21 15:54:15 · 918 阅读 · 0 评论 -
【acwing】1118. 分成互质组 ***(DFS)
穿越隧道递归组合型搜索+gcd#include <bits/stdc++.h>using namespace std;const int N = 1e2 + 10;int n;int p[N];int group[N][N];int ans = N;bool st[N];//判断当前每个数被用过。 int gcd(int a, int b){ return b ? gcd(b,a % b):a;} bool chk(int group[], int gc, int原创 2022-04-21 15:09:22 · 372 阅读 · 2 评论 -
【acwing】1459. 奶牛体操(模拟、思维)
穿越隧道模拟、思维#include <bits/stdc++.h>using namespace std;const int N = 2e2 + 10;int n,m;typedef pair<int,int> pii;map<pii,int> mp;int a[N][N];int ans;bool flag[N][N];int main(){ cin >> n >> m; for(int i = 0; i <原创 2022-04-21 09:03:03 · 427 阅读 · 0 评论 -
【acwing】1874. 哞加密(模拟)
穿越隧道对图中的每个点的8个方向,走两步。判断是否越界,若合法,则进行判断是否为ABB型字符串,且A不为M,B不为O;使用hash表存储字符串#include <bits/stdc++.h>using namespace std;const int N = 1e2 + 10;int n,m;string g[N];int dx[8] = {-1,-1,0,1,1,1,0,-1};int dy[8] = {0,1,1,1,0,-1,-1,-1};unordered_ma原创 2022-04-20 20:02:52 · 184 阅读 · 0 评论 -
【acwing】1353. 滑雪场设计*(枚举)
穿越隧道开始思路:对所有山的高度排序,取两个极值。但觉得不对劲,假如次大的、或者次小的,可能不符合题意。当第一次的最大值,减少一定高度时,说不定次大的山就成了最高山,反之同理。抱着侥幸心理交了一发,果然错了。后来,看了大佬博客,他们采用了穷举发,使得所有山的高度差都保持在17以内。#include <bits/stdc++.h>using namespace std;const int N = 1e3 + 10;int a[N];int ans;int n;int原创 2022-04-20 07:57:51 · 172 阅读 · 0 评论 -
【洛谷】P1455 搭配购买**
穿越隧道并查集和01背包的搭配01背包需优化,防止TLE.#include <bits/stdc++.h>using namespace std;const int N = 1e4 + 10;int p[N];int n,m,k;int w[N];//第i朵云的价钱int v[N];//第i朵云的价值 int f[N];int ans;int find(int x){ if(p[x] != x){ p[x] = find(p[x]);//路径压缩,直接除根节点外原创 2022-03-17 20:17:52 · 497 阅读 · 0 评论 -
【acwing】1250. 格子游戏*(并查集)
穿越隧道并查集模板题:1.二维坐标转为一维坐标2.在同一个集合的点,再加入一条边时就会联通。3.并查集更比较适合一维上操作#include <iostream>#include <cstring>#include <algorithm>using namespace std;const int N = 4e4 + 10;int n,m;int p[N];int fi(int x){//状态压缩 if(p[x] != x) p[x] = f原创 2022-04-18 13:06:22 · 2275 阅读 · 0 评论 -
【acwing】1117. 单词接龙(dfs)
穿越隧道#include <bits/stdc++.h>using namespace std;const int N = 22;int n;string word[N];int g[N][N];int used[N];int ans;void dfs(string dragon, int last){ ans = max((int)dragon.size(), ans);//更新答案 used[last]++;//表明last编号的这个单词已经访问过。 for(i原创 2022-04-17 11:23:01 · 359 阅读 · 2 评论 -
【acwing】1116. 马走日*(DFS)
穿越隧道模板题微变型。#include <bits/stdc++.h>using namespace std;const int N = 1e2 + 10;int g[N][N];int ans;int t;int n,m,x,y;int dx[8] = {-2,-1,1,2,2,1,-1,-2};int dy[8] = {1,2,2,1,-1,-2,-2,-1};bool st[N][N];void dfs(int u, int uu,int num){ if(原创 2022-04-16 21:35:17 · 237 阅读 · 0 评论 -
【acwing】831. KMP字符串*
穿越隧道需多次回忆#include <iostream>using namespace std;const int N = 1e5 + 10, M = 1e6 + 10;int n,m;char p[N], s[M];int ne[N];int main(){ cin >> n >> p + 1 >> m >> s + 1; //求next过程和匹配过程类似 /*next[]数组定义:最大的真前后缀,(不含本身)*/原创 2022-04-16 19:59:40 · 196 阅读 · 0 评论 -
【acwing】1883. 删减*(string类型操作应用)
穿越隧道ACac的代码,熟悉string的性质,而我太菜了,要加油啊。。#include <bits/stdc++.h>using namespace std;const int N = 1e3 + 10;string s;string t;int main(){ cin >> s; cin >> t; int len = t.size(); for(int i = 0; i < s.size(); i++){ if(s.subst原创 2022-04-15 10:23:23 · 152 阅读 · 0 评论 -
【acwing】3370. 牛年**(模拟)
穿越隧道这道模拟题,没想清楚,逻辑没有理顺,需要模拟。强制灌输。#include <bits/stdc++.h>using namespace std;const int N = 1e3 + 10;struct noww{ string na1; string na2;};map<pair<string, string>,int> mp;map<string, int> mmp;string years[13] = {"Ox",原创 2022-04-14 22:01:17 · 357 阅读 · 0 评论 -
【acwing】3358. 放养但没有完全放养*(思维|模拟)
穿越隧道没想清楚的氵题。。。又是被自己菜哭的一天。#include <bits/stdc++.h>using namespace std;const int N = 1e3 + 10;map<char,int> mp, mpp;int b[N];bool st[N];int ans;int main(){ string s; cin >> s; string t; cin >> t; int len = s.size();原创 2022-04-14 17:09:50 · 384 阅读 · 0 评论 -
【acwing】1058. 股票买卖 V**(状态机|线性DP)
开始的思路:试图写三维状态f[i][j][k]f[i][j][k]f[i][j][k],iii表示第几天,jjj表示手中有货还是无货,kkk表示是否为冷却期我可能写得三维代码的状态冗余了错误思路代码(三维)#include <bits/stdc++.h>using namespace std;const int N = 1e5 + 10;int n;int a[N];int f[N][2][2];int main(){ scanf("%d",&n); for.原创 2022-04-14 11:10:37 · 289 阅读 · 0 评论 -
【acwing】1057. 股票买卖 IV**(状态机模型|DP)
穿越隧道#include <bits/stdc++.h>using namespace std;const int N = 1e5 + 10, M = 1e2 + 10;int n,k;int a[N];int f[N][M][2];int main(){ scanf("%d%d",&n,&k); for(int i = 1; i <= n; i++){ scanf("%d",&a[i]); } memset(f,-0x3f,sizeof原创 2022-04-13 20:51:38 · 313 阅读 · 0 评论 -
【acwing】1049. 大盗阿福*(状态机模型|线性DP)
穿越隧道DP版#include <bits/stdc++.h>using namespace std;const int N = 1e5 + 10;int n;int a[N];int t;int f[N];int main(){ scanf("%d",&t); while(t--){ scanf("%d",&n); for(int i = 1; i <= n; i++){ scan原创 2022-04-13 12:20:10 · 199 阅读 · 0 评论 -
【acwing】907. 区间覆盖**(贪心|双指针)
穿越隧道开始的思路对N个区间的区间长度进行从大到小排序,因考虑到要最少的区间来对目标区间进行覆盖。后来,发现有点难以实现,看了y总的思路是:对N个区间的左端点,进行从小到大排序,找到左端点小于st,且右端点最大的区间来更新st,直到扫描完所有的区间,判断此时的st是否>=ed,若是,则成功覆盖,否则失败。采用了双指针的思想#include <bits/stdc++.h>#define x first#define y secondusing namespace std原创 2022-04-12 15:01:37 · 332 阅读 · 0 评论 -
【acwing】906. 区间分组*(贪心)
穿越隧道大佬的思路太妙了,将一段区间认为是教室开始上课时间,和教室结束上课时间。当教室开始上课时,需要一个教室,到了结束时间,则释放教室。求的是:高峰时期,有多少个教室在同时上课。将开始时间设置为偶数,结束时间设置为奇数。开始则++,结束–,求期间的最大值。#include <bits/stdc++.h>#define x first#define y secondusing namespace std;const int N = 2e5 + 10;int b[N]原创 2022-04-11 17:25:09 · 158 阅读 · 0 评论 -
【acwing】125. 耍杂技的牛**(贪心|推导证明)
穿越隧道#include <iostream>#include <cstring>#include <bits/stdc++.h>#define x first#define y secondusing namespace std;typedef pair<int,int> pii;const int N = 1e5 + 10;int n;pii a[N];int main(){ scanf("%d",&n);原创 2022-04-11 16:48:29 · 264 阅读 · 0 评论 -
【acwing】104. 货仓选址*(贪心|绝对值不等式)
穿越隧道绝对值不等式如果x在a~b区间外,∣a−x∣+∣b−x∣>=∣a−b∣|a - x| + |b - x| >= |a-b|∣a−x∣+∣b−x∣>=∣a−b∣(当x在a,b两点上,等式成立)如果x在a~b区间之间,∣a−x∣+∣b−x∣==∣a−b∣|a - x| + |b - x| == |a-b|∣a−x∣+∣b−x∣==∣a−b∣这个题目,要满足x这个点在所有区间的中心位置,如:1,2,3,4,51,2,3,4,51,2,3,4,5(排序后)下标0,1,2,3原创 2022-04-11 15:58:16 · 420 阅读 · 0 评论 -
【acwing】913. 排队打水(贪心)
穿越隧道排序当第iii个人打水的时候,其余 i+1\ i+1 i+1 ~ nnn个人需要等待tit_iti的时间,直到最后一个人打完水#include <bits/stdc++.h>using namespace std;const int N = 1e5 + 10;typedef long long ll;int a[N];int n;int main(){ scanf("%d",&n); for(int i = 0; i原创 2022-04-11 14:24:42 · 207 阅读 · 0 评论 -
【acwing】148. 合并果子(贪心|堆)
穿越隧道基于堆来实现贪心用的是stl中的堆。#include <bits/stdc++.h>using namespace std;typedef long long ll;const int N = 1e4 + 10;int a[N];int n;int main(){ cin >> n; priority_queue<int,vector<int>,greater<int>> heap; for(原创 2022-04-11 12:16:57 · 366 阅读 · 0 评论 -
【acwing】908. 最大不相交区间数量(贪心)
穿越隧道思路和代码和区间选点一摸一样。开始,犹豫在以为一定要选一对区间,没想到单个区间可以。。#include <bits/stdc++.h>#define x first#define y secondusing namespace std;typedef pair<int,int> pii;const int N = 1e5 + 10;pii a[N];bool st[N];int n;bool cmp(pii a, pii b){ return原创 2022-04-10 22:07:03 · 438 阅读 · 0 评论 -
【acwing】905. 区间选点*(贪心)
穿越隧道对右端点排序,则从左往右扫描,看右端点经过几个区间。对左端点排序,则从右往左扫描,看左端点经过几个区间。类似双指针的思想,来进行优化#include <bits/stdc++.h>#define x first#define y secondusing namespace std;typedef pair<int,int> pii;const int N = 1e5 + 10;pii a[N];bool st[N];int n;bool cmp原创 2022-04-10 20:15:36 · 427 阅读 · 0 评论 -
【蓝桥杯】1204. 错误票据*(读入方式)
穿越隧道自己没注意的坑点:读入方式数组大小sstream读入方式#include <bits/stdc++.h>using namespace std;map<int,int> mp;const int N = 5e4 + 10;int a[N];int n;int main(){ scanf("%d\n",&n); int k = 0; int ans1 = -1, ans2 = -1; int mxx = -2,mii = 0x3f3f原创 2022-04-08 15:43:20 · 124 阅读 · 0 评论 -
【蓝桥杯】1245. 特别数的和(简单模拟)
穿越隧道取数字的每一位来进行判断。#include <iostream>#include <bits/stdc++.h>using namespace std;const int N = 1e5 + 10;int n;typedef long long ll;ll sum = 0;bool chk(int x){ while(x){ if(x%10 == 2 || x%10 == 0 || x % 10 == 1 || x % 10 == 9){原创 2022-04-08 12:20:12 · 116 阅读 · 0 评论