头像

HUSTzyk

华中科技大学




离线:3天前


最近来访(37)
用户头像
我对变换一无所知
用户头像
拿波利塔纳
用户头像
ONROAD
用户头像
做事要有遗逝感
用户头像
种花家的兔兔
用户头像
NUC-CYJ
用户头像
种花家的市长
用户头像
提子面包
用户头像
lsz_
用户头像
Byte_1
用户头像
guluguluboy
用户头像
小小_88
用户头像
kerman0411
用户头像
ltss
用户头像
Nobody_Z
用户头像
Andy2035
用户头像
Ethanyyc
用户头像
xiuzhiyuan
用户头像
smr666
用户头像
cjlworld

活动打卡代码 AcWing 906. 区间分组

HUSTzyk
9天前

区间分组

#include <iostream>
#include <algorithm>
#include <queue>

using namespace std;

const int N = 100010;

int n;
struct Range
{
    int l, r;
    bool operator< (const Range &W)
    {
        return l < W.l;
    }
} ranges[N];

int main()
{
    cin >> n;

    for(int i = 0; i < n; i ++ )
    {
        int l, r;
        cin >> l >> r;
        ranges[i] = {l, r};
    }

    sort(ranges, ranges + n);

    priority_queue<int, vector<int>, greater<int>> heap;
    for(int i = 0; i < n; i ++ )
    {
        auto r = ranges[i];
        if(heap.empty() || heap.top() >= r.l) heap.push(r.r);
        else
        {
            int t = heap.top();
            heap.pop();
            heap.push(r.r);
        }
    }

    printf("%d\n", heap.size());

    return 0;
}



HUSTzyk
10天前

最大不相交区间数量

#include <iostream>
#include <algorithm>

using namespace std;

const int N = 100010;

int n;
struct Range
{
    int l, r;
    bool operator< (const Range &W)const
    {
        return r < W.r;
    }
} ranges[N];

int main()
{
    cin >> n;
    for(int i = 0; i < n; i ++ )
    {
        int l, r;
        cin >> l >> r;
        ranges[i] = {l, r};
    }

    sort(ranges, ranges + n);

    int res = 0, ed = -2e9;
    for(int i = 0; i < n; i ++ )
    {
        if(ranges[i].l > ed)
        {
            ed = ranges[i].r;
            res ++ ;
        }
    }

    cout << res << endl;

    return 0;
}


活动打卡代码 AcWing 905. 区间选点

HUSTzyk
10天前

贪心-区间选点问题

#include <iostream>
#include <algorithm>

using namespace std;

const int N = 100010;

int n;
struct Range
{
    int l, r;
    bool operator< (const Range &W)const
    {
        return r < W.r;
    }
}

int main()
{
    cin >> n;

    for(int i = 0; i < n; i ++ )
    {
        int l, r;
        cin >> l >> r;
        ranges[i] = {l, r};
    }

    sort(ranges, ranges + n);

    int res = 0, ed = -2e9
    for(int i = 0; i < n; i ++ )
    {
        if(ranges[i].l > ed)
        {
            res ++ ;
            ed = ranges[i].r;
        }
    }

    cout << res << endl;

    return 0;
}



HUSTzyk
10天前

Kruskal算法求最小生成树(带思路版)

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 200010;

int n, m;
int p[N];

struct Edge
{
    int a, b, w;

    bool operator< (const Edge &W)const
    {
        return w < W.w;
    }
} edges[N];

int find(int x)
{
    if(p[x] != x) p[x] = find(p[x]);
    return p[x];
}

int main()
{
    cin >> n >> m;

    for(int i = 0; i < m; i ++ )
    {
        int a, b, w;
        cin >> a >> b >> w;
        edges[i] = {a, b, w};
    }

    sort(edges, edges + m); //将所有边按权重从小到大排序

    for(int i = 1; i <= n; i ++ ) p[i] = i;

    int res = 0, cnt = 0;
    for(int i = 0; i < m; i ++ ) //枚举每条边
    {
        int a = edges[i].a, b = edges[i].b, w = edges[i].w;

        a = find(a), b = find(b);
        if(a != b)
        {
            p[a] = b; //若点a, b不连通,则加入集合中
            res += w;
            cnt ++ ;
        }
    }

    if(cnt < n - 1) puts("impossible");
    else printf("%d\n", res);

    return 0;
}



HUSTzyk
10天前

Kruskal算法求最小生成树

复习一下并查集hh

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 200010;

int n, m;
int p[N];

struct Edge
{
    int a, b, w;

    bool operator< (const Edge &W)const
    {
        return w < W.w;
    }
} edges[N];

int find(int x)
{
    if(p[x] != x) p[x] = find(p[x]);
    return p[x];
}

int main()
{
    cin >> n >> m;

    for(int i = 0; i < m; i ++ )
    {
        int a, b, w;
        cin >> a >> b >> w;
        edges[i] = {a, b, w};
    }

    sort(edges, edges + m);

    for(int i = 1; i <= n; i ++ ) p[i] = i;

    int res = 0, cnt = 0;
    for(int i = 0; i < m; i ++ )
    {
        int a = edges[i].a, b = edges[i].b, w = edges[i].w;

        a = find(a), b = find(b);
        if(a != b)
        {
            p[a] = b;
            res += w;
            cnt ++ ;
        }
    }

    if(cnt < n - 1) puts("impossible");
    else printf("%d\n", res);

    return 0;
}



HUSTzyk
10天前

Prim算法

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 510, INF = 0x3f3f3f3f;

int n, m;
int g[N][N];
int dist[N];
bool st[N];

int prim()
{
    memset(dist, 0x3f, sizeof dist);

    int res = 0;
    for(int i = 0; i < n; i ++ )
    {
        int t = -1;
        for(int j = 1; j <= n; j ++ )
            if(!st[j] && (t == -1 || dist[t] > dist[j]))
                t = j;

        if(i && dist[t] == INF) return INF;
        if(i) res += dist[t];

        for(int j = 1; j <= n; j ++ ) dist[j] = min(dist[j], g[t][j]);

        st[t] = true;
    }

    return res;
}

int main()
{
    cin >> n >> m;

    memset(g, 0x3f, sizeof g);

    while(m -- )
    {
        int a, b, c;
        cin >> a >> b >> c;
        g[a][b] = g[b][a] = min(g[a][b], c);
    }

    int t = prim();

    if(t == INF) puts("impossible");
    else printf("%d\n", t);

    return 0;
}


新鲜事 原文

HUSTzyk
12天前
AcSaber 可真是个好东西!😆


新鲜事 原文

HUSTzyk
12天前
AcWing《寒假每日一题2023》拼团优惠!https://www.acwing.com/activity/content/introduction/2712/group_buy/118809/
拼团仅需1元 !!!
还差一人,想拼的同学速速加入辣😄


活动打卡代码 AcWing 854. Floyd求最短路

HUSTzyk
12天前

Floyd算法求最短路

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 210, INF = 1e9;

int n, m, Q;
int d[N][N];

void floyd()
{
    for(int k = 1; k <= n; k ++ )
        for(int i = 1; i <= n; i ++ )
            for(int j = 1; j <= n; j ++ )
                d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
}

int main()
{
    cin >> n >> m >> Q;

    for(int i = 1; i <= n; i ++ )
        for(int j = 1; j <= n; j ++ )
            if(j == i) d[i][j] = 0;
            else d[i][j] = INF;

    while(m -- )
    {
        int a, b, w;
        cin >> a >> b >> w;

        d[a][b] = min(d[a][b], w);
    }

    floyd();

    while(Q -- )
    {
        int a, b;
        cin >> a >> b;

        if(d[a][b] > INF / 2) puts("impossible");
        else printf("%d\n", d[a][b]);
    }

    return 0;
}



活动打卡代码 AcWing 852. spfa判断负环

HUSTzyk
12天前

spfa算法判断负环

#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>

using namespace std;

const int N = 100010;

int n, m;
int h[N], e[N], w[N], ne[N], idx;
int dist[N], cnt[N];
bool st[N];

void add(int a, int b, int c)
{
    e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++ ;
}

int spfa()
{
    queue<int> q;

    for(int i = 1; i <= n; i ++ )
    {
        st[i] = true;
        q.push(i);
    }

    while(q.size())
    {
        int t = q.front();
        q.pop();

        st[t] = false;

        for(int i = h[t]; i != -1; i = ne[i])
        {
            int j = e[i];
            if(dist[j] > dist[t] + w[i])
            {
                dist[j] = dist[t] + w[i];
                cnt[j] = cnt[t] + 1;

                if(cnt[j] > n) return true;
                if(!st[j])
                {
                    q.push(j);
                    st[j] = true;
                }
            }
        }
    }

    return false;
}

int main()
{
    cin >> n >> m;

    memset(h, -1, sizeof h);

    while(m -- )
    {
        int a, b, c;
        cin >> a >> b >> c;
        add(a, b, c);
    }

    if(spfa()) puts("Yes");
    else puts("No");

    return 0;
}