Codeforces Round #543

Posted hjmmm

tags:

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

(比赛链接)[http://codeforces.com/contest/1120]

A

给出一个长度为m的序列a 可以删除一些数
使得最后从没删的第一个数开始 每k个一截
截出的n个多重集合中 至少有一个包含所给的长度为s的多重集b
\(n, m, k, s, 两个序列中的数 \leq 5e5\)

考虑每个位置r 找到满足[l, r]中包含b的区间的最大的l 显然l是单调递增的
然后我们需要讨论r - l + 1大于等于k 还是小于k
update : 不过其实不用 强行r - l + 1 >= k显然可行
然后前l - 1个数删除\((l - 1) % k\)个 [l, r]中删除r - l + 1 - k个 就可以啦
维护每一个数的出现位置 在后面出现了就更新

#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <queue>
#include <vector>
#include <set>
using namespace std;
typedef multiset<int> :: iterator SIT;
const int N = 5e5 + 5;
int n, m, s, k, lim, tot;
int a[N], b[N], c[N];
int main(){
    scanf("%d%d%d%d", &m, &k, &n, &s); lim = m - n * k;
    for(int i = 1; i <= m; ++i) scanf("%d", &a[i]);
    for(int i = 1, x; i <= s; ++i) scanf("%d", &x), tot += (!b[x]), ++b[x];
    for(int r = 1, l = 1; r <= m; ++r){
        ++c[a[r]]; tot -= (c[a[r]] == b[a[r]]);
        while(!tot && l < r && r - l + 1 > k && c[a[l]] > b[a[l]]) --c[a[l]], ++l;
        int tt = (l - 1) % k;
        if(!tot && r - l + 1 >= k && tt + (r - l + 1 - k) <= lim){
            printf("%d\n", tt + (r - l + 1 - k));
            for(int i = 1; i <= tt; ++i) printf("%d ", i);
            for(int i = l, j = (r - l + 1 - k); i <= r && j; ++i) if(c[a[i]] > b[a[i]]){
                printf("%d ", i); --c[a[i]]; --j;
            } 
            return 0;
        }
    }
    printf("-1");
    return 0;
}

B

给出长度为n的数a和数b
在保证每一位置在整个过程中取值[0, 9]且第一个数取值[1, 9]的情况下
每次可以选择把相邻的两个数同时加一或减一
如果a不能变成b的话 输出-1
\(n \leq 1e5\)

首先可以推出: 操作的先后顺序不影响结果, 所以如果一位一位地做 最后一位不相等 那么就不成立
那么 直接一位一位移动就好了。。如何解决超过范围的问题呢?
198 -> 297 现在要把第一位加一 但是第二位就超了 所以我们先把第二位减一
那么如果第二位也不满足呢?1902 -> 2903 那么递归下去就好了。。

#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <queue>
#include <vector>
#include <set>
#define mp(x, y) make_pair(x, y)
using namespace std;
typedef multiset<int> :: iterator SIT;
typedef pair<int, int> PII;
const int N = 5e5 + 5;
const long long LIM = 1e5;
char sa[N], sb[N];
int n, a[N], b[N], c[N];
PII opt[N];
int top;
long long ans;
void solve(int x, int type){
    if(ans <= 0) return ;
    if(x == n){printf("-1\n"); exit(0);}
    if(a[x + 1] + type > 9 || a[x + 1] + type < 0) solve(x + 1, -type);
    a[x] += type, a[x + 1] += type;
    if(ans > 0) --ans, printf("%d %d\n", x, type);
}
int main(){
    scanf("%d%s%s", &n, sa + 1, sb + 1);
    for(int i = 1; i <= n; ++i) a[i] = sa[i] - '0', b[i] = sb[i] - '0', c[i] = a[i];
    for(int i = 1; i <= n - 1; ++i){
        int d = b[i] - c[i]; c[i] += d, c[i + 1] += d, ans += abs(d); 
    }
    if(c[n] != b[n]){printf("-1\n"); return 0;}
    printf("%lld\n", ans); ans = min(ans, LIM); 
    for(int i = 1; i <= n && ans > 0; ++i){
        while(a[i] < b[i] && ans > 0) solve(i, 1);
        while(a[i] > b[i] && ans > 0) solve(i, -1);
    }
    return 0;
}

C

现在你要构造一个指定的字符串S
如果你目前建出了str 那么你有两种操作
在末尾添加一个字符 花费为a
在末尾添加一个str的子串 花费为b
\(a, b, len \leq 5000\)

边建字符串SAM边dp
每插入一个字符 就先把它放入SAM
然后从起点跑一边[i + 1, n] 用子串更新f
即f[i] = min(f[i], f[i - 1] + a), f[j] = min(f[j], f[i] + b) (S[i + 1, j]是str的子串)

以上是关于Codeforces Round #543的主要内容,如果未能解决你的问题,请参考以下文章

[Codeforces Round #522 (Div. 2, based on Technocup 2019 Elimination Round 3)][C. Playing Piano](代码片段

Codeforces#543 div2 B. Mike and Children(暴力?)

Codeforces543BDestory Roads心得

codeforces 543B Destroying Roads

Codeforces 543 B. World Tour

Codeforces543 B. Destroying Roads