zoukankan      html  css  js  c++  java
  • codeforces 722C (并查集)

    题目链接:http://codeforces.com/contest/722/problem/C

    题意:每次破坏一个数,求每次操作后的最大连续子串和。

    思路:并查集逆向操作


    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    const int N = 1e5 + 10;
    ll sum[N], ans[N];
    int n, a[N], b[N], father[N], r[N];
    bool vis[N];
    int finds(int x)
    {
        if(father[x] != x)
            father[x] = finds(father[x]);
        return father[x];
    }
    void connect(int a,int b)
    {
        a = finds(a);
        b = finds(b);
        if(r[a] > r[b])
            father[b] = a;
        else if(r[a] < r[b])
            father[a] = b;
        else
        {
            father[a] = b;
            r[b]++;
        }
        sum[a] = sum[b] = sum[a] + sum[b];
    }
    void init()
    {
        for(int i = 1; i <= n + 1; i++)
            father[i] = i;
        vis[0] = vis[n+1] = 1;
    }
    int main()
    {
        scanf("%d",&n);
        for(int i = 1; i <= n; i++)
            scanf("%d",a+i);
        for(int i = 1; i <= n; i++)
            scanf("%d",b+i);
        init();
        for(int i = n; i > 1; i--)
        {
            sum[b[i]] = a[b[i]];
            if(vis[b[i]-1])
                connect(b[i] - 1,b[i]);
            if(vis[b[i]+1])
                connect(b[i] + 1,b[i]);
            ans[i-1] = max(ans[i],sum[finds(b[i])]);
            vis[b[i]] = 1;
        }
        for(int i = 1; i <= n; i++)
            printf("%I64d
    ",ans[i]);
        return 0;
    }
    



  • 相关阅读:
    MySQL decimal unsigned 更新负数不报错却为0
    centos 安装jdk
    CentOS7安装docker
    Cron 时间元素
    PHPStorm
    日志习惯
    HTTP幂等性
    navicat for mysql 10.1.7注册码
    localStorage、sessionStorages 使用
    FreePascal
  • 原文地址:https://www.cnblogs.com/westwind1005/p/5975183.html
Copyright © 2011-2022 走看看