ACM
ACM训练记录
Spikeeee-
消磨到死的那些希望的亡灵:苍白、凄惨、哭的哀伤。
展开
-
排序算法总结篇 - (堆排序, 快速排序(链表, 数组),直接插入排序,选择排序,冒泡排序, 归并排序)
1.冒泡排序(Bubble Sort):#include <stdio.h>void Pri(int a[],int s) { for(int i = 0; i < s; ++i) { printf("%d ", a[i]); } printf("\n"); return;}void Swap(int *a,int *b) { int t = *a; *a = *b; *b = t; return;}void Bubble_Sort(int a[],i原创 2021-04-20 15:01:28 · 142 阅读 · 0 评论 -
4.5 联机局部搜索(LRTA*(learn real time A*) - agent) --- 实现代码附详细注释
是不是英美思维逻辑习惯从后往前啊还是按着自己的理解重新写了一下很简单的算法 但是很有效局部对单一状态判别, 不断更新H(s):从s到达目标的估计代价,且将每走一步的权值加入了计算,而不仅仅只是启发函数,这样使得看起来最优的方案,如果其是错误的,到达目标的预估代价会越变越大import mathdirections4 = {'N': (-1, 0), 'E': (0, 1), 'S': (1, 0), 'W': (0, -1)}# directions8 = dict(directions4原创 2021-04-20 14:50:35 · 877 阅读 · 1 评论 -
4.5 联机搜索(online-dfs-agent) --- 实现代码附详细注释
蛮简单的算法,但因为对python不太熟悉,实现过程不是一般的痛苦…联机dfs传统dfs的区别只是要维护一个回溯序列和一个解序列启发式函数使用的是与目标的曼哈顿距离,在一些极限条件下,因为每次只能感知一步,所以效果并不好,但总体情况下还是具有一定优化能力整体流程见书中描述:Python代码:import mathdirections4 = {'W': (-1, 0), 'N': (0, 1), 'E': (1, 0), 'S': (0, -1)}directions8 = dict(dir原创 2021-04-17 15:09:44 · 759 阅读 · 2 评论 -
4.3 与或搜索树(and-or-search) --- 实现代码附详细注释(待完善)
书上好像使用的状态的映射写的, 这样在List上更加直观但是感觉普适性较低,所以本人用多个参量记录状态但是在输出表结构上出现了问题主要的点是不太明白为什么在循环中遇到了一个出现过的节点便直接退出,那不会忽略它后继节点的状态嘛?并且由于刚上手Python几日, 对List还是不太熟, 所以对输出有点困难,基本程序结构是正确的, 可以调试来进行阅读与或搜索树只适用于连续空间离散化的一个时间片进行处理,从而得到一个解序列的算法目的是通过该解序列,计算启发式函数,得出启发值,整体可以通过每个时间片得出的原创 2021-04-15 22:57:56 · 1311 阅读 · 2 评论 -
3.6 八数码问题 - 启发式搜索(A* Search) --- 实现代码附详细注释
参考博客:https://blog.csdn.net/qq_32400847/article/details/51816685#include<cmath> #include<queue> #include<cstdio> #include<cstring> #include<iostream>#include<algorithm> using namespace std; int Hash[15]原创 2021-04-12 17:14:48 · 249 阅读 · 0 评论 -
4.1 八皇后问题 - 遗传算法(genetic-algorithm) --- 实现代码附详细注释
久闻遗传算法大名,算法实现的好有趣书上讲解十分详细且精简,所以不进行赘述了,建议直接看书上介绍遗传算法并不保证你能获得问题的最优解,但是使用遗传算法的最大优点在于你不必去了解和操心如何去“找”最优解。而只要简单的“否定”一些表现不好的个体就行了。C++实现代码:#include <iostream>#include <time.h>#include <stdlib.h>#include <algorithm>#include <cm原创 2021-04-14 16:29:28 · 331 阅读 · 0 评论 -
4.1 八皇后问题 - 传统dfs,爬山法,首选爬山法,模拟退火 --- 实现代码附详细注释
传统解法 - 广度优先搜索(DFS):#include <iostream>#include <cstdio>using namespace std;#define ll long longconst int maxn = 200005;const int INF = 0x3f3f3f;int n,a[50],ans = 0;bool vis[3][50];int cnt = 0;void dfs(int i) { ++cnt; if(i ==原创 2021-04-13 23:55:51 · 752 阅读 · 0 评论 -
3.4.5 迭代加深的深度优先搜索(iterative-deepening search) --- 实现代码附详细注释
用链式前向星或者邻接表存图会更加方便的 懒得改了就这样吧 注释之后有时间补上因为dfs是相同代价搜索 所以路径代价没有用处import pandas as pdimport sysfrom pandas import Series, DataFrame # 城市信息:city1 city2 path_cost_city_info = None # 按照路径消耗进行排序的FIFO,低路径消耗在前面# 优先队列_frontier_priority = [] # 节点数据结构...原创 2021-04-07 15:23:51 · 2208 阅读 · 1 评论 -
3.4.2 一致代价搜索 (uniform-cost search) --- 实现代码附详细注释
import pandas as pdfrom pandas import Series, DataFrame # 城市信息:city1 city2 path_cost_city_info = None # 按照路径消耗进行排序的FIFO,低路径消耗在前面# 优先队列_frontier_priority = [] # 节点数据结构class Node: def __init__(self, state, parent, action, path_cost): ...原创 2021-04-03 18:26:09 · 1906 阅读 · 1 评论 -
Running Routes (区间dp)
题目链接:https://ac.nowcoder.com/acm/contest/13168/Hdp[i][j] 表示从第i点到第j点所含不相交边数dp转移方程:dp[i][j] = max(dp[i][j], dp[i][G(t)] + dp[G(t+1)][j]);#include <iostream>#include <cstdio>#include <stdio.h>#include <map>#include <cmath>原创 2021-03-23 16:46:31 · 93 阅读 · 0 评论 -
Lowest Common Ancestor(位运算)
题目链接: https://ac.nowcoder.com/acm/contest/12794/J十六进制每位转化为四位二进制#include <iostream>#include <algorithm>#include <cstring>#include <vector>#include <stack> #include <cmath>using namespace std;#define ll long long原创 2021-03-16 16:13:53 · 132 阅读 · 0 评论 -
Pegasus Circle Shortcut(计算几何)
题目链接:https://ac.nowcoder.com/acm/contest/12794/I已知三点坐标:A (X1,Y1) B (X2,Y2) C (X3,Y3)AB向量:(X2-X1,Y2-Y1)AC向量:(X3-X1,Y3-Y1)BC向量:(X3-X2,Y3-Y2)COS∠A=[(X2-X1)(X3-X1)+(Y2-Y1)(Y3-Y1)]/|AB||AC|其中:|AB|=[(X2-X1)2+(Y2-Y1)2]^0.5|AC|=[(X3-X1)2+(Y3-Y1)2]^0.5∠A =原创 2021-03-16 14:10:09 · 104 阅读 · 0 评论 -
NIH Budget(背包dp)
题目链接:https://ac.nowcoder.com/acm/contest/12794/E#include <iostream>#include <cstdio>#include <stdio.h>#include <map>#include <cmath>#include <utility>#include <cstring>#include <string.h>#include <原创 2021-03-15 23:34:12 · 133 阅读 · 0 评论 -
Interstellar Love (并查集求无向图连通分量和闭环数)
题目链接:https://ac.nowcoder.com/acm/contest/12794/F并查集添边时若该边重复添加,则证明该连通分量中有闭环,注意这点的维护!!!#include<iostream>#include<algorithm>#include<cstring>#include <vector>#include<stack> using namespace std;const int maxn = 500000 +原创 2021-03-15 23:32:34 · 253 阅读 · 0 评论 -
Full Depth Morning Show (树形dp)
题目链接:https://ac.nowcoder.com/acm/contest/12606/I解题思路:#include <iostream>#include <cstdio>#include <cmath>#include <vector>using namespace std;#define ll long longconst int maxn = 100005;struct Path{ ll v,w;};ll t[m原创 2021-03-09 23:52:05 · 148 阅读 · 0 评论 -
Weird Flecks, But OK (最小圆覆盖)
题目链接:https://ac.nowcoder.com/acm/contest/12606/A等效为三个平面,在三个平面分别求能够覆盖所有节点的最小圆直径参考dalao的博客:最小圆覆盖三角形外接圆圆心计算#include <iostream>#include <cstdio>#include <cmath>using namespace std;const int maxn = 5005;int n;struct Node{ doubl原创 2021-03-09 15:46:29 · 315 阅读 · 0 评论 -
Pulling Their Weight (模拟水题)
题目链接: https://ac.nowcoder.com/acm/contest/12606/F#include <iostream>#include <cstdio>#include <cmath>#include <vector>#include <algorithm>using namespace std;#define ll long longll a[100005];ll n;ll sum = 0;int mai原创 2021-03-08 23:36:02 · 322 阅读 · 0 评论 -
Early Orders (贪心,单调栈)
题目链接:https://ac.nowcoder.com/acm/contest/12606/E贪心选择小数放在前面,且保证每个数均出现一次#include <iostream>#include <cstdio>#include <cmath>#include <vector>#include <stack>using namespace std;#define ll long longconst int maxn = 20000原创 2021-03-08 23:31:54 · 177 阅读 · 0 评论 -
Some Sum (数论)
题目链接:https://ac.nowcoder.com/acm/contest/12606/D注意是连续的一段整数!!比赛时没看到卡了好久奇数:(n/2 奇) + (n/2+1 偶) 或 (n/2 偶) + (n/2+1 奇) eithern/2%2 == 1 : 存在奇数个奇数加和n/2%2 == 0: 存在偶数个奇数加和#include <iostream>#include <cstdio>#include <cmath>using namespa原创 2021-03-08 19:49:32 · 152 阅读 · 0 评论 -
On Average They‘re Purple (bfs求最短路)
题目链接:https://ac.nowcoder.com/acm/contest/12606/H由分析得所求颜色变化数为最短路径上步数减一利用bfs求最短路径#include <iostream>#include <cstdio>#include <cmath>#include <vector>#include <queue>using namespace std;#define ll long longconst int IN原创 2021-03-08 19:43:45 · 213 阅读 · 0 评论 -
n的约数 n = p1^a1*p2^a2*... (ACM学习笔记)
pi = i ; ai = vis[ i ] ;void euler_phi(int n) { memset(vis, 0, sizeof(vis)); printf("n:%d\n", n); for (int i = 2; i <= n; i++) { while (n % i == 0) { vis[i]...原创 2020-04-28 16:03:23 · 373 阅读 · 0 评论 -
小于n且与n互素的整数个数-欧拉phi函数 (ACM学习笔记)
基于欧拉函数推导: α(n) = n(1 - 1/p1)(1 - 1/p2)(1 - 1/p3)…(1 - 1/pk)int euler_phi_1(int n) { memset(vis, 0, sizeof(vis)); int ans = n; for (int i = 2; i <= n; i++) { if (n % i == 0) ans...原创 2020-04-28 15:59:00 · 384 阅读 · 0 评论 -
容斥原理 (ACM学习笔记)
|A ∪ B ∪ C| = |A| + |B| + |C| - |A ∩ B| - |B ∩ C| - |C ∩ A| + |A ∩ B ∩ C|左边是所有集合并的元素个数,右边是这些集合的"各种搭配".每个"搭配"都是若干集合的交集,且每一项前面的正负号取决于集合的个数 – 奇数个集合为正,偶数个集合为负....原创 2020-04-27 19:45:33 · 679 阅读 · 0 评论 -
XOR性质 (ACM学习笔记)
若 a ^ b = c则 a ^ c = b原创 2020-04-27 19:37:55 · 279 阅读 · 0 评论 -
快速幂取模-分治 (ACM学习笔记)
int pow_mod(int a, int n, int m) { if (n == 0) return 1; int x = pow_mod(a, n / 2, m); long long ans = (long long)x * x % m; if (n & 1) ans = ans * a % m; return (int)ans;}原创 2020-04-27 15:57:03 · 115 阅读 · 0 评论 -
模运算性质-mod (ACM学习笔记)
(a + b) % n == (a % n + b % n) % n(a - b) % n == (a % n - b % n + n) % n(a * b) % n == ((a % n) * (b % n)) % n原创 2020-04-26 17:43:53 · 601 阅读 · 0 评论 -
扩展欧几里得(Euclid)算法-Exgcd (ACM笔记)
欧几里得算法 - gcd:int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b);}扩展欧几里得算法-Exgcd:void Exgcd(int a, int b, int& d, int& x, int& y) { if (!b) { d = a; x = 1; y = 0; } else { Exg...原创 2020-04-26 17:25:14 · 214 阅读 · 0 评论 -
Eratosthenes筛法-素数打表 (ACM笔记)
void era(int n) { int m = sqrt(n + 0.5); memset(vis, 0, sizeof(vis)); for (int i = 2; i <= m; i++) if(!vis[i]) for (int j = i * i; j <= n; j += i) vis[j] = 1;}原创 2020-04-26 14:44:17 · 199 阅读 · 0 评论 -
Binary Tree Traversals(ACM训练 - 对顶堆学习笔记)
A binary tree is a finite set of vertices that is either empty or consists of a root r and two disjoint binary trees called the left and right subtrees. There are three most important ways in which th...原创 2020-03-10 15:02:41 · 151 阅读 · 0 评论 -
分糖果 (ACM训练)
描述要开学了,老师买了一些糖果准备发给同学们吃。老师一共买了3种糖果,数量分别为a,b,ca,b,ca,b,c个 , 老师将会给每位同学3颗糖果 , 但是3颗糖果不能完全来自于同一种, 求最多能有多少个学生能分到糖果 ?注意: 糖果少于3颗时不能分给学生。输入3个整数a , b, c(1≤a,b,c≤10^6) 分别表示每种糖果的数量。输出输出一个整数,表示拿到糖果的学生的最大数量。样...原创 2020-03-06 18:36:47 · 516 阅读 · 0 评论 -
排名 (ACM训练)
*原题链接*AC代码:#include <iostream>#include<stdio.h>#include <algorithm>#include<queue>#include<vector>#include<string.h>#include<math.h>#include<map&g...原创 2020-03-06 18:25:04 · 167 阅读 · 0 评论 -
1442 Black Box (ACM训练)
原题链接二分查找后插入;(最好不要用cin,会超时)测试样例:Sample Input:7 43 1 -4 2 8 -1000 21 2 6 6Sample Output3:312#include<vector>#include<iostream>#include<stdio.h>using namespace std;vecto...原创 2020-03-06 18:16:34 · 204 阅读 · 0 评论 -
线段树 (ACM学习笔记)
#include<iostream>typedef long long ll;const int maxn = 1e5 + 5;using namespace std;ll arr[maxn] = { 0 }, tree[5 * maxn] = { 0 };void build(int node, int start, int end) { //建树 if (start == end) { tree[node] = arr[start]; return; } int le原创 2020-07-03 22:41:36 · 89 阅读 · 0 评论 -
双向链表(Doubly Linked List) (ACM学习笔记)
//Doubly Linked List#include <stdio.h>#include <string.h>#include <stdlib.h>#include <algorithm>using namespace std;struct node { int date; struct node* next; struct node* pre;};struct node* Creat_list(int n)原创 2020-05-20 20:04:36 · 178 阅读 · 0 评论 -
洛谷 P6559 [SBCOI2020]小镇 (ACM训练)
对于80%样例:#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>#include<cmath>#include<map>using namespace std;const int MAXN = 1e5 + 5;inline int read(){ int x = 0, f = 1; char c = getchar..原创 2020-05-18 16:47:53 · 228 阅读 · 0 评论 -
Proud Merchants - (ACM训练)
原题地址#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>#include<cmath>using namespace std;const int maxn = 10005;const int INF = 0x3f3f3f3f;typedef long long ll;struct Node { int P, Q, V;}p[maxn]原创 2020-05-11 14:16:16 · 140 阅读 · 0 评论 -
汉诺塔问题-斐波那契解法
#include<cstdio>#include<iostream>#include<algorithm>#include<cmath>using namespace std;typedef long long ll;int main() { int n; while (scanf("%d", &n) != EOF) printf("%d\n", pow(2, n) - 1);}原创 2020-05-11 14:00:34 · 148 阅读 · 0 评论 -
0-1背包问题 -- 动态规划,分支界限
d[i][j] 为选择体积编号为i-n的物体放如容量为j的背包中的最大重量#include<cstdio>#include<iostream>#include<algorithm>#include<cmath>using namespace std;const int maxn = 10005;const int INF = 0x3f3...原创 2020-05-06 15:09:20 · 304 阅读 · 0 评论 -
物品无限的01背包问题-动态规划 (ACM学习笔记)
物品无限的背包问题:有n种物品,每种均有无穷多个。第i种物品的体积为Vi,重量 为Wi。选一些物品装到一个容量为C的背包中,使得背包内物品在总体积不超过C的前提下重量尽量大。1≤n≤100,1≤Vi ≤C≤10000,1≤Wi≤106#include<cstdio>#include<iostream>#include<algorithm>#includ...原创 2020-05-06 13:39:47 · 3081 阅读 · 0 评论 -
硬币问题-动态规划 (ACM学习笔记)
#include<cstdio>#include<iostream>#include<algorithm>#include<cmath>using namespace std;const int maxn = 10000005;const int INF = 0x3f3f3f3f;typedef long long ll;int V[...原创 2020-05-05 16:41:58 · 153 阅读 · 0 评论