AtCoder Beginner Contest 221 A~E题解

发挥比较好的一场,就来搓篇题解。

F 没时间写了,最后只是口胡了一下(还不是很对,改完后放上)。

比赛传送

A - Seismic magnitude scales

给你 (A,B),表示两场地震的强度。已知强度每 (+1) 所产生的能量就是原来的 (32) 倍。求 (A) 地震产生的能量是 (B) 地震产生能量的多少倍。数据保证 (A ge B)

输出 (32^{A-B}) 即可。

int Pow(int x, int p) {
    int res = 1;
    while(p) {
        if(p & 1) res = res * x;
        x = x * x, p >>= 1;
    }
    return res;
}

signed main()
{
	int A = read(), B = read();
	int x = A - B;
	cout<<Pow(32, x);
    return 0;
}

B - typo

给你两个相同长度的字符串 (S,T),问 (S) 串交换一次或不交换能否得到 (T) 串。

找到第一个不相同的位置交换一下,然后重新判断整串是否相同即可。

int main()
{
	cin >> s + 1 >> t + 1;
	int len1 = strlen(s + 1);
	int len2 = strlen(t + 1);
	int cnt = 0;
	for(int i = 1; i < len1; ++i) {
	    if(s[i] != t[i] && s[i + 1] != t[i + 1]) {
	        swap(s[i], s[i + 1]);
	        break;
        }
    }
    int flag = false;
    for(int i = 1; i <= len1; ++i) {
        if(s[i] != t[i]) {
            flag = true;
        }
    }
    if(flag) puts("No");
    else puts("Yes"); 
    return 0;
}

C - Select Mul

给你一个数 (n)(n) 的每位数可以随便排列,然后可以将 (n) 分成两部分。求这两部分乘积的最大值。注意两部分都不能有前导零。 (n le 10^9)

模拟即可。

(n) 拆位,枚举 (n) 所有位数的全排列,对于每个排列暴力分成两部分求乘积最大值。

总复杂度大概是 (mathcal O( s ! s^2)),其中 (s) 表示 (n) 的位数。反正随便过喽。

void Calc() {
    for(int i = sc; i >= 2; --i) {
        int flag = false;
        for(int j = sc; j >= i; --j) {
            if(!stc[j]) flag = true;
            if(stc[j]) break;
        }
        if(flag) continue;
        flag = false;
        for(int j = i - 1; j >= 1; --j) {
            if(!stc[j]) flag = true;
            if(stc[j]) break;
        }
        if(flag) continue;
        int x = 0, y = 0;
        for(int j = sc; j >= i; --j) x = x * 10 + stc[j];
        for(int j = i - 1; j >= 1; --j) y = y * 10 + stc[j];
        ans = max(ans, x * y);
    }
}

signed main()
{
	n = read();
	while(n) {
	    stc[++sc] = n % 10;
	    n /= 10;
    }
    sort(stc + 1, stc + sc + 1);
    do {
        Calc();
    }while(next_permutation(stc + 1, stc + sc +1));
    printf("%lld
", ans);
    return 0;
}

D - Online games

给你 (n) 条线段,每条线段有一个左端点 (a_i) 和长度 (b_i)。设 (d_k) 表示有 (d_k) 个点被覆盖了 (k) 次,求出所有 (d_k (k in [1,n])) 并输出。
(n le 2 imes 10^5)(1 le a_i,b_i le 10^9)

这个问题好像非常经典。

考虑差分统计贡献。

需要对 +1 贡献的 左端点 和有 -1 贡献的 右端点+1 进行离散化。

设离散化后一共有 (Cnt) 个点,实际上这 (Cnt) 个点构成了 (Cnt - 1) 个左开右闭的区间,那么

(i) 个点用来统计 ([date_{i},date_{i+1})) 这段区间的贡献,直接用一个 (cnt_i) 来记录。(显然这段区间中没个点被覆盖次数都是一样的吧)

最后枚举所有表示的区间统计答案即可。

signed main()
{
	n = read();
	for(int i = 1; i <= n; ++i) {
        a[i] = read(), b[i] = a[i] + read();
        date[++date_num] = a[i];
        date[++date_num] = b[i];
    }
    sort(date + 1, date + date_num + 1); date[0] = -INF;
    for(int i = 1; i <= date_num; ++i) if(date[i] != date[i - 1]) date[++Cnt] = date[i];
    for(int i = 1; i <= n; ++i) {
        a[i] = lower_bound(date + 1, date + Cnt + 1, a[i]) - date;
        b[i] = lower_bound(date + 1, date + Cnt + 1, b[i]) - date;
        cnt[a[i]]++, cnt[b[i]]--;
    }
    date[0] = 0;
    for(int i = 1; i <= Cnt; ++i) {
        cnt[i] += cnt[i - 1];
    }
//    for(int i = 1; i <= Cnt; ++i) cout<<cnt[i]<<" "; puts("");
//    for(int i = 1; i <= Cnt; ++i) cout<<date[i]<<" "; puts("");
    for(int i = 2; i <= Cnt; ++i) {
        ans[cnt[i - 1]] += date[i] - date[i - 1];
    }
    for(int i= 1; i <= n; ++i) {
        printf("%lld ", ans[i]);
    }
    return 0;
}

E - LEQ

给你一个长度为 (n) 的序列 (a),统计有多少长度 (k ge 2) 的子序列 (a_{b_1},a_{b_2},...,a_{b_k}) 满足 (a_{b_1} le a_{b_k}),答案对 (998244353) 取模。
(n le 3 imes 10^5, 1 le a_i le 10^9)

一开始你很想当然的以为统计的是长度 (ge 2) 的子串,然后你感觉这个题简单的一匹,离散化后想当然的敲了一个树状数组维护出现次数。

你发现第一个样例不对,然后你发现它题目写的的是子序列。。。

没错就是我。

那好,正文开始。

我们延续上面的做法,想想怎么魔改一下。

不难发现所有贡献只和序列首元素和序列尾元素之间的大小关系有关。

然后你考虑找出所有满足 (a_j le a_i)((j,i)) 数对,然后 (j,i) 之间的数可以随便选,假设有 (s = i - j - 1) 个,那么 ((j,i)) 的贡献就是 (2^s)

暴力找所有合法数对是 (mathcal O(n^2)) 的,考虑用上面那个错解的方法优化。

假设现在有三个点 (j,k,i),满足 (j,k < i)(j = k-1)(a_j le a_i)(a_k le a_i)

不难发现 ((j,i)) 的贡献是 ((k,i)) 的贡献的 (2^1) 倍。

那我们对 (j) 这个点在树状数组上统计贡献时可以用 (2^{n-j}),这样每个点的贡献都是 (2)(2) 倍的缩小。

然后考虑枚举右端点 (i),并且再次之前 ([1,i-1]) 中的点的全部被统计进树状数组了。

假设 (j_1,j_2,...,j_k) 的点都满足 (a_{j_x} le a_i)

此时在树状数组上查询的和其实就是 (displaystyle sum_{x=1}^{k} 2^{n-j_x})

显然算多了,那么算多了多少?

假设 (j_k = i - 1),但 (j_k) 在树状数组中的贡献为 (2^{n-i+1}),它与 (i) 的贡献为 (2^0 = 1),所以应该除以这个数 (2^{n-i+1}),然后你发现与前面所有的有贡献的点都需要除以 (2^{n-i+1})

所以答案为

[frac{displaystyle sum_{x=1}^{k} 2^{n-j_x}}{2^{n-i+1}} ]

统计所有 (i) 的贡献求和即为最终答案。

总时间复杂度为 (mathcal O(n log^2 n)),如果你预处理 (2) 的次幂,可以做到 (mathcal O(n log n))

namespace Bit {
    int sum[MAXN];
    int lb(int x) { return x & -x; }
    void Modify(int x, int k) { for(; x <= Cnt; x += lb(x)) sum[x] = (sum[x] + k) % mod; }
    int Query(int x) { int res = 0; for(; x; x -= lb(x)) res = (res + sum[x]) % mod; return res; }
}

int Pow(int x, int p) {
    int res = 1;
    while(p) {
        if(p & 1) res = res * x % mod;
        x = x * x % mod;
        p >>= 1;
    }
    return res;
}

signed main()
{
	n = read();
	for(int i = 1; i <= n; ++i) {
	    a[i] = read();
	    date[i] = a[i];
    }
    sort(date + 1, date + n + 1), date[0] = -INF;
    for(int i = 1; i <= n; ++i) if(date[i] != date[i - 1]) date[++Cnt] = date[i];
    for(int i = 1; i <= n; ++i) a[i] = lower_bound(date + 1, date + Cnt + 1, a[i]) - date;
    for(int i = 2; i <= n; ++i) {
        Bit::Modify(a[i - 1], Pow(2, n - i));
        int res = Bit::Query(a[i]);
//        cout<<res<<" "<<sum<<"
";
        ans = (ans + res * Pow(Pow(2, (n - i)), mod - 2) % mod) % mod; 
    }
    printf("%lld
", ans);
    return 0;
}
原文地址:https://www.cnblogs.com/Silymtics/p/atcoder-ABC221.html