【PTA】刷题总结(持续更新)

好像CSDN目前不支持代码收缩的功能,页面有拖沓,可以根据目录来寻找位置。或者之后我会分个不同板块

目录

一、考点

二、代码小知识点

三、算法模板

1、堆排序

2、树的排序

3、LCA

四、例题


一、考点

1、排序:堆排序、插入排序

2、图论:拓扑图、最短路径(基本dijkstra)、遍历(深搜广搜)

3、树:前中后层遍历、前中求后/后中求前(前后求中【不唯一】)、完全二叉、平衡二叉(AVL)、二叉查找(BST)

4、其他:并查集、快乐模拟、哈希、背包、最大不下降(LIS)、最长公共(LCS)、分块思想、最大连续和、最长回文、最近公共祖先(LCA)

二、代码小知识点

三、算法模板

1、堆排序

void downAd(int low, int high) {
	int i = low, j = i * 2;
	while (j <= high) {
		if (j + 1 <= high && temp[j + 1] > temp[j])
			j = j + 1;
		if (temp[j] > temp[i]) {
			swap(temp[j], temp[i]);
			i = j;
			j = i * 2;
		}
		else break;
	}
}

void heapSort() {
	bool flag = false;
	for (int i = n / 2; i >= 1; i--)downAd(i,n);
	for (int i = n; i > 1; i--) {
		swap(temp[i], temp[1]);
		downAd(1, i - 1);
	}
}

2、树的排序

//前后序获得中序
void getIn(int prel, int prer, int postl, int postr) {
	if (prel == prer) {
		in.push_back(pre[prel]);
		return;
	}
	if (pre[prel] == post[postr]) {
		int i = prel + 1;
		while(i <= prer && pre[i] != post[postr-1])i++;
		getIn(prel + 1, i - 1, postl, postl + (i - prel - 1) - 1);
		in.push_back(post[postr]);
		getIn(i, prer, postl + (i - prel - 1), postr - 1);
	}
}

3、LCA

(但是目前没用过,求LCA的题目都有更简便的做法)

#include<bits/stdc++.h>
using namespace std;
const int maxn = 500005;
struct edge{
    int next, to;
}e[maxn << 1];
int head[maxn], cnt;
int n, m, s, a, b, xx, yy;
int depth[maxn], fa[maxn][22];

void add(int u, int v)
{
    e[++cnt].to = v;
    e[cnt].next = head[u];
    head[u] = cnt;
}

void dfs(int now, int fath)
{
    depth[now] = depth[fath] + 1;
    fa[now][0] = fath;
    for(int i = 0; i <= 18; i++)
        fa[now][i+1] = fa[fa[now][i]][i];
    for(int i = head[now]; i != 0; i = e[i].next)
    {
        if(e[i].to != fath)
        dfs(e[i].to, now);
    }
}

int lca(int x, int y)
{
    if(depth[x] < depth[y]) swap(x, y);
    for(int i = 18; i >= 0; i--)
    {
        if(depth[fa[x][i]] >= depth[y]) x = fa[x][i];
        if(x == y) return x;
    }
    for(int i = 18; i >= 0; i--)
    {
        if(fa[x][i] != fa[y][i])
        {
            x = fa[x][i];
            y = fa[y][i];
        }
    }
    return fa[x][0];
}

int main()
{
    scanf("%d%d%d", &n, &m, &s);
    for(int i = 1; i < n; i++)    
    {
        scanf("%d%d", &xx, &yy);
        add(xx, yy); add(yy, xx);
    }
    dfs(s, 0);
    for(int i = 1; i <= m; i++)
    {
        scanf("%d%d", &a, &b);
        printf("%d\n", lca(a, b));
    }
    return 0;
}

4、并查集 

四、例题

1、dijkstra+DFS

1018 Public Bike Management (30 分)

2、AVL

1066 Root of AVL Tree (25 分)

1123 Is It a Complete AVL Tree (30分)(层次遍历/BFS)

3、LCS、LIS(DP)

1045 Favorite Color Stripe (30分)

题解:【PTA-A】1045 Favorite Color Stripe (30分)(LIS与LCS)_Netceor的博客-CSDN博客

4、背包问题

1068 Find More Coins (30分)

5、堆排序

1098 Insertion or Heap Sort (25分)

6、前后建立中序

1119 Pre- and Post-order Traversals (30分)

7、快乐模拟(快乐模拟的题目其实挺好理解,就是要细心耐心)

1014 Waiting in Line (30分)

8、zigzag层序遍历

1127 ZigZagging on a Tree (30分)

9、LCA

1151 LCA in a Binary Tree (30分)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值