
acwing 算法基础课 (存答案)
acwing 算法基础课的所有题解题思路和代码,希望可以帮助到大家,有任何问题可以私聊博主!
三粒小金子
第十四届蓝桥杯河南省省一等奖
展开
-
acwing 907 区间覆盖
#include <iostream>#include <algorithm>using namespace std;const int N = 100010;int n;struct Range{ int l, r; bool operator< (const Range &W)const { return l < W.l; }}range[N];int main(){ int .原创 2022-05-14 23:12:43 · 132 阅读 · 0 评论 -
acwing 905 区间选点
项目场景:提示:这里简述项目相关背景:例如:项目场景:示例:通过蓝牙芯片(HC-05)与手机 APP 通信,每隔 5s 传输一批传感器数据(不是很大)问题描述提示:这里描述项目中遇到的问题:例如:数据传输过程中数据不时出现丢失的情况,偶尔会丢失一部分数据APP 中接收数据代码:@Override public void run() { bytes = mmInStream.read(buffer); mHandler.obtainMessage(READ_DATA, b原创 2022-05-13 14:01:10 · 140 阅读 · 3 评论 -
acwing 901 滑雪 2022/05/11
#include<iostream>#include<cstring>#include<algorithm>using namespace std ;const int N = 310 ;int n , m ;int f[N][N] ;int h[N][N] ;int dx[4] = {-1,0,1,0} , dy[4] = {0,1,0,-1} ;int dp(int x , int y){ int &v = f[x][y].原创 2022-05-11 21:18:09 · 154 阅读 · 0 评论 -
acwing 285 没有上司的舞会 2022/05/11
#include<iostream>#include<cstring>#include<algorithm>using namespace std ;const int N = 6010 ;int happy[N] ;int e[N] , h[N] , ne[N] , idx ;bool hasfather[N];int f[N][2];int n;void add(int a , int b){ e[idx] = b , ne[idx.原创 2022-05-11 17:01:38 · 1921 阅读 · 0 评论 -
acwing 最短hamilton路径 2022/05/11
#include<iostream>#include<cstring>#include<algorithm>using namespace std ;const int N = 25 , M = 1 << 20 ;int f[M][N] , a[N][N];int n ;int main(){ cin >> n ; for(int i = 0; i < n ; i ++) { fo.原创 2022-05-11 14:34:08 · 133 阅读 · 0 评论 -
acwing 900 整数划分 2022/05/10
#include <iostream>using namespace std;const int N = 1e3 + 7, mod = 1e9 + 7;int f[N][N];int main() { int n; cin >> n; for (int i = 0; i <= n; i ++) { f[i][0] = 1; // 容量为0时,前 i 个物品全不选也是一种方案 } for (int i .原创 2022-05-11 08:21:35 · 91 阅读 · 0 评论 -
acwing 282 石子合并 2022/05/10
#include<iostream>#include<algorithm>using namespace std ;const int N = 310 ;int n ;int s[N];int f[N][N] ;int main(){ cin >> n ; for(int i =1 ; i <=n ; i++) { cin >> s[i] ; s[i] += s[i-...原创 2022-05-10 16:03:19 · 162 阅读 · 0 评论 -
acwing 902 最短编辑距离 2022/05/09
#include<iostream>#include<algorithm>using namespace std ;const int N = 1010 ;int n , m ;char a[N] , b[N] ;int f[N][N] ;int main(){ cin >> n; for(int i = 1; i <= n; ++i) cin >> a[i]; cin >>...原创 2022-05-09 18:14:48 · 239 阅读 · 2 评论 -
acwing 897 最长公共子序列 2022/05/09
#include<iostream>#include<cstring>using namespace std ;int n , m ;char a[1010],b[1010] ;int f[1010][1010];int main(){ cin >> n >> m >> a+1 >> b+1 ; for(int i = 1; i <= n ;i++) { for(int.原创 2022-05-09 17:12:27 · 238 阅读 · 0 评论 -
acwing 291 蒙德里安的梦想 2022/05/02
#include<iostream>#include<cstring>#include<vector>using namespace std ;const int N = 20 , M = 1 << 11 ;vector<int> state[M] ;long long f[N][M] ;bool st[M];int m , n ;int main(){ while(cin >> n >&...原创 2022-05-04 00:52:21 · 264 阅读 · 0 评论 -
acwing 895 最长上升子序列 2022/04/27
#include <iostream>using namespace std;const int N = 1010;int n;int w[N], f[N];int main() { cin >> n; for (int i = 0; i < n; i++) cin >> w[i]; int mx = 1; // 找出所计算的f[i]之中的最大值,边算边找 for (int i = 0; i <...原创 2022-04-27 21:41:04 · 153 阅读 · 0 评论 -
acwing 898 数字三角形 2022/04/27
左上和右上的位置来更新i,j的f数组题目要求必须走到底层,所以就必须是f【n】【i】的最大值每一行都要额外定义一个负无穷的数值,这个位置是下一行最后一个元素的右上。#include<bits/stdc++.h>using namespace std;const int N=510,INF=0x3f3f3f3f;int f[N][N];int a[N][N];int main(){ int n; cin>>n; for(int i=原创 2022-04-27 19:27:06 · 164 阅读 · 0 评论 -
acwing 分组背包问题 2022/04/23
#include<iostream>#include<algorithm>using namespace std;const int N=110;int f[N],v[N][N],w[N][N],s[N];int main(){ int n,m; cin>>n>>m; for(int i=1;i<=n;i++){ cin>>s[i]; for(int j=0;j<s[i.原创 2022-04-23 23:30:39 · 121 阅读 · 0 评论 -
acwing 多重背包问题 2022/4/21
#include<iostream>using namespace std;const int N = 12010, M = 2010;int n, m;int v[N], w[N]; //逐一枚举最大是N*logSint f[M]; // 体积<Mint main(){ cin >> n >> m; int cnt = 0; //分组的组别 for(int i = 1;i <= n;i ++) ...原创 2022-04-22 17:39:21 · 260 阅读 · 0 评论 -
acwing 完全背包问题 2022/04/21
朴素做法:是写出状态转移之后,然后每次都对该状态的权值和去掉k个物品i之后的权值来确定该状态的最大值但是写完会发现ac不成功,超时了,TLE#include<iostream>#include<algorithm>using namespace std ;const int N = 1010 ;int n , m ;int v[N] , w[N] ;int f[N][N];int main(){ cin >> n >>..原创 2022-04-21 09:49:17 · 109 阅读 · 0 评论 -
acwing 2.01背包问题 2022/04/19
#include<bits/stdc++.h>using namespace std;const int MAXN = 1005;int v[MAXN]; // 体积int w[MAXN]; // 价值 int f[MAXN][MAXN]; // f[i][j], j体积下前i个物品的最大价值 int main() { int n, m; cin >> n >> m; fo...原创 2022-04-20 01:06:44 · 363 阅读 · 0 评论 -
acwing 894 拆分 nim游戏 2022/03/26
#include <iostream>#include <cstring>#include <unordered_set>using namespace std;const int N = 110;int n;int f[N];unordered_set<int> S;int sg(int x){ if(f[x] != -1) return f[x]; for(int i = 0 ; i < x ; i++.原创 2022-03-26 18:21:47 · 4432 阅读 · 0 评论 -
acwing 893 集合nim游戏 2022/03/25
sg函数是有向图当中 对一个状态, (这个状态能转换成不同的状态)原创 2022-03-25 22:04:04 · 172 阅读 · 0 评论 -
acwing 892 台阶nim游戏 2022/03/24
当奇数层台阶的异或值为0的时候,对手拿奇数,我们也拿奇数,对手拿偶数,我们也拿偶数(拿相同的石子到下一台阶 , 奇数偶数台阶)#include <iostream> using namespace std; /*先手必胜状态:先手操作完,可以走到某一个必败状态先手必败状态:先手操作完,走不到任何一个必败状态先手必败状态:a1 ^ a2 ^ a3 ^ ... ^an = 0先手必胜状态:a1 ^ a2 ^ a3 ^ ... ^an ≠ 0*/ int main(){..原创 2022-03-24 21:39:28 · 3501 阅读 · 0 评论 -
acwing 891 nim游戏 2022/03/21
异或 :1 ⊕ 1 = 00 ⊕ 0 = 01 ⊕ 0 = 10 ⊕ 1 = 1异或成对是0 , 是0不一定成对#include <iostream>using namespace std;/*先手必胜状态:先手操作完,可以走到某一个必败状态先手必败状态:先手操作完,走不到任何一个必败状态先手必败状态:a1 ^ a2 ^ a3 ^ ... ^an = 0先手必胜状态:a1 ^ a2 ^ a3 ^ ... ^an ≠ 0*/...原创 2022-03-21 20:33:19 · 3852 阅读 · 0 评论 -
acwing 890 能被整除的数 2022/03/20
从n中选几个的所有情况,可以认为每一个样本选与不选用位运算来枚举所有的数#include<iostream>using namespace std;typedef long long LL;const int N = 20;int p[N], n, m;int main() { cin >> n >> m; for(int i = 0; i < m; i++) cin >> p[i]; ...原创 2022-03-20 18:05:26 · 201 阅读 · 0 评论 -
acwing 844 高斯消元解异或方程组 2022/03/19
异或 : 不进位加法#include <iostream>#include <algorithm>using namespace std;const int N = 110;int n;int a[N][N];int gauss(){ int c,r; for(c=0,r=0;c<n;c++) { // 找主元 int t=-1; for(int i=r;i<..原创 2022-03-19 14:58:36 · 88 阅读 · 0 评论 -
acwing 833 高斯消元解线性方程 2022/03/17
c表示的是哪一列,r表示的是哪一行#include <iostream>#include <cstring>#include <algorithm>#include <cmath>using namespace std;const int N = 110;const double eps = 1e-8;int n;double a[N][N];int gauss() // 高斯消元,答案存于a[i][n]中,...原创 2022-03-17 21:10:07 · 257 阅读 · 0 评论 -
acwing 878 线性同余方程 2022/03/11
这里的d是最大公约数,如果要变成是b的话(原式相当于ax+my=b)只需要乘 b / d 就可所以最后x乘以b / d#include<bits/stdc++.h>using namespace std;int n, x, y;using LL = long long ;int exgcd(int a, int b, int &x, int &y){ if (!b) { x = 1, y = 0; retur..原创 2022-03-11 22:58:45 · 210 阅读 · 0 评论 -
acwing 877 扩展欧几里得算法 2022/03/11
(a , b) 是 a和b 的最大公约数#include<iostream>using namespace std;int exgcd(int a,int b,int &x,int &y) { if(!b) { x = 1 , y = 0 ; return a ; } int d = exgcd(b ,...原创 2022-03-11 21:41:49 · 261 阅读 · 0 评论 -
acwing 875 快速幂 2022/03/10
#include<iostream>using namespace std ;typedef long long LL ;int qmi(int a , int k , int p){ int res = 1 ; while(k) { if(k & 1) { res = (LL)res * a % p ; } k = k >> 1 ; ...原创 2022-03-10 22:21:35 · 123 阅读 · 0 评论 -
acwing 872 最大公约数 欧几里得算法(辗转相除法) 2022/03/09
#include<iostream>using namespace std ;int n ;int get(int a , int b){ return a ? get(b % a , a) : b ; }int main(){ cin >> n ; while(n--) { int x , y ; cin >> x >> y ; ...原创 2022-03-09 21:17:24 · 164 阅读 · 0 评论 -
acwing 870 约数个数 2022/03/09
#include<iostream>#include<unordered_map>using namespace std ;typedef long long LL ;const int N = 1e9 + 7 ;int main(){ int n , a; cin >> n ; unordered_map<int, int> pj ; while(n--) { cin &g...原创 2022-03-09 20:27:25 · 114 阅读 · 0 评论 -
acwing 869 试除法求约数 2022/03/09
成对约数,d <= n /d所以遍历成对约数的时候,遍历到 n / i 就行了#include<iostream>#include<algorithm>#include<vector>using namespace std ;int n ;vector<int> get(int n){ vector<int> res ; for(int i = 1 ; i <= n/i ; i++) {原创 2022-03-09 12:07:22 · 98 阅读 · 0 评论 -
acwing 868. 筛质数 2022/03/09
#include <iostream>#include <algorithm>using namespace std;const int N = 1000010;//primes数组用来存放质数int primes[N], cnt;//st[i], i为质数则为false否则为truebool st[N];void get_primes(int n){ for(int i = 2; i <= n; i++)...原创 2022-03-09 11:20:55 · 116 阅读 · 0 评论 -
acwing 867 分解质因数 2022/03/08
因为如果有两个的话,乘起来就大于n了#include<iostream>#include<algorithm>using namespace std;int main(void){ int n;cin>>n; while(n--) { int a;cin>>a; for(int i=2;i<=a/i;i++) { if(a%i==0) ...原创 2022-03-08 23:13:47 · 182 阅读 · 0 评论 -
acwing 861 二分图的最大匹配 2022/03/08
遍历男生,把女生清空,表示这些女生都还没有考虑过如果这个男生找到了一个女生,最后输出结果#include<iostream>#include<cstring>using namespace std ;const int N = 510 , M = 100010 ;int n1, n2 , m ;int e[M] , h[N] , ne[M] , idx ;int match[N] ;bool st[N] ;void add(int a , int b..原创 2022-03-08 23:02:39 · 244 阅读 · 0 评论 -
acwing 866 试除法判定质数 2022/03/08
最暴力的算法是on的时间复杂度一个数的因数都是成对出现的:例如12的因数有3和4,2和6所以我们可以只枚举较小的那一个,即根下n,假设较小的为d,较大的为n/d;#include<iostream>using namespace std ;bool xlx(int x){ if(x < 2) return false ; for(int i = 2 ; i * i <= n ; i++) { if(x % i ==原创 2022-03-08 22:35:04 · 157 阅读 · 0 评论 -
acwing 860 染色法判定二分图 2022/03/08
#include<iostream>#include<cstring>using namespace std ;const int N = 2e5 + 10 ;int n , m ;int e[N] , h[N] , ne[N] , idx ;int color[N] ;void add(int a , int b){ e[idx] = b , ne[idx] = h[a] , h[a] = idx ++ ;}bool dfs(int u ...原创 2022-03-08 16:44:26 · 223 阅读 · 0 评论 -
acwing 859 kruskal算法 重载运算符 2022/03/07
这是别人的视频,up主是 crazyOIER5分钟学会重载运算符_哔哩哔哩_bilibili就是重新定义了运算符的意义#include<iostream>#include<algorithm>using namespace std;const int N=1e5+10,M=2*N;int n,m;// 并查集 int p[N];struct Edge{ int a,b,w; // 重载小于运算符 bool operator< (c..原创 2022-03-07 23:29:02 · 217 阅读 · 0 评论 -
acwing 837 连通块中点的数量 2022/03/06
#include<iostream>using namespace std ;const int N = 1e5 + 10 ;int p[N] ,si[N] ;int n , m ;int find(int x){ if(p[x] != x) p[x] = find(p[x]); return p[x] ;}int main(){ cin >> n >> m ; for(int i = 1 ; i <= n ; i.原创 2022-03-06 23:34:27 · 83 阅读 · 0 评论 -
acwing 854 Floyd 求最短路
三重循环#include<iostream>#include<algorithm>using namespace std ;const int N = 209 , INF = 1e9 ;int n , m , k; int dist[N][N] ;void foyld(){ for(int k = 1 ; k<=n ; k++) { for(int i = 1 ; i <= n ; i++) ...原创 2022-02-09 15:53:47 · 6390 阅读 · 0 评论 -
acwing 154 滑动窗口(单调队列) 2021/12/19
给定一个大小为 n≤106的数组。有一个大小为k的滑动窗口,它从数组的最左边移动到最右边。你只能在窗口中看到k个数字。每次滑动窗口向右移动一个位置。以下是一个例子:该数组为[1 3 -1 -3 5 3 6 7],k为33。窗口位置 最小值 最大值 [1 3 -1] -3 5 3 6 7 -1 3 1 [3 -1 -3] 5 3 6 7 -3 3 1 3 [-1 -3 5] 3 6 7 -3 5 1 3 -...原创 2021-12-19 23:47:26 · 106 阅读 · 0 评论 -
acwing 830 单调栈 2021/12/19
给定一个长度为N 的整数数列,输出每个数左边第一个比它小的数,如果不存在则输出−1。输入格式第一行包含整数N,表示数列长度。第二行包含N个整数,表示整数数列。输出格式共一行,包含N个整数,其中第i个数表示第i个数的左边第一个比它小的数,如果不存在则输出−1。数据范围1≤N≤1051≤数列中元素≤109输入样例:53 4 2 7 5输出样例:-1 3 -1 2 2要大于等于,因为如果相邻的两个数相等的话,也要tt--这道题...原创 2021-12-19 22:41:20 · 226 阅读 · 0 评论 -
acwing 829 模拟队列 2021/12/18
实现一个队列,队列初始为空,支持四种操作:push x– 向队尾插入一个数x; pop– 从队头弹出一个数; empty– 判断队列是否为空; query– 查询队头元素。现在要对队列进行MM个操作,其中的每个操作3和操作4都要输出相应的结果。输入格式第一行包含整数M,表示操作次数。接下来MM行,每行包含一个操作命令,操作命令为push x,pop,empty,query中的一种。输出格式对于每个empty和query操作都要输出一个查...原创 2021-12-18 20:27:29 · 96 阅读 · 0 评论