PAT笔记

判断大根堆/小根堆

for(int i = 2; i <= n; i++){
    if(a[i/2] >a[i]) isMin = 0;
    if(a[i/2] <a[i]) isMax = 0;
}

堆排序(大根堆)→递增序列

取出堆顶元素,将最后一个元素替换至堆顶,再一一进行向下调整。

while(p > 2 && b[p] >= b[1]) p--;
swap(b[1], b[p]);
downAdjust(b, 1, p-1);

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

判断是否联通:

void dfs(int index){
    visit[index] = true;
    cnt++;
    for(int i = 0; i < v[index].size(); i++){
        if(visit[v[index][i]] == false)
        dfs(v[index][i]);
    }
}

通过判断cnt判断是否连通。

AVL树

struct node{
    int val;
    struct node *left;
    struct node *right;
}
//左旋
node* leftRotate(node *tree){
    node *temp = tree -> right;
    tree -> right = temp -> left;
    temp -> left = tree;
    return temp; 
}
//右旋
node* rightRotate(node *tree){
    node *temp = tree -> left;
    tree -> left = temp -> right;
    temp -> right = tree;
    return temp; 
}
//先左再右
node* leftRightRotate(node *tree){
    tree -> left = leftRotate(tree -> left);
    return rightRotate(tree);
}
//先右再左
node* rightLeftRotate(node *tree){
    tree -> right = rightRotate(tree -> right);
    return leftRotate(tree);
}
//AVL建树
node* insert(node *tree, int val){
    if(tree == NULL){
        tree = new node();
        tree -> val = val;
    }else if(tree -> val > val){
        tree -> left = insert(tree -> left, val);
        int l = getHeight(tree -> left);
        int r = getHeight(tree -> right);
        if(l - r >= 2){
            if(val < tree -> left -> val){
                tree = rightRotate(tree);
            }else{
                tree = leftRightRotate(tree);
            }
        }
    }else{
        tree -> right = insert(tree -> right, val);
        int l = getHeight(tree -> left);
        int r = getHeight(tree -> right);
        if(r - l >= 2){
            if(val > tree -> right -> val){
                tree = leftRotate(tree);
            }else{
                tree = rightLeftRotate(tree);
            }
        }
    }
    return tree;
}

并查集

查找父节点

int findFather(int x){
    int a = x;
    while(x != father[x]){
        x = father[x];
    }
    while(a != father[a]){
        int z = a;
        a = father[a];
        father[z] = x;
    }
    return x;
}
void Uion(int a, int b){
    int faA = findFather(a);
    int faB = findFather(b);
    if(faA != faB){
        father[faA] = faB;
    }
}

从大到小排序

sort(a, a+n, greater<int>());


Dijkstra

邻接表

struct node{
    int v,dis;
}
vector<node> Adj[Max];
int n;
int d[Max];
bool vis[Max] = {false};
void Dijkstra(int s){
    fill(d, d+Max, INF);
    dis[s] = 0;
    for(int i = 0; i < n; i++){
        int u = -1;
        int Min = INF;
        for(int j = 0; j < n; j++){
            if(vis[j] == false && d[j] <Min){
                u = j;
                Min = d[j];
            }
        }
        if(u == -1) return;
        vis[u] = true;
        for(int j = 0; j < Adj[u].size(); j++){
            int v = Adj[u][j].v;
            if(vis[v] == false && dis[u] + Adj[u][j].dis < d[v]){
                d[v] = d[u] +Adj[u][j].dis;
            }
        }

    }
}

矩阵

int n, G[Max][Max];
int d[Max];
bool vis[Max] = {false};
void Dijkstra(int s){
    fill(d, d+Max, INF);
    dis[s] = 0;
    for(int i = 0; i < n; i++){
        int u = -1;
        int Min = INF;
        for(int j = 0; j < n; j++){
            if(vis[j] == false && d[j] <Min){
                u = j;
                Min = d[j];
            }
        }
        if(u == -1) return;
        vis[u] = true;
        for(int v = 0; v < n; v++){
            if(vis[v] == false && G[u][v] != INF && d[u] + G[u][v] < d[v]){
                d[v] = d[u] + G[u][v];
            }
        }

    }
}

sscanf() 从一个字符串中读进与指定格式相符的数据

scanf("%s",a);

sscanf(a,"%lf", &temp);           //判断a是否符合格式,将其读进temp

sprintf() 字符串格式化命令,主要功能是把格式化的数据写入某个字符串

ssprintf(b,"%.2f",temp);

temp:已格式化的数据写入b

getline(cin,s)


最大公约数

int gcd(int a,int b){
    return b == 0 ? a : gcd(b, a%b);
}

判断是否为素数

bool isprime(int n){
    if(n <= 1) return false;
    for(int i = 2; i * i <= n; i++){
        if(n % i == 0) return false;
    } 
    return true;
}

最小公倍数

int lcm(a,b){ return a * b / gcd(a,b);}

当string类型以%s输出时:  .c_str()


改变set的排序方式

struct node{
    int value,cnt;
    bool operator < (const node &a)const{
        return (cnt != a.cnt) ? cnt >a.cnt : value < a.value;
    }
}
set<node> s;

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值