Codeforces Round #490 (Div. 3)-赛后补题

Posted zgglj-com

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces Round #490 (Div. 3)-赛后补题相关的知识,希望对你有一定的参考价值。

D. Equalize the Remainders

思维太僵硬了,我从余数入手,嫩是记录不了每个数要操作多少次。但是如果考虑每个数的贡献,即操作多少次能使得满足条件,就好写了,实际上也是暴力。

#include<bits/stdc++.h>
#define ll long long
#define P pair<int,int>
#define pb push_back
#define lson root << 1
#define INF (int)2e9 + 7
#define maxn (int)2e5 + 7
#define rson root << 1 | 1
#define LINF (unsigned long long int)1e18
#define mem(arry, in) memset(arry, in, sizeof(arry))
using namespace std;

int n, m, t;
int a[maxn], pre[maxn], sum[maxn];

int Find(int x){
    return (sum[x] < t ? x : pre[x] = Find(pre[x]));
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    
    cin >> n >> m;
    for(int i = 0; i < m; i++) pre[i] = (i + 1) % m;

    t = n / m;
    ll ans = 0;
    for(int i = 1; i <= n; i++) {
        int tp = 0;
        cin >> tp;
        int x = Find(tp % m);
        sum[x]++;
        a[i] = (x - tp % m + m) % m +tp;
        ans += a[i] - tp;
    }

    cout << ans << endl;
    for(int i = 1; i <= n; i++) cout << a[i] << " ";
    cout << endl;

    return 0;
}

E. Reachability from the Capital

题解:先从起点搜索一遍,对不能到达的点加一条从首都到这个点的边(加边操作只能是思维上的,不能真的addedge(st, i),因为后面还有删除边的操作),并标记,假如后加入的边能访问到先前的点,就删除原先对应加的边(说明允许犯错)。

#include<bits/stdc++.h>
#define ll long long
#define P pair<int,int>
#define pb push_back
#define lson root << 1
#define INF (int)2e9 + 7
#define maxn (int)5e3 + 7
#define rson root << 1 | 1
#define LINF (unsigned long long int)1e18
#define mem(arry, in) memset(arry, in, sizeof(arry))
using namespace std;

int n, m, tot, st;
int head[maxn];
bool use[maxn], connect[maxn], link[maxn];

struct node{ int to, next; } g[maxn << 1];

void Inite()
{
    tot = 0;
    mem(head, -1);
}

void addedge(int u, int v){
    g[tot].to = v;
    g[tot].next = head[u];
    head[u] = tot++;
}

void DFS(int u){
    use[u] = connect[u] = 1;    //connect数组表示从首都能到这个点
    for(int i = head[u]; i != -1; i = g[i].next){
        int v = g[i].to;
        if(!use[v]){
            link[v] = 0;   //删除标记
            DFS(v);
        }
    }
}

int main()
{
    cin >> n >> m >> st;
    Inite();
    for(int i = 1; i <= m; i++){
        int u, v;
        cin >> u >> v;
        addedge(u, v);
    }

    DFS(st);
    mem(use, 0);

    for(int i = 1; i <= n; i++){
        if(!connect[i]){
            link[i] = 1;

            DFS(i);
            mem(use, 0);
        }
    }

    int res = 0;
    for(int i = 1; i <= n; i++) if(link[i]) res++;

    cout << res << "
";
    return 0;
}

 

以上是关于Codeforces Round #490 (Div. 3)-赛后补题的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces Round #490 (Div. 3)

Codeforces Round #490 (Div. 3)-赛后补题

Codeforces Round #490(Div.3) F

Codeforces Round #490 (Div. 3) B

Codeforces Round #490 (Div. 3) F - Cards and Joy

Reachability from the Capital(Codeforces Round #490 (Div. 3)+tarjan有向图缩点)