Educational Codeforces Round 47 (Rated for Div. 2)

【比赛链接】

【A】Game Shopping

  • 【题意】有 n n 个物品,m个账单,尝试用最前面的账单去买物品,若能买,账单和物品都转到下一个,否则转到下一个物品而依然使用现在的账单。问能买到多少物品。
  • 【题解】按题意模拟即可。
  • 时间复杂度 O(N+M) O ( N + M )
  • 【代码】
#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
#define LL  long long
#define MAXN    10010
using namespace std;
int n, m, a[MAXN], b[MAXN];

template <typename T> void chkmin(T &x, T y){x = min(x, y);}
template <typename T> void chkmax(T &x, T y){x = max(x, y);}
template <typename T> void read(T &x){
    x = 0; int f = 1; char ch = getchar();
    while (!isdigit(ch)) {if (ch == '-') f = -1; ch = getchar();}
    while (isdigit(ch)) {x = x * 10 + ch - '0'; ch = getchar();}
    x *= f;
}

int main(){
    read(n), read(m);
    for (int i = 1; i <= n; ++i)
        read(a[i]);
    for (int i = 1; i <= m; ++i)
        read(b[i]);
    int cur = 1, ans = 0;
    for (int i = 1; i <= n; ++i){
        if (b[cur] >= a[i]) ++ans, ++cur;
        if (cur > m) break;
    }
    printf("%d\n", ans);
    return 0;
}

【B】Minimum Ternary String

  • 【题意】给出一个长度为 n n 的仅包含0,1,2的序列,每次可以交换相邻的 0,1 0 , 1 或者相邻的 1,2 1 , 2 ,问在若干次操作后可以得到的字典序最小的序列是什么。
  • 【题解】考虑一个问题,原先在 2 2 后的0,无论如何操作,最后一定在 2 2 之后。那么直接从后向前贪心,每碰到一个2就将后面的 0 0 从后向前放并且将2放入,最后先放 1 1 再放0即可。
  • 时间复杂度 O(N) O ( N )
  • 【代码】
#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
#define LL  long long
#define MAXN    100010
using namespace std;
char s[MAXN], t[MAXN];
int size[3], last;

template <typename T> void chkmin(T &x, T y){x = min(x, y);}
template <typename T> void chkmax(T &x, T y){x = max(x, y);}
template <typename T> void read(T &x){
    x = 0; int f = 1; char ch = getchar();
    while (!isdigit(ch)) {if (ch == '-') f = -1; ch = getchar();}
    while (isdigit(ch)) {x = x * 10 + ch - '0'; ch = getchar();}
    x *= f;
}

int main(){
    scanf("%s", s + 1);
    int n = strlen(s + 1);
    last = n;
    for (int i = n; i >= 1; --i){
        if (s[i] == '0') ++size[0];
        else if (s[i] == '1') ++size[1];
        else {
            while (size[0] > 0){
                t[last] = '0', --size[0];
                --last;
            }
            t[last] = '2';
            --last;
        }
    }
    while (size[0] + size[1]){
        if (size[1] > 0) t[last] = '1', --size[1];
        else if (size[0] > 0) t[last] = '0', --size[0];
        --last;
    }
    printf("%s", t + 1);
    return 0;
}

【C】Annoying Present

  • 【题意】给出一个长度为 n n 的一开始全为零的序列,有m次操作。每次操作给出两个数 x x d,你可以选择一个位置 pos p o s ,使得序列中的每个位置 i i 加上x+d|posi|,现在需要使序列的平均数最大。
  • 【题解】显然,题目要求就是要使得序列的和最大。 x x 对于位置的选择没有影响,直接统计入答案即可。若d为正,则将 pos p o s 定于序列一端,否则将 pos p o s 定于序列中间,然后就是一个等差数列求和。特别的,本题需要注意精度误差。
  • 时间复杂度 O(M) O ( M )
  • 【代码】
#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
#define LL  long long   
using namespace std;
int n, m;
LL ans;

template <typename T> void chkmin(T &x, T y){x = min(x, y);}
template <typename T> void chkmax(T &x, T y){x = max(x, y);}
template <typename T> void read(T &x){
    x = 0; int f = 1; char ch = getchar();
    while (!isdigit(ch)) {if (ch == '-') f = -1; ch = getchar();}
    while (isdigit(ch)) {x = x * 10 + ch - '0'; ch = getchar();}
    x *= f;
}

int main(){
    read(n), read(m);
    ans = 0;
    for (int i = 1; i <= m; ++i){
        int x, d; read(x), read(d);
        ans += 1ll * x * n;
        if (d > 0) ans += 1ll * n * (n - 1) / 2 * d;
        else {
            int tmp = n / 2;
            if (n % 2) ans += 1ll * (tmp + 1) * tmp * d;
            else ans += 1ll * tmp * (tmp - 1) / 2 * d, 
                 ans += 1ll * tmp * (tmp + 1) / 2 * d;
        }
    }
    printf("%.10lf\n", 1.0 * ans / n);
    return 0;
}

【D】Relatively Prime Graph

  • 【题意】构造一张有 n n 个点,m条边的无向连通图,其中节点编号为 1 1 ~n,且无重边自环,使得图中的每条边 <u,v> < u , v > <script type="math/tex" id="MathJax-Element-33"> </script>都满足 uv u v 互质。或输出无解。
  • 【题解】首先,若 m<n1 m < n − 1 ,无解,无法构成连通图,否则一定可以将所有除了 1 1 号节点以外的点连向1号节点使得图连通。其次,若 ni=2ϕ(i)<m ∑ i = 2 n ϕ ( i ) < m ,其中 ϕ(n) ϕ ( n ) 表示 1 1 ~n中与 n n 互质的数的个数,则合法的总边数小于m,无解。剩余情况有解,分析发现, ni=2ϕ(i) ∑ i = 2 n ϕ ( i ) 的增长速度很快,在 n n 很小的时候就超过了105,直接暴力构造即可。
  • 时间复杂度 O(NlogN+K2logK) O ( N l o g N + K 2 l o g K ) ,其中 K K 满足i=2Kϕ(i)m
  • 【代码】
#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
#define LL  long long
#define MAXN    100010
using namespace std;
int n, m, f[MAXN], p[MAXN], pri[MAXN], cnt;

template <typename T> void chkmin(T &x, T y){x = min(x, y);}
template <typename T> void chkmax(T &x, T y){x = max(x, y);}
template <typename T> void read(T &x){
    x = 0; int f = 1; char ch = getchar();
    while (!isdigit(ch)) {if (ch == '-') f = -1; ch = getchar();}
    while (isdigit(ch)) {x = x * 10 + ch - '0'; ch = getchar();}
    x *= f;
}

int gcd(int a, int b){
    if (a == 0 || b == 0) return a + b;
    else return gcd(b, a % b);
}

int main(){
    read(n), read(m);
    if (m < n - 1) {printf("Impossible\n"); return 0;}
    for (int i = 1; i <= n; ++i)
        f[i] = i;
    for (int i = 1; i <= n; ++i)
        for (int j = i + i; j <= n; j += i)
            f[j] -= f[i];
    LL tot = 0;
    for (int i = 2; i <= n; ++i)
        tot += f[i];
    if (tot < m) {printf("Impossible\n"); return 0;}
    printf("Possible\n");
    m -= n - 1;
    tot = 0;
    for (int i = 2; i <= n; ++i)
        printf("%d %d\n", i, 1);
    for (int i = 2; i <= n; ++i){
        for (int j = 2; j < i; ++j){
            if (tot == m) break;
            if (gcd(i, j) == 1) {printf("%d %d\n", i, j); ++tot;}
        }
        if (tot == m) break;
    }
    return 0;
}

【E】Intercity Travelling

  • 【题意】从起点到终点之间共有 n n 段路程,n1个休息站,每个休息站都有 12 1 2 的概率会停下来休息。每段路的难度为 ap a p ,其中 p p 是指上一个停下来休息的位置到这条路的距离+1。求从起点走到终点的期望难度,答案乘 2n1 2 n − 1 998244353 998244353
  • 【题解】根据期望的线性可加性质,和的期望等于期望的和,问题转化为求每段路的期望难度之和。对于每一段,贡献是 a1 a 1 的的概率是上一个点为休息站的概率( 12 1 2 ),贡献为 a2 a 2 的概率是上一个点不是休息站且再上一个点是休息站的概率( 122 1 2 2 )……总结起来,每段路的期望难度 e(x)=xi=1(12iai)+2xax e ( x ) = ∑ i = 1 x ( 1 2 i ∗ a i ) + 2 x ∗ a x ,答案就是 ni=1e(i) ∑ i = 1 n e ( i ) ,最后还要按照题目要求乘上 2n1 2 n − 1
  • 时间复杂度 O(N) O ( N )
  • 【代码】
#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
#define LL  long long
#define mod 998244353
#define MAXN    1000010
using namespace std;
LL f[MAXN], a[MAXN], b[MAXN], inv;
int n;

template <typename T> void chkmin(T &x, T y){x = min(x, y);}
template <typename T> void chkmax(T &x, T y){x = max(x, y);}
template <typename T> void read(T &x){
    x = 0; int f = 1; char ch = getchar();
    while (!isdigit(ch)) {if (ch == '-') f = -1; ch = getchar();}
    while (isdigit(ch)) {x = x * 10 + ch - '0'; ch = getchar();}
    x *= f;
}

LL qpow(LL a, int b){
    LL ret = 1;
    while (b){
        if (b & 1) ret = ret * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return ret;
}

int main(){
    read(n);
    for (int i = 1; i <= n; ++i)
        read(a[i]);
    f[1] = a[1];
    inv = qpow(2, mod - 2);
    LL tnp = inv;
    b[1] = a[1] * inv % mod;
    b[2] = (b[1] + a[2] * inv % mod) % mod;
    for (int i = 3; i <= n; ++i){
        inv = inv * tnp % mod;
        b[i] = (b[i - 1] - inv * a[i - 1] % mod + a[i] * inv % mod) % mod;
    }
    for (int i = 2; i <= n; ++i)
        f[i] = (f[i - 1] + b[i]) % mod;
    LL ans = f[n] * qpow(2, n - 1) % mod;
    ans = (ans + mod) % mod;
    printf("%I64d\n", ans);
    return 0;
}

【F】Dominant Indices

  • 【题意】给出一棵以 1 1 号节点为根的树。求出对于1~ n n 每个节点,在其子树中宽度最大的一层到该节点的距离,若有多层宽度相同取深度最小的。
  • 【题解】对每个节点维护其子树中各个深度的点的个数,在原树上从叶子向上做线段树合并。每到一个节点就在线段树上二分出最大值,求出答案并记录一下。
  • 时间复杂度O(NlogN)
  • 【代码】
#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
#define LL  long long
#define MAXN    1000004
using namespace std;
int n, ans[MAXN];
vector <int> a[MAXN];

template <typename T> void chkmin(T &x, T y){x = min(x, y);}
template <typename T> void chkmax(T &x, T y){x = max(x, y);}
template <typename T> void read(T &x){
    x = 0; int f = 1; char ch = getchar();
    while (!isdigit(ch)) {if (ch == '-') f = -1; ch = getchar();}
    while (isdigit(ch)) {x = x * 10 + ch - '0'; ch = getchar();}
    x *= f;
}

struct Segment_Tree{
    struct info{int ls, rs, maxn;}a[MAXN * 26];
    int root[MAXN], cnt, n;
    void push_up(int pos){
        a[pos].maxn = max(a[a[pos].ls].maxn, a[a[pos].rs].maxn);
    }
    int merge(int x, int y, int l, int r){
        if (x == 0 || y == 0) return x + y;
        if (l == r){
            a[x].maxn = a[x].maxn + a[y].maxn;
            return x;
        }
        int mid = (l + r) >> 1;
        a[x].ls = merge(a[x].ls, a[y].ls, l, mid);
        a[x].rs = merge(a[x].rs, a[y].rs, mid + 1, r);
        push_up(x);
        return x;
    }
    void merge(int p, int x, int y){
        root[p] = merge(root[x], root[y], 1, n);
    }
    void modify(int &pos, int l, int r, int p, int d){
        if (!pos) pos = ++cnt;
        if (l == r) {a[pos].maxn += d; return;}
        int mid = (l + r) >> 1;
        if (p <= mid) modify(a[pos].ls, l, mid, p, d);
        else modify(a[pos].rs, mid + 1, r, p, d);
        push_up(pos);
    }
    void modify(int T, int p, int d){
        modify(root[T], 1, n, p, d);
    }
    void init(int x){
        n = x;
        cnt = 0;
    }
    int query(int pos, int l, int r){
        if (l == r) return l;
        int mid = (l + r) >> 1;
        if (a[a[pos].ls].maxn == a[pos].maxn) return query(a[pos].ls, l, mid);
        else return query(a[pos].rs, mid + 1, r);
    }
    int query(int T){
        return query(root[T], 1, n);
    }
}sgt; 

void dfs(int pos, int dad, int dep){
    bool is_leaf = 1;
    for (unsigned i = 0, si = a[pos].size(); i < si; ++i){
        int son = a[pos][i];
        if (son != dad){
            dfs(son, pos, dep + 1);
            is_leaf = 0;
        }
    }
    if (is_leaf) {
        ans[pos] = 0;
        sgt.modify(pos, dep, 1);
    } else {
        for (unsigned i = 0, si = a[pos].size(); i < si; ++i){
            int son = a[pos][i];
            if (son != dad){
                sgt.merge(pos, pos, son);
            }
        }
        sgt.modify(pos, dep, 1);
        ans[pos] = sgt.query(pos) - dep;
    }
}

int main(){
    read(n);
    for (int i = 1; i < n; ++i){
        int u, v; read(u), read(v);
        a[u].push_back(v);
        a[v].push_back(u);
    }
    sgt.init(n);
    dfs(1, 0, 1);
    for (int i = 1; i <= n; ++i)
        printf("%d\n", ans[i]);
    return 0;
}

【G】Allowed Letters

  • 【题意】给出一个长度为 n n 的仅包含字符a~ f ′ f ′ 的字符串,每次可以任意交换两个位置的字符。对于一些位置,规定这个位置只能选择哪些字符,对于其他没有规定的位置,可以选择任意的字符。求交换后的字典序最小的合法字符串,或输出无解。
  • 【题解】先考虑一种可以判断是否有解的方法。用网络流做二分图匹配,左边一排点表示 a ′ a ′ ~ f ′ f ′ ,右边一排点二进制表示可以选哪些字符。 S S 向左边一排点连边,流量为这种字符的数量;左边一排点向右边一排点中选择这种字符的点连边,流量为INF;右边一排点向 T T 连边,流量为满足这种限制的位置的数量。若满流,即存在完美匹配,则有解,否则无解。
    此时考虑字典序最小的限制,贪心地从前向后逐位确定,每一次强制一条边至少要流过1的流量,然后判断是否还存在完美匹配即可,那么这样我们就得到了一种退流的做法。
    但这是一张二分图,判断二分图的完美匹配是否存在可以使用Hall定理。Hall定理:二分图 G G 中的两部分顶点组成的集合分别为X Y Y ,其中|X||Y|,存在完美匹配的充要条件是: X X 中的任意k个点至少与 Y Y 中的k个点相邻( 1k|X| 1 ≤ k ≤ | X | )。
    我们称在左边一排点中选择的可重集合为 x x ,右边一排点中选择的可重集合为y。考虑若一种存在情况中将所有在这种情况的子集都选上时,满足 |x||y| | x | ≥ | y | , 那么 y y 的所有不改变存在性的子集都满足(相当于|x|不变 |y| | y | 减小),所以只要枚举每一种存在情况进行判断即可。
    具体实现,记录 f[pos][s] f [ p o s ] [ s ] 表示从pos向后有多少位置的 mask m a s k s s 的子集,从前向后从小到大枚举放哪个字母,更改sum,若对于下一个位置 pos p o s ,满足 s ∀ s tssum[t]f[pos][s] ∑ t ∈ s s u m [ t ] ≥ f [ p o s ] [ s ] 则表示可行,否则需将 sum s u m 改回来并尝试下一个字母。若一个位置放每一种字母都不可行,输出无解。
  • 时间复杂度 O(NA22A) O ( N ∗ A 2 ∗ 2 A ) ,其中 A A 为字符集大小,本题中A=6
  • 【代码】
#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
#define LL  long long
#define MAXN    100010
using namespace std;
int n, m, sum[8], f[MAXN][(1 << 6) + 2], mask[MAXN], maxn;
char s[MAXN], t[8], ans[MAXN];

template <typename T> void chkmin(T &x, T y){x = min(x, y);}
template <typename T> void chkmax(T &x, T y){x = max(x, y);}
template <typename T> void read(T &x){
    x = 0; int f = 1; char ch = getchar();
    while (!isdigit(ch)) {if (ch == '-') f = -1; ch = getchar();}
    while (isdigit(ch)) {x = x * 10 + ch - '0'; ch = getchar();}
    x *= f;
}

int main(){
    scanf("%s", s + 1);
    n = strlen(s + 1);
    maxn = (1 << 6) - 1;
    for (int i = 1; i <= n; ++i)
        mask[i] = maxn;
    for (int i = 1; i <= n; ++i)
        ++sum[s[i] - 'a'];
    read(m);
    for (int i = 1; i <= m; ++i){
        int pos; read(pos);
        scanf("%s", t + 1);
        int tmp = 0;
        for (int j = 1; j <= (int)strlen(t + 1); ++j)
            tmp |= (1 << (t[j] - 'a'));
        mask[pos] = tmp;
    }
    for (int i = 1; i <= n; ++i){
        for (int j = 1; j <= maxn; ++j)
            if ((mask[i] | j) == j) ++f[i][j];
    }
    for (int i = n; i >= 1; --i)
        for (int j = 1; j <= maxn; ++j)
            f[i][j] += f[i + 1][j];
    for (int i = 1; i <= n; ++i){
        int fla = 0;
        for (int cur = 0; cur <= 5; ++cur){
            if (!sum[cur]) continue;
            if (!(mask[i] & (1 << cur))) continue;
            --sum[cur];
            int ff = 1;
            for (int j = 1; j <= maxn; ++j){
                int tmp = 0;
                for (int t = 0; t <= 5; ++t)
                    if (j & (1 << t)) tmp += sum[t];
                if (tmp < f[i + 1][j]) {ff = 0; break;}
            }
            if (!ff) ++sum[cur];
            else {ans[i] = cur + 'a'; fla = 1; break;}
        }
        if (!fla) {printf("Impossible\n"); return 0;}
    }
    for (int i = 1; i <= n; ++i)
        printf("%c", ans[i]);
    printf("\n");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
"educational codeforces round 103 (rated for div. 2)"是一个Codeforces平台上的教育性比赛,专为2级选手设计评级。以下是有关该比赛的回答。 "educational codeforces round 103 (rated for div. 2)"是一场Codeforces平台上的教育性比赛。Codeforces是一个为程序员提供竞赛和评级的在线平台。这场比赛是专为2级选手设计的,这意味着它适合那些在算法和数据结构方面已经积累了一定经验的选手参与。 与其他Codeforces比赛一样,这场比赛将由多个问题组成,选手需要根据给定的问题描述和测试用例,编写程序来解决这些问题。比赛的时限通常有两到三个小时,选手需要在规定的时间内提交他们的解答。他们的程序将在Codeforces的在线评测系统上运行,并根据程序的正确性和效率进行评分。 该比赛被称为"educational",意味着比赛的目的是教育性的,而不是针对专业的竞争性。这种教育性比赛为选手提供了一个学习和提高他们编程技能的机会。即使选手没有在比赛中获得很高的排名,他们也可以从其他选手的解决方案中学习,并通过参与讨论获得更多的知识。 参加"educational codeforces round 103 (rated for div. 2)"对于2级选手来说是很有意义的。他们可以通过解决难度适中的问题来测试和巩固他们的算法和编程技巧。另外,这种比赛对于提高解决问题能力,锻炼思维和提高团队合作能力也是非常有帮助的。 总的来说,"educational codeforces round 103 (rated for div. 2)"是一场为2级选手设计的教育性比赛,旨在提高他们的编程技能和算法能力。参与这样的比赛可以为选手提供学习和进步的机会,同时也促进了编程社区的交流与合作。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值