CSP-J 总结 进阶一

CSP-J 总结 进阶一

数学专题

1. 万能头文件

#include <bits/stdc++.h>
using namespace std;
// just in order to simplify code, better to know where the function belongs to 

2. 等差数列,等比数列。

数列类型通用公式求和公式
等差数列An = A1 + (n - 1) * dSn = 1/2 * n * (An + A1)
等比数列An = A1 * q[n-1]Sn = A1 * (1 - qn) / (1 - q)
12 + 22 + 32 + …An = n2Sn = 1/6 * n * (n + 1) * (2n + 1)
13 + 2 3 + 33 + …An = n3Sn = [1/2 * n * (n + 1)]2
12 + 32 + 52 + …An = (2n - 1)2Sn = 1/3 * n * (4n2 - 1)
2 + 6 + 12 + 20 + …An = n * (n + 1)
22 + 62 + 122 + 202 + …An = [n * (n + 1)]2
斐波那契数列An = An-1 + An-2
斐波那契数列引申An = An-i + An-j

3. 最大公约数问题,最小公倍数问题。

一般用辗转相除法获取最大公约数,最小公倍数是a*b/最大公约数。

// GCD : Greatest Common Divisor
int gcd(int a, int b) { // 递归实现 
   return b == 0 ? a : gcd(b, a % b);
}
int gcd(int a, int b) { // 迭代实现
   while (b != 0) {
       a %= b;
       swap(a, b);
   }
   return a;
}
// LCM : Least Common Multiple
int lcm(int a, int b) {
    return a * b / gcd(a, b);
}

很多个数的最大公约数,最小公倍数,可以先求出两个数的最大公约数和最小公倍数,然后用同样的方法求出第三个数的最大公约数和最小公倍数,以此类推。

int gcd_x(int A[], int size) {
    int g = gcd(A[0], A[1]);
    for (int i = 2; i < size; i++) {
        g = gcd(g, A[i]);
    }
    return g;
}
int lcm_x(int A[], int size) {
    int l = lcm(A[0], A[1]);
    for (int i = 2; i < size; i++) {
        l = lcm(l, A[i]);
    }
    return l;
}

4. 质数问题,埃氏筛选法。

循环方法判断一个数是否是质数。
多个质数时,用埃氏筛选将质数的倍数全部排除,代码如下:

vector<int> eratosthenes_sieve(int n) {
   vector<bool> is_prime(n + 1, true);
   vector<int> primes;
   is_prime[0] = is_prime[1] = false;
   for (int i = 2; i <= n; i++) {
       if (is_prime[i]) {
           primes.push_back(i);
           for (int j = i * i; j <= n; j += i) {
               is_prime[j] = false;
           }
       }
   }
   return primes;
}

基础搜索问题

1. 深度优先搜索

dfs的标准函数模板,dfs使用了栈的概念,利用递归函数实现。

#include <bits/stdc++.h>
using namespace std;

const int M = 100; // row
const int N = 100; // column
typedef struct {
    int x;
    int y;
    int step;
} node_t;
bool visited[M][N] = {0};
int adjcent[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int m_map[M][N] = {0};
// node_t m_path[M]; // un-comment when need to store path
bool node_is_valid(node_t next) {
    if ((next.x >= 0 && next.x < M && next.y >= 0 && next.y < N) && 
        (!visited[next.x][next.y] && m_map[next.x][next.y] != 0)) {
        return true;
    }
    return false;
}
void dfs(node_t cur, node_t finish) {
    if (cur.x == finish.x && cur.y == finish.y) {
        cout << cur.step << endl;
    }
    for (int i = 0; i < 4; i++) {
        node_t next = {cur.x + adjcent[i][0], cur.y + adjcent[i][1], cur.step + 1};
        if (node_is_valid(next)) {
            visited[next.x][next.y] = true;
            // m_path[next.step] = next; // used to store path
            dfs(next, finish);
            visited[next.x][next.y] = false; // comment this @ isolated island issue 
        }
    }
}
int main() {
    node_t start;
    node_t finish;
    cin >> start.x >> start.y;
    cin >> finish.x >> finish.y;
    for (int i = 0; i < M; i++) {
        for (int j = 0; j < N; j++) {
            cin >> m_map[i][j];
        }
    }
    visited[start.x][start.y] = true;
    // m_path[0] = start; // used to store start node in path
    dfs(start, finish);
}

有题目搜索方向增加,可以修改adjcent数组。
有题目统计孤岛个数,可以在主函数针对所有node做DFS或BFS,已搜过的点visited置true,不退回false。
有题目是不重复字母,visited数组代表的含义是那些字母被统计过,可以用visited[val - ‘A’]=true/false来表示。

2. 广度优先搜索

bfs的标准函数模板,bfs使用了队列的概念,利用标准库中的queue实现。

#include <bits/stdc++.h>
using namespace std;

const int M = 200; // row 
const int N = 200; // column
typedef struct {
    int x;
    int y;
    int step;
} node_t;
bool visited[M][N] = {0};
int adjcent[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int m_map[M][N];
bool node_is_valid(node_t next) {
    if ((next.x >= 0 && next.x < M && next.y >= 0 && next.y < N) && 
        (!visited[next.x][next.y] && m_map[next.x][next.y] != 0)) {
        return true;
    }
    return false;
}

void bfs(node_t start, node_t finish) {
    queue<node_t> q;
    q.push(start);
    visited[start.x][start.y] = true;
    while (!q.empty()) { // head < tail
        node_t cur = q.front(); // front node in queue
        q.pop();
        for (int i = 0; i < 4; ++i) { // 需要根据adjcent的逻辑修改
            node_t next = {cur.x + adjcent[i][0], cur.y + adjcent[i][1], cur.step + 1};
            if (next.x == finish.x && next.y == finish.y) { // 到达终点
                cout << next.step << endl;  // just a sample
                return;
            }
            if (node_is_valid(next)) {
                q.push(next);
                visited[next.x][next.y] = true;
            }
        }
    }
}

int main() {
    node_t start = {0, 0, 0};
    node_t finish = {0, 0, 0};
    cin >> start.x >> start.y;
    cin >> finish.x >> finish.y;
    for (int i = 0; i < M; ++i) {
        for (int j = 0; j < N; ++j) {
            cin >> m_map[i][j];
        }
    }
    bfs(start, finish);
    return 0;
}

有题目统计城市间最短线路,用BFS优于DFS,DFS统计城市间有多少线路更合适,当然也可求解最短线路。

3. DFS和BFS小结

深度优先搜索是能深则深,不能深则退,通常使用栈实现,深表示压栈,退表示出栈,一般运行时间长。
广度优先搜索是先搜近点,之后搜远点,类似水波层层外延,通常用队列实现,一般内存开销大。

进阶的搜索问题

在CSP-J中一般不会考到,NOI的比赛中有可能会考到。

  1. 有时候会采用DFS和BFS相结合的方式搜索,例如一个树型结构的前几层采用BFS,之后更深的节点采用DFS。
  2. 优化的搜索问题,如果发现搜索过程中目标已经比预期差(例如搜索时间,搜索路径代价等),可以跳过本次搜索,一般称之为剪枝。
  3. 有从出发点和目标点同时进行的搜索,称为双向搜索,这种可以降低计算复杂度。一般的,出发点可以采用BFS+DFS的搜索方式,目标点可以采用BFS的搜索方式。
  4. 有启发式信息的搜索,例如添加搜索的代价,添加搜索的步数限制等。
    可以在node_t结构体中添加cost属性,模拟搜索代价。
    可以在判断node_is_valid中添加step的比较,模拟步数限制。
  5. 递归最佳优先搜索RBFS,A*搜索等,属于进阶拓展,有兴趣可以深入研究这些搜索。

栈和队列

栈stack

栈是一种后进先出的结构,栈只操作最后一个元素,有push和pop可以使用,可调用的函数如下:

#include <bits/stdc++.h>
using namespace std;

int main() {
    std::stack<int> s; // 创建一个整型栈, int可修改为结构体
    for (int i = 0; i < 5; ++i) {
        s.push(i);
    }
    cout << s.size() << endl; // 输出栈的大小
    cout << s.top() << endl; // 输出栈顶元素,不弹出该元素
    s.pop(); // 弹出栈顶元素, 不返回该元素的值
    cout << s.top() << endl;
    cout << s.empty() << endl; // 判断栈是否为空,空返回1,非空返回0
    return 0;
}

stack的push动作相当于将数据填写到stack指针中,然后stack指针++。
stack的pop动作相当于stack–,所以没有返回值。

队列queue

队列是一种先进先出的结构,pop队列第一个元素,push元素到队列尾部,可调用的函数如下:

#include <bits/stdc++.h>
using namespace std;

int main() {
    std::queue<int> q; // 创建一个整型队列, int可修改为任意结构体
    for (int i = 0; i < 5; ++i) {
        q.push(i);
    }
    cout << q.front() << endl; // 输出队头元素,不弹出该元素
    cout << q.back() << endl; // 输出队尾元素,不弹出该元素
    q.pop(); // 弹出队头元素, 不返回该元素的值
    cout << q.front() << endl;
    cout << q.back() << endl; // 和刚才的q.back()值相同
    cout << q.size() << endl; // 输出队列的大小
    cout << q.empty() << endl; // 判断队列是否为空,空返回1,非空返回0
    return 0;
}

queue的push相当于将数据赋值到tail地址,然后tail指针++。
queue的pop相当于head指针++,所以没有返回值。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值