五一快乐假期每天都在上课qaq

5.1


Introduction

NOIP(National Olympiad in Informatics in Provinces):全国青少年信息学奥林匹克联赛

NOI(National Olympiad in Informatics):全国青少年信息学奥林匹克竞赛

CPU(Central Processing Unit):中央处理器

CPU - memory - O/I device

1 byte = 8 bit


前置知识

int a;
int *b = &a;
View Code

*:pointer 指针

&:address 地址

*(a + 1) = x 等价于 a[1] = x

int:%d

long long:%lld / %I64d

usigned int:%u

usigned long long:%llu

数据结构(struct)

对象(class)

%*d 读完扔掉

#include<cstdio>
scanf printf
#include<cstring>
memset strlen strcmp
#include<ctime>
time(0)
#include<cstdlib>
rand() srand()
#include<iostream>
cin cout string
cin 关闭同步 std::ios::sync_with_stdio(false)//速度提高至scanf左右
#include<algorithm>
sort lower_bound max min
#include<cmath>
sqrt sin log pow
头文件(header files)
int read(){
    int x = 0,f = 1;
    char ch = getchars();
    while(ch < '0' || ch > '9'){
        if(ch == '-')
            f = -1;
        ch = getchars();
    }    
    while(ch >= '0' && ch <='9'){
        x = x * 10 + ch - '0';
        ch = getchars();    
    }
    return x * f;    
}
//////////////////////////////////////////////
fread
quick read

复杂度(Complexity)

Big-O Notation:大O记号

定义:T(n) = O(f(n)) iff(当且仅当)存在 c,N > 0 s.t.(subject to)任意 n > N T(n) <= c * f(n)

主定理(master theorem):用渐近符号表示许多由分治法得到的递推关系式

P(polynomial)问题:能在多项式时间内解决的问题

NP问题:(Nondeterministic Polynomial time Problem)不能在多项式时间内解决或不确定能不能在多项式时间内解决,但能在多项式时间内验证的问题

NPC问题:(NP Complete)NP完全问题,所有NP问题在多项式时间内都能规约(Reducibility)到它的NP问题,即解决了此NPC问题,所有NP问题也都能得到解决

NP hard问题:NP难问题,所有NP问题在多项式时间内都能规约到它的问题,但不一定是NP问题


some websites

cplusplus.com

duck.ac


排序(sequence)

有序:{a1,a2,...,an} iff 任意i属于1...n - 1 ai <= ai + 1

稳定性:{1,1,2,1}排序——>{1,1,1,2} 重复的1在序列中的相对位置不变 则该排序稳定

不稳定到稳定:加pair<int,int>排序

逆序对(inversion):i > j,ai < aj

选择排序(selection sort)

for(int i = 0;i<n - 1;i++)
    for(int j = i + 1;j < n;j++)
        if(a[j] < a[i])
            swap(a[j],a[i]);
     
selection sort

插入排序(insertion sort)

for(i = 1;i < n;i++)
    for(j = 0;j < i;j++)
        if(a[j] > a[i])
            swap(a[i],a[j]);
insertion sort

冒泡排序(bubble sort)

for(int i = 0;i < n - 1;i++)
    for(int j = 0;j < n - 1 - i;j++)
        if(a[j] > a[j + 1])
            swap(a[j],a[j + 1]);
bubble sort

归并排序(merge sort)

#include<cstdio>
#define sev en
using namespace std;
#define N 500010

int a[N],n,f[N];
long long ans;

void merge(int l,int r) {
    if(l == r)
        return ;
    int mid = (l + r) >> 1;
    merge(l,mid);
    merge(mid + 1,r);
    int i = l,k = l,j = mid + 1;
    while(i <= mid && j <= r) {
        if(a[i] <= a[j]) {
            f[k] = a[i];
            i++;
            k++;
        } else {
            f[k] = a[j];
            j++;
            k++;
            ans += mid - i + 1;
        }
    }
    while(i <= mid) {
        f[k] = a[i];
        i++;
        k++;
    }
    while(j <= r) {
        f[k] = a[j];
        k++;
        j++;
    }
    for(int w = l; w <= r; w++)
        a[w] = f[w];
}

int main() {
    scanf("%d",&n);
    for(int i = 1; i <= n; i++)
        scanf("%d",&a[i]);
    merge(1,n);
    printf("%lld",ans);
    return 0;
}
merge sort --> inversion

桶排序(bucket sort)

for(i = 1;i <= n;i++){
    scanf("%d",&x); 
        a[x]++;        
}
for(i = 0;i <= n;i++)   
    for(j = 1;j <= a[i];j++) 
        printf("%d ",i);
bucket sort

基数排序(radix sort)

快速排序(quick sort)

void quicksort(int p,int q){
    int i = p;
    int j = q;
    int temp = a[p];
    while(i < j){
        while(a[j] >= temp && j > i )
            j--;
        if(j > i){
            a[i] = a[j];
            i++;
            while(a[i] <= temp && i < j )
                i++;
            if(i < j){
                a[j] = a[i];
                j--;
            }
        }        
    }
    if(p < i - 1)
        quicksort(p,i - 1);
    if(j + 1 < q )
        quicksort(j + 1,q);    
}
quick sort
sort(a + 1,a + n + 1);
sort

STL标准模版库(Standard Template Library)

向量(vector)

#include<vector>
push_back();
pop_back();
size();
empty();
clear();
erease();
insert();
operator[];
vector

链表(list)

#include<map>
map<string,string>a;
a[qaq] = 'qwq';
map
#include<set>
size();
begin();
end();
empty();
clear();
集合(set)
#include<queue>
push();
pop();
empty();
first();
////////////////////
priority_queue<int>q;//大根堆
priority_queue<int,vector<int>,greater<int> >q;//小根堆
队列(queue) FIFO
#include<stack>
push();
pop();
栈(stack) LIFO

 


5.2

图论(graph theory)

图(graph):<V,E>

简单图(simple graph):无重边,无自环

度(degree)

有向图——max edge n * (n - 1)

无向图——max edge n * (n - 1) / 2

简单路径(simple path)

回路(cycle):环

连通图(connected graph):任意两点可互相到达

强连通图(strong connected graph):有向图

弱连通图(weakly connected graph):有向边变为无向连通

子图(subgraph)

最短路可能不存在(有负环 | 不可达) | 不唯一

有向无环图(DAG):directed acyclic graph


存图

邻接矩阵(Adjacency Matrix)

for(int i = 1;i <= m;i++)
    scanf("%d%d%d",&x,&y,&w);
    map[x][y] = w;
    map[y][x] = w;
}
adjacency matrix

链式前向星(邻接表)

struct EDGE{
    int nxt,to,w;
}e[N << 1];
int head[N],cnt;

void add(int x,int y,int z){
    e[++cnt].to = y;
    e[cnt].nxt = head[x];
    e[cnt].w = z;
    head[x] = cnt;
}
链式前向星

图的遍历

宽度优先搜索(bfs):breadth first search

深度优先搜索(dfs):depth first search

前序遍历(DLR)

中序遍历(LDR)

后序遍历(LRD)


 树(tree)

有n个点,n - 1条边的连通图

无向无环连通图

任意两点间只有一条简单路径的无向图

森林

无根树

有根树

二叉树(binary tree):每个结点最多有两个子树的树结构

满二叉树(full binary tree):除最后一层无任何子节点外,每一层上的所有结点都有两个子结点的二叉树

完全二叉树(complete binary tree):若设二叉树的深度为h,除第 h 层外,其它各层 (1~h-1) 的结点数都达到最大个数,第 h 层所有的结点都连续集中在最左边


 一些其它的东西

二分图(bipartite graph):设G=(V,E)是一个无向图,如果顶点V可分割为两个互不相交的子集(A,B),并且图中的每条边(i,j)所关联的两个顶点i和j分别属于这两个不同的顶点集(i in A,j in B),则称图G为一个二分图

拓扑排序(topological sort)


 字符串(string)

brute force

hash

trie tree

 


 

to be continued... 

posted @ 2019-05-01 21:30  ./seven  阅读(235)  评论(2编辑  收藏  举报