Codeforces Round #659 (Div.2) 做题报告

· · 个人记录

A. Common Prefixes

又是一道构造题,想明白了其实很简单的,假设原串是s_0,那么对于a_i=k,我们只要将s_0前k位不动,后面k位全部加一,如果是z就变成a即可。至此,不妨假设原串s_0是aaa\dots aaa,即可满足题意。

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 110;

int T;
int n, arr[MAXN], length;
int str[MAXN];

int main(){
    scanf("%d", &T); while(T--){
        scanf("%d", &n);
        length = 1;
        for(int i = 1; i <= n; i++){
            scanf("%d", &arr[i]);
            length = max(length, arr[i]);
        }
        for(int i = 1; i <= length; i++){
            str[i] = 0;
            printf("a");
        }
        printf("\n");
        for(int i = 1; i <= n; i++){
            for(int j = arr[i] + 1; j <= length; j++){
                str[j] = (str[j] + 1) % 26;
            }
            for(int j = 1; j <= length; j++)
                printf("%c", 'a' + str[j]);
            printf("\n");
        }
    }
    return 0;
}

C. String Transformation 1

由于B题明显比C题难,我们先看C题。

由于每个数只能变大,所以只要有位置变小,就一定不可行。除此之外,最差也是每个点单独变大,所以一定可行。我们经过仔细仔细仔细的读题后会发现(因为读错题浪费近一个小时调的蒟蒻在此),每次选的是一个子序列,不用连续,不用连续!那我们很容易想到,将将多个数一起变大比仅仅一个数变大一定更优,因为都是一次操作。所以我们只用从$a$到$t$枚举一下,扫$20$遍,每次将所有$a_i=x\;(x\in\left('a','t'\right))$都变成$min\left\{b_i\right\}$即可,可以证明这么操作一定是最优的。 我想到了一个绝妙的证明方法,可惜这里空间太小写不下。 ```cpp #include<bits/stdc++.h> using namespace std; const int MAXN = 100100; int T; int n; char a[MAXN], b[MAXN]; int main(){ scanf("%d", &T); while(T--){ scanf("%d", &n); scanf("%s", a + 1); scanf("%s", b + 1); bool flg = false; for(int i = 1; i <= n; i++){ if(a[i] > b[i]){ flg = true; break; } } if(flg){ printf("-1\n"); continue; } int ans = 0; for(char i = 'a'; i <= 'z'; i++){ queue<int> que; char dest = 'z'; for(int j = 1; j <= n + 1; j++){ if(a[j] == i && a[j] != b[j]){ que.push(j); dest = min(dest, b[j]); } } if(!que.empty()){ ans++; while(!que.empty()) a[que.front()] = dest, que.pop(); } } printf("%d\n", ans); } return 0; } ``` ### [B1. Koa and the Beach (Easy Version)](https://codeforc.es/contest/1384/problem/B1) ### [B2. Koa and the Beach (Hard Version)](https://codeforc.es/contest/1384/problem/B2) 依旧未能成功想到针对$B1$的部分分怎么求解,所以直接讲$B2$的解法。 海浪会上下移动,我们只要从头游到尾。但是这么考虑就会很烦,所以不妨认为海浪不会升高降低,只有$Koa$的身高会不断变长缩短。那么对于一个$d_i\leq l-k$的海,就是无论什么时候,$Koa$的身高一定可以站到底,于是原问题就被分割成了很多子问题,每一个子问题都不会出现$a_i\leq l-k$的位置。这有什么好处呢?我们可以发现,如果将波动的周期向后移动$k$个位置,身高函数就变成了一个三角波,且起始点$t=0$在三角波的最低点。所以每一块海一定是一段连续的区间可以让$Koa$通过,这就保证了一定要在一个周期($2k$)内,抵达下一块可以休息的位置。所以我们只用记录最早抵达$i$点的时间$T_{earliest}$可能是多少即可,因为如果$T_{earliest}\leq k$那么,只要$T_{earliest}+1\leq$下一个点可以到的时间即可。如果$T_{earliest}>k$,只要考虑$T_{earliest}+1\leq$下一个点最晚什么时候可以到达即可。如果能够上下一个中继节点就可以到任意位置重新开始新的周期,这样就可以了。 ```cpp #include<bits/stdc++.h> using namespace std; const int MAXN = 300100; int T; int n, k, l, arr[MAXN]; inline int f(int x){ if(0 <= x && x <= k) return x; else return k - (x - k); } int main(){ scanf("%d", &T); while(T--){ scanf("%d %d %d", &n, &k, &l); for(int i = 1; i <= n; i++) scanf("%d", &arr[i]); int ans = 1; int earliest = -1; for(int i = 1; i <= n; i++){ if(arr[i] <= l - k){ earliest = -1; continue; } if(arr[i] > l){ ans = 0; break; } int left_time = arr[i] - (l - k); int right_time = k + l - arr[i]; // printf(":%d %d %d -> %d\n", i, left_time, right_time, earliest); if(earliest + 1 > right_time){ ans = 0; break; }else{ earliest = max(earliest + 1, left_time); } } if(!ans) printf("No\n"); else printf("Yes\n"); } return 0; } ``` 最终得分:$2309=482+272+408+1147$,排名$257$。