PDF

暑假开始准备转移博客,试了几个都不怎么满意(我还去试了下LineBlog 不知道那时候在想什么。。),屯着一堆文章,,到时候一起发了

现在暂时转移至WordPress,不过还在完善中,预计。。算了不瞎预计的好。。

课上说最好做个代码集,嗯嗯 我也觉得挺有必要的

毕竟现在我连Floyd怎么写都忘了 无脑SPFA_(:з」∠)_

反正有用没用都稍微写一下,暂定是目录这些,有些还在找例题、整理代码什么的,所以还是空的。

GItHub上还欠了几题,之后会补上来。

我做的二级目录到博客园就被无视了,,将就看看吧

感觉实在简陋了些啊。。

题号以作业次数为准


STL


stack

头文件

#include<stcak>
using namespace std;

声明

stack<数据类型> 变量名;
a.empty() 判断栈是否为空
a.pop() 移除栈顶元素
a.push(b) 将元素b压入栈中
a.size() 返回栈中元素个数
a.top() 返回栈顶元素

queue

头文件

#include<queue>
using namespace std;

声明

queue<数据类型> 变量名;
a.empty() 判断队列是否为空
a.pop() 将队头元素出队
a.push(b) 将元素b入队
a.size() 返回队列中元素个数
a.front() 返回队头元素
a.back() 返回队尾元素

priority_queue

头文件

#include<queue>
using namespace std;

声明

priority_queue<数据类型> 变量名;
a.empty() 判断队列是否为空
a.pop() 移除队头元素
a.push(b) 将元素b入队
a.size() 返回队列中元素个数
a.top() 返回队头元素 //默认从大到小
//从小到大&&多关键字
struct t
{
int p, q;
};
priority_queue<t> a[n];
bool operator < (t x, t y)
{
return x.p < y.p;
}

sort

头文件

#include<algorithm>
using namespace std;
//从小到大
int a[n];
sort(a,a+n); //从大到小
int compare(int x, int y)
{
return x > y;
}
sort(a, a + 3, compare); //多关键字
struct t
{
int p, q;
};
t a[n];
int compare(t x, t y)
{
if (x.p == y.p) return x.q > y.q;
else return x.p > y.p;
}
sort(a, a+n, compare);

功能函数


MAX

int max(int x, int y)
{
return x > y ? x : y;
}

MIN

int min(int x, int y)
{
return x < y ? x : y;
}

最大公约数

int gcd(int x, int y)
{
if (y == 0) return x;
else return gcd(y, x%y);
}

基础算法与数据结构


快速排序

#include<iostream>
using namespace std; int i, j, k, n, m, s, t, a[1000]; void q(int l, int r)
{
int i, j, x, t;
i = l;
j = r;
x = a[(i + j) / 2];
do
{
while (a[i] < x) i++;
while (a[j] > x) j--;
if (i <= j)
{
t = a[i];
a[i] = a[j];
a[j] = t;
i++;
j--;
}
} while (i <= j);
if (j > l) q(l, j);
if (i < r) q(i, r);
} int main()
{
cin >> n;
for (i = 1; i <= n; i++)
cin >> a[i];
q(1, n);
for (i = 1; i <= n; i++)
cout << a[i] << ' ';
return 0;
}

归并排序

2.1 nxd

给定 n 个数 a1,a2,...,an,求满足条件的(i,j)数量: i < j 且 a[i] < a[j]

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std; int a[200000], b[200000];
__int64 s; void p(int l, int m, int r)
{
int i = l;
int j = m + 1;
int k = l;
while (i <= m && j <= r)
{
if (a[i] < a[j])
{
b[k++] = a[j++];
s += m - i + 1;
}
else
{
b[k++] = a[i++];
}
}
while (i <= m) b[k++] = a[i++];
while (j <= r) b[k++] = a[j++];
for (i = l; i <= r; i++)
a[i] = b[i];
} void q(int l, int r)
{
if (l < r)
{
int m = (l + r) >> 1;
q(l, m);
q(m + 1, r);
p(l, m, r);
}
} int main()
{
int n;
scanf("%d", &n);
for (int i = 0; i<n; i++)
scanf("%d", &a[i]);
s = 0;
q(0, n - 1);
printf("%I64d", s);
return 0;
}

表达式求值(调度场算法)

3.2 calculator

#include<stdio.h>
#include<string.h> int i, j, k, n, m, s, t, a[1000];
char b[2000], c[2000], d[2000]; int main()
{
scanf("%s", &b);
i = 0;
j = 0;
k = 0;
n = strlen(b);
//中缀转后缀
while (i < n)
{
if ((b[i] >= '0') && (b[i] <= '9'))
{
while ((b[i] >= '0') && (b[i] <= '9'))
{
c[j++] = b[i++];
}
c[j++] = '!';
}
if ((b[i] == '+') || (b[i] == '-'))
{
while ((k > 0) && (d[k - 1] != '('))
{
c[j++] = d[k - 1];
k--;
}
d[k++] = b[i];
}
if ((b[i] == '*') || (b[i] == '/'))
{
while ((k > 0) && (d[k - 1] != '(') && ((d[k - 1] == '*') || (d[k - 1] == '/')))
{
c[j++] = d[k - 1];
k--;
}
d[k++] = b[i];
}
if (b[i] == '(')
{
d[k++] = b[i];
}
if (b[i] == ')')
{
while ((k > 0) && (d[k - 1] != '('))
{
c[j++] = d[k - 1];
k--;
}
if (k > 0) k--;
}
i++;
}
while (k > 0)
{
c[j++] = d[k - 1];
k--;
}
//计算后缀
c[j] = '\0';
i = 0;
j = -1;
while (c[i] != '\0')
{
if ((c[i] >= '0') && (c[i] <= '9'))
{
double x = 0;
while ((c[i] >= '0') && (c[i] <= '9'))
{
x = 10 * x + c[i] - '0';
i++;
}
j++;
a[j] = x;
}
else
{
j--;
switch (c[i])
{
case '+':
{
a[j] += a[j + 1];
break;
}
case '-':
{
a[j] -= a[j + 1];
break;
}
case '*':
{
a[j] *= a[j + 1];
break;
}
case '/':
{
a[j] /= a[j + 1];
break;
}
}
}
i++;
}
printf("%d", a[j]);
return 0;
}

线段树求区间和

5.2 bubble_sort

#include<stdio.h>

int i, j, k, n, m, s, t, a[300001], b[100001], c[100001];

int min(int x, int y)
{
return x < y ? x : y;
}
int max(int x, int y)
{
return x > y ? x : y;
}
int p(int l, int r)
{
int s;
s = 0;
l += m - 1;
r += m + 1;
while ((l^r != 1) && (l != r))
{
if (l & 1 == 0) s += a[l ^ 1];
if (r & 1 == 1) s += a[r ^ 1];
l >>= 1;
r >>= 1;
}
return s;
} void q(int k)
{
k >>= 1;
while (k > 1)
{
a[k] = a[k << 1] + a[(k << 1) + 1];
k >>= 1;
}
} int main()
{
scanf("%d", &n);
for (i = 1; i <= n; i++)
scanf("%d", &b[i]);
m = 1;
while (m <= n) m <<= 1;
for (i = m + 1; i <= m + n; i++)
a[i] = 1;
for (i = m - 1; i >= 1; i--)
a[i] = a[i << 1] + a[(i << 1) + 1];
for (i = 1; i <= n; i++)
{
t = p(1, b[i] - 1) + i;
c[b[i]] = max(b[i], max(t, i)) - min(b[i], min(t, i));
a[m + b[i]] = 0;
q(m + b[i]);
}
printf("%d", c[1]);
for (i = 2; i <= n; i++)
printf(" %d", c[i]);
return 0;
}

AVL树(不包含删除操作)

8.1 wbhavl

#include<stdio.h>
#include<stdlib.h> int i, j, k, n, m, s, t, a[100001]; struct node
{
int dep;
int val;
node *p;
node *l;
node *r;
}; node* insert(node *tree, int value);
void updata(node *tree);
int depth(node *tree);
node* aaaavl(node *tree, node *newp);
int papa(node *tree);
node* leftSingle(node *tree);
node* rightSingle(node *tree);
node* leftDouble(node *tree);
node* rightDouble(node *tree);
int haha(node *tree, int pp); node* insert(node *tree, int value)
{
node *newp, *nowp;
newp = new node;
newp->val = value;
newp->p = NULL;
newp->l = NULL;
newp->r = NULL;
if (tree == NULL)
{
newp->dep = 1;
tree = newp;
}
else
{
nowp = tree;
while (1 > 0)
{
if (newp->val <= nowp->val)
{
if (nowp->l == NULL)
{
nowp->l = newp;
newp->p = nowp;
break;
}
else
{
nowp = nowp->l;
continue;
}
}
else
{
if (nowp->r == NULL)
{
nowp->r = newp;
newp->p = nowp;
break;
}
else
{
nowp = nowp->r;
continue;
}
}
}
updata(newp);
tree = aaaavl(tree, newp);
}
return tree;
} void updata(node *tree)
{
if (tree == NULL) return;
else
{
int l, r;
l = depth(tree->l);
r = depth(tree->r);
tree->dep = 1 + (l > r ? l : r);
}
} int depth(node *tree)
{
if (tree == NULL) return 0;
else return tree->dep;
} node* aaaavl(node *tree, node *newp)
{
int pa;
while (newp != NULL)
{
updata(newp);
pa = papa(newp);
if ((pa < -1) || (pa > 1))
{
if (pa > 1)
{
if (papa(newp->r) > 0)
{
newp = leftSingle(newp);
}
else
{
newp = leftDouble(newp);
}
}
if (pa < -1)
{
if (papa(newp->l) < 0)
{
newp = rightSingle(newp);
}
else
{
newp = rightDouble(newp);
}
}
if (newp->p == NULL) tree = newp;
break;
}
newp = newp->p;
}
return tree;
} int papa(node *tree)
{
if (tree == NULL) return 0;
else return depth(tree->r) - depth(tree->l);
} node* leftSingle(node *tree)
{
node *newroot, *mature;
mature = tree->p;
newroot = tree->r;
if (newroot->l != NULL)
{
newroot->l->p = tree;
}
tree->r = newroot->l;
updata(tree);
newroot->l = tree;
newroot->p = mature;
if (mature != NULL)
{
if (mature->l == tree)
{
mature->l = newroot;
}
else
{
mature->r = newroot;
}
}
tree->p = newroot;
updata(newroot);
return newroot;
} node* rightSingle(node *tree)
{
node *newroot, *mature, *naive;
mature = tree->p;
newroot = tree->l;
if (newroot->r != NULL)
{
newroot->r->p = tree;
}
tree->l = newroot->r;
updata(tree);
newroot->r = tree;
newroot->p = mature;
if (mature != NULL)
{
if (mature->l == tree)
{
mature->l = newroot;
}
else
{
mature->r = newroot;
}
}
tree->p = newroot;
updata(newroot);
return newroot;
} node* leftDouble(node *tree)
{
rightSingle(tree->r);
return leftSingle(tree);
} node* rightDouble(node *tree)
{
leftSingle(tree->l);
return rightSingle(tree);
} int haha(node *tree, int pp)
{
node *nowp;
int qq;
qq = 1;
nowp = tree;
while (nowp)
{
if (nowp->val > pp)
{
nowp = nowp->l;
qq++;
}
else
{
if (nowp->val < pp)
{
nowp = nowp->r;
qq++;
}
else break;
}
}
return qq;
} int main()
{
node *tree, *now;
int val;
tree = NULL;
scanf("%d", &n);
for (i = 0; i < n; i++)
{
scanf("%d", &a[i]);
tree = insert(tree, a[i]);
}
printf("%d", haha(tree, a[0]));
for (i = 1; i < n; i++)
printf(" %d", haha(tree, a[i]));
return 0;
}

k叉哈夫曼树(求合并n个数的最小代价)

也可用堆或优先队列

9.1 hbsz

#include<stdio.h>
#include<algorithm>
using namespace std; int i, j, k, n, m, s, t, b[100002];
short int a[100002]; int main()
{
scanf("%d", &n);
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
sort(a, a + n);
t = 0;
i = 0;
j = 0;
s = 0;
while (n - i + t - j > 1)
{
m = 0;
for (k = 0; k < 2; k++)
{
if (i == n)
{
m += b[j];
j++;
}
else
if (j == t)
{
m += a[i];
i++;
}
else
if (a[i] < b[j])
{
m += a[i];
i++;
}
else
{
m += b[j];
j++;
}
}
s += m;
b[t] = m;
t++;
}
printf("%d", s);
return 0;
}

并查集(求图的连通性)

10.2 friends

#include<stdio.h>

struct node
{
int x, y;
}; node e[50010]; int i, j, k, n, m, s, t, x, y, d, l, a[50010], b[50010], f[50010], c[50010], p[50010], q[50010]; int aaaa(int x)
{
return f[x] == x ? x : f[x] = aaaa(f[x]);
} void qqq(int x)
{
int i, pp, qq;
pp = aaaa(x);
i = a[x];
while (i != 0)
{
if (p[e[i].y])
{
qq = aaaa(e[i].y);
if (pp != qq)
{
t--;
f[qq] = pp;
}
}
i = e[i].x;
}
} int main()
{
scanf("%d%d", &n, &m);
for (i = 0; i < n; i++)
{
f[i] = i;
}
l = 0;
for (i = 0; i < m; i++)
{
scanf("%d%d", &x, &y);
l++;
e[l].x = a[x];
a[x] = l;
e[l].y = y;
l++;
e[l].x = a[y];
a[y] = l;
e[l].y = x;
}
scanf("%d", &d);
for (i = 1; i <= d; i++)
{
scanf("%d", &b[i]);
c[b[i]] = 1;
}
t = 0;
for (i = 0; i < n; i++)
{
if (!c[i])
{
t++;
qqq(i);
p[i] = 1;
}
}
q[d + 1] = t;
for (i = d; i >= 1; i--)
{
t++;
qqq(b[i]);
p[b[i]] = 1;
q[i] = t;
}
for (i = 1; i <= d + 1; i++)
{
printf("%d\n", q[i]);
}
return 0;
}

SPFA求负权环

11.1 CrazyScientist

#include<stdio.h>

int i, j, k, n, m, s, t, p, a[2010], b[80010][3], c[2010];
bool d[2010]; void q(int k)
{
int i, j;
d[k] = true;
i = a[k];
while (i != 0)
{
j = b[i][0];
if (c[k] + b[i][1] < c[j])
{
c[j] = c[k] + b[i][1];
if ((d[j] == true) || (p == 1))
{
p = 1;
if (d[s] == true)
{
t = 1;
}
break;
}
q(j);
}
i = b[i][2];
}
d[k] = false;
} int main()
{
scanf("%d%d", &n, &m);
for (i = 1; i <= n; i++)
{
a[i] = 0;
c[i] = 0;
d[i] = false;
}
s = 0;
for (i = 1; i <= m; i++)
{
scanf("%d%d%d", &j, &k, &t);
s++;
b[s][0] = k;
b[s][1] = t;
b[s][2] = a[j];
a[j] = s;
}
scanf("%d", &s);
t = 0;
for (i = 1; i <= n; i++)
{
p = 0;
q(i);
if (t == 1) break;
}
if (t == 1)
printf("EL PSY CONGROO");
else
printf("ttt");
return 0;
}

SPFA求多源点最短路径(可直接作单源点用)

11.2 FuYihao

#include<stdio.h>
#include<string.h> int i, j, k, n, m, s, t, q, a[410][410] = { 0 }, b[410][410] = { 0 }, c[410], d[200010], e[410][410];
bool f[410]; void sasasa(int k)
{
int i, j, h, t;
if (k > 1)
{
j = 1;
for (i = 2; i < k; i++)
if (e[i][k] < e[j][k]) j = i;
for (i = 1; i <= n; i++)
e[k][i] = e[j][k] + e[j][i];
}
e[k][k] = 0;
f[k] = true;
d[1] = k;
h = 0;
t = 1;
while (h < t)
{
h++;
j = d[h];
f[j] = false;
for (i = 1; i <= n; i++)
{
if (e[k][i] > e[k][j] + a[j][i])
{
e[k][i] = e[k][j] + a[j][i];
if (f[i] == false)
{
t++;
d[t] = i;
f[i] = true;
}
}
}
}
} int main()
{
memset(a, 0x3f, sizeof(a));
memset(e, 0x3f, sizeof(e));
scanf("%d%d", &n, &m);
for (i = 0; i < m; i++)
{
scanf("%d%d%d", &j, &k, &t);
if ((a[j][k] != 0) && (t > a[j][k])) continue;
a[j][k] = t;
a[k][j] = t;
}
scanf("%d", &q);
for (i = 1; i <= n; i++)
{
memset(f, 0, sizeof(f));
sasasa(i);
}
while (q--)
{
scanf("%d%d", &j, &k);
if (e[j][k] != 0x3f3f3f3f)
{
if (q > 0) printf("%d\n", e[j][k]);
else printf("%d", e[j][k]);
}
else
{
if (q > 0) printf("-1\n");
else printf("-1");
}
}
return 0;
}

Dijkstra

直接手打的

#include<iostream>
#include<cstring>
using namespace std; int i, j, k, n, m, s, t, x, y, a[100][100], b[100] = { 0 }, d[100]; int main()
{
memset(a, 0x3f, sizeof(a));
cin >> n >> m;
for (i = 0; i < m; i++)
{
cin >> x >> y >> t;
a[x - 1][y - 1] = t;
a[y - 1][x - 1] = t;
}
cin >> x >> y;
x--;
y--;
for (i = 0; i < n; i++)
d[i] = a[x][i];
b[x] = 1;
d[x] = 0;
for (i = 0; i < n - 1; i++)
{
t = 0x3f3f3f3f;
k = -1;
for (j = 0; j < n; j++)
if ((b[j] == 0) && (d[j] < t))
{
k = j;
t = d[j];
}
if (k == -1) break;
b[k] = 1;
for (j = 0; j < n; j++)
if (d[k] + a[k][j] < d[j])
d[j] = d[k] + a[k][j];
}
cout << d[y];
return 0;
}

Floyd

11.2 FuYihao

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std; int i, j, k, n, m, s, t, a[410][410]; int main()
{
memset(a, 0x3f, sizeof(a));
cin >> n >> m;
for (i = 1; i <= n; i++)
a[i][i] = 0;
for (i = 0; i < m; i++)
{
scanf("%d%d%d", &j, &k, &t);
if (t < a[j][k])
{
a[j][k] = t;
a[k][j] = t;
}
}
for (k = 1; k <= n; k++)
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
if (a[i][j] > a[i][k] + a[k][j])
{
a[i][j] = a[i][k] + a[k][j];
}
cin >> t;
for (i = 0; i < t; i++)
{
cin >> j >> k;
if (a[j][k] == 0x3f3f3f3f) printf("%d\n", -1);
else printf("%d\n", a[j][k]);
}
return 0;
}

Summary的更多相关文章

  1. Summary of Critical and Exploitable iOS Vulnerabilities in 2016

    Summary of Critical and Exploitable iOS Vulnerabilities in 2016 Author:Min (Spark) Zheng, Cererdlong ...

  2. 三个不常用的HTML元素:<details>、<summary>、<dialog>

    前面的话 HTML5不仅新增了语义型区块级元素及表单类元素,也新增了一些其他的功能性元素,这些元素由于浏览器支持等各种原因,并没有被广泛使用 文档描述 <details>主要用于描述文档或 ...

  3. [LeetCode] Summary Ranges 总结区间

    Given a sorted integer array without duplicates, return the summary of its ranges. For example, give ...

  4. Network Basic Commands Summary

    Network Basic Commands Summary set or modify hostname a)     temporary ways hostname NEW_HOSTNAME, b ...

  5. Summary - SNMP Tutorial

    30.13 Summary Network management protocols allow a manager to monitor and control routers and hosts. ...

  6. Mac Brew Install Nginx Summary

    ==> Downloading https://homebrew.bintray.com/bottles/nginx-1.10.1.el_capitan.bot################# ...

  7. Leetcode: LFU Cache && Summary of various Sets: HashSet, TreeSet, LinkedHashSet

    Design and implement a data structure for Least Frequently Used (LFU) cache. It should support the f ...

  8. How to add taxonomy element to a summary view?

    [re: Orchard CMS] This caused me scratching my head for days and now I can even feel it's bleeding. ...

  9. (转) Summary of NIPS 2016

    转自:http://blog.evjang.com/2017/01/nips2016.html           Eric Jang Technology, A.I., Careers       ...

  10. leetcode-【中等题】228. Summary Ranges

    题目: 228. Summary Ranges Given a sorted integer array without duplicates, return the summary of its r ...

随机推荐

  1. Linux基础实操二

    实操一: 1) 新建用户natasha uid为1000,gid为555,备注信息为“master” 2) 修改natasha用户的家目录为/Natasha 3) 查看用户信息配置文件的最后一行 ca ...

  2. servlet 遇到的奇怪问题

    一. servlet URl 连接多了 %09 原因value里面多了个空格 value=" value'; 改成 value="value'; 二.servlet get方法可以 ...

  3. 常用ajax样例

    ---恢复内容开始--- // url(String):请求地址 // param(String):请求参数 // targetId(String):结果显示id function ajaxReq(u ...

  4. Duplicate 复制数据库 搭建Dataguard

    1 操作系统环境 此处隐藏具体信息 System IP-address db_name db_version Comment         Target DB         Auxiliary D ...

  5. Spring Cloud Eureka简介及原理

    Eureka是Netflix开发的服务发现组件,本身是一个基于REST的服务.Spring Cloud将它集成在其子项目spring-cloud-netflix中,以实现Spring Cloud的服务 ...

  6. 一张图看懂JVM之垃圾回收算法详解

    导读                                                                                                  ...

  7. linux基础练习题(1)

    Linux命令作业(关卡一) 练习题1 理解操作系统的作用,以及各种操作系统的不同 要求: 简述什么是OS 简述应用程序.硬件.OS的关系 列举出3种常见的操作系统 简述Ubuntu和Linux的关系 ...

  8. C. cltt的幸运数LCAtarjan

    /*C: cltt的幸运数 Time Limit: 1 s      Memory Limit: 128 MB Submit Problem Description 一棵树有n个节点,共m次查询,查询 ...

  9. Java集合中List,Set以及Map等集合体系详解(史上最全)

    https://blog.csdn.net/zhangqunshuai/article/details/80660974

  10. POJ 3713 Transferring Sylla【Tarjan求割点】

    题意:给出一个无向图,判断是否任意两点间都存在至少3条互相独立的路,独立指公共顶点只有起点和终点.算法:枚举每个点,删去后用Tarjan判断图中是否存在割点,如果存在则该图不满足三连通性.Tarjan ...