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 << "\n";
    return 0;
}

原文地址:https://www.cnblogs.com/zgglj-com/p/9215006.html

时间: 2024-07-30 22:07:18

Codeforces Round #490 (Div. 3)-赛后补题的相关文章

Codeforces Round #491 (Div. 2) — 赛后补题

C. Candies PS:大概是又傻了,读题啊. #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)1e5 + 7 #define rson root << 1 | 1 #define LINF (

Educational Codeforces Round 48 (Rated for Div. 2) - 赛后补题

C. Vasya And The Mushrooms 题解:拿笔画他的行走路线,你会发现可以前缀和预处理一些东西出来. const int N = 300005; int n; ll a[N], b[N], dp[2][N], sp[2][N], sum[N]; int get_a() { dp[0][0] = 0; for (int i = 1; i < n; ++i) { dp[0][i] = dp[0][i - 1] + 1ll * i * a[i]; } int t = n; dp[1]

Educational Codeforces Round 24 CF 818 A-G 补题

6月快要结束了 期末也过去大半了 马上就是大三狗了 取消了小学期后20周的学期真心长, 看着各种北方的学校都放假嗨皮了,我们这个在北回归线的学校,还在忍受酷暑. 过年的时候下定决心要拿块ACM的牌子,一直坚持刷题,这一个学期刷了200道吧,感觉还是小有收获.特别是Ural和Codeforces上的题,质量很高. 然后4月的校赛,5月的省赛,发挥的也一般,不过也没有太失常. 希望暑假的选拔赛能碰到有趣的队友 蛤蛤. 这两天各种考试,实在是太忙,看了一下edu24的题目,不是很容易,做了一道水题,以

Codeforces Round #315 (Div. 2) (ABCD题)

A. Music 题意: 一首歌长度为S秒,已经下载了T秒,下载速度为每q秒的现实时间能下载下来(q-1)秒 的歌曲.现在开始听歌,如果听到还没下载的地方就从0秒的地方开始replay,求一首歌听完需要从0秒听几次(包括一开始那次) 思路: 我们可以用路程-时间的思路来考虑这道题. 假设两位选手"播放"与"下载","播放"的起点是0m处,"下载"的起点是Tm处,终点在Sm处,"播放"的速度是1m/s,&qu

Codeforces Round #310 (Div. 2)--A(简单题)

http://codeforces.com/problemset/problem/556/A 题意:给一个01字符串,把所有相邻的0和1去掉,问还剩下几个0和1. 题解:统计所有的0有多少个,1有多少个,最后答案就是两者不同的个数. #include <cstdio> #include <cmath> #include <cstring> #include <ctime> #include <iostream> #include <algo

Educational Codeforces Round 11 C hard process_补题——作为司老大的脑残粉

司老大当时教了一种姿势枚举连续K个0,说实话当时比赛写这题完全蒙了 纵然后来知道思路还是写了一段时间 真的是.. 题目大意 n长度的序列,由0 1构成 我们可以改变 k个0为1 求可以得到的最长连续1序列的长度 既然求连续1 我们贪心连续k个0 枚举端点 左端点0设置为0 右端点0设置为 n+1 中间统计一下 最长长度和改变的0的位置就OK了 1 #include<cstdio> 2 #include<map> 3 //#include<bits/stdc++.h> 4

Codeforces Round #345 (Div. 2) D 细节题

这个题的意思是给你n张首尾相接的照片, 当前照片在1, 每次只能转移一个单位, 转移时间为a, 照片有可能颠倒, 将照片摆正需要的时间为b, 看照片的时间为1, 想要看尽可能多的照片, 问这个数量是多少, 我们可以预处理查看每张照片需要的时间, 然后枚举从左边看需要的时间, 从右边看需要的时间, 二分从另外一边看需要的时间即可, 代码如下: #include <cstdio> #include <algorithm> #include <cstring> using n

Codeforces Round #316 (Div. 2) (ABC题)

A - Elections 题意: 每一场城市选举的结果,第一关键字是票数(降序),第二关键字是序号(升序),第一位获得胜利: 最后的选举结果,第一关键字是获胜城市数(降序),第二关键字是序号(升序),第一位获得胜利: 求最后选举获胜者. 思路: 直接模拟即可. 代码: /* * @author FreeWifi_novicer * language : C++/C */ #include<cstdio> #include<iostream> #include<cstring

Codeforces Round #490 (Div. 3) B

传送门http://codeforces.com/contest/999/problem/B 一个长度为n的字符串t,将n的因子从大到小排个序(设为a[i]),每次把t的第一个字符到第a[i]个字符reverse一下,最终得到一个怪异的串s.现在给n和s,求t. 最长100个字符,直接模拟好了.先把n分解质因数,然后进行"逆操作",即把因子从小到大排序,然后把给的串reverse回去. 我在做的时候忘记reverse怎么用了,尴尬. 1 #include <iostream>