2021年暑假康复性训练(Codeforces Round #731 (Div. 3))全题解

Posted PushyTao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2021年暑假康复性训练(Codeforces Round #731 (Div. 3))全题解相关的知识,希望对你有一定的参考价值。

Codeforces Round #731 (Div. 3)

A Shortest Path with Obstacle

在这里插入图片描述
input

7

1 1
3 3
2 2

2 5
2 1
2 3

1000 42
1000 1
1000 1000

1 10
3 10
2 10

3 8
7 8
3 7

2 1
4 1
1 1

1 344
1 10
1 1

output

4
6
41
4
4
2
334

在这里插入图片描述
code:

int main()
{
    int _ = read;
    while(_ --){
        int x1 = read,y1 = read;
        int x2 = read,y2 = read;
        int tx1 = read,ty1 = read;
        int ans = abs(x1 - x2) + abs(y1 - y2);
        if(x1 == x2 && x2 == tx1 && ty1 >= min(y1,y2) && ty1 <= max(y2,y1)) ans += 2;
        if(y1 == y2 && y2 == ty1 && tx1 >= min(x1,x2) && tx1 <= max(x1,x2)) ans += 2;
        printf("%d\\n",ans);
    }
    return 0;
}

B. Alphabetical Strings

在这里插入图片描述
input

11
a
ba
ab
bac
ihfcbadeg
z
aa
ca
acb
xyz
ddcba

output:

YES
YES
YES
YES
YES
NO
NO
NO
NO
NO
NO

Note
The example contains test cases from the main part of the condition.
code:

int main()
{
    int _ = read;
    while(_ --){
        char str[100];
        cin >> (str + 1);
        int len = strlen(str + 1);
        int l = 1,r = len;
        int flag = 0;
        for(int i = len;i >= 1;i--){
            char c = i + 96;
            if(c == str[l]) l ++;
            else if(c == str[r]) r --;
            else flag = 1;
        }
        if(flag) puts("NO");
        else puts("YES");
    }
    return 0;
}
/**
 
**/

C. Pair Programming

在这里插入图片描述
input:

5

3 2 2
2 0
0 5

4 3 2
2 0 5
0 6

0 2 2
1 0
2 3

5 4 4
6 0 8 0
0 7 0 9

5 4 1
8 7 8 0
0

output:

2 0 0 5 
0 2 0 6 5 
-1
0 6 0 7 0 8 0 9
-1

int main()
{
    int _ = read;
    while(_ --){
        int k = read,n = read,m = read;
        for(int i=1;i<=n;i++) a[i] = read;
        for(int i=1;i<=m;i++) b[i] = read;
        int p1 = 1,p2 = 1;
        vector<int> vet;
        int flag = 0;
        while(p1 <= n || p2 <= m){
            if(p1 <= n && a[p1] <= k){
                if(a[p1] == 0) k++;
                vet.push_back(a[p1]);
                p1 ++;
            }
            else if(p2 <= m && b[p2] <= k){
                if(b[p2] == 0) k ++;
                vet.push_back(b[p2]);
                p2 ++;
            }
            else{
                flag = 1;
                break;
            }
        }
 
        if(flag) puts("-1");
        else{
            int siz = n + m;
            for(int i=1;i<=siz;i++){
                printf("%d ",vet[i-1]);
            }puts("");
        }
 
    }
    return 0;
}

D. Co-growing Sequence

在这里插入图片描述
input:

5
4
1 3 7 15
4
1 2 4 8
5
1 2 3 4 5
4
11 13 15 1
1
0

output:

0 0 0 0 
0 1 3 7 
0 1 0 3 2 
0 2 0 14 
0 

code:

int a[maxn<<1],b[maxn<<1];
int main()
{
    int _ = read;
    while(_ --){
        int n = read;
        for(int i=1;i<=n;i++){
            a[i] = read;
            if(i >= 2){
                int temp = (a[i] | a[i-1]);
                b[i] = a[i] ^ temp;
                a[i] = temp;
            }
        }
        for(int i = 1;i <= n;i++){
            printf("%d ",b[i]);
        }puts("");
    }
    return 0;
}

E. Air Conditioners

在这里插入图片描述
input:

5

6 2
2 5
14 16

10 1
7
30

5 5
3 1 4 2 5
3 1 4 2 5

7 1
1
1000000000

6 3
6 1 3
5 5 5

output:

15 14 15 16 16 17 
36 35 34 33 32 31 30 31 32 33 
1 2 3 4 5 
1000000000 1000000001 1000000002 1000000003 1000000004 1000000005 1000000006 
5 6 5 6 6 5 

int a[maxn << 1], b[maxn << 1];
typedef pair<int, int> PII;
vector<PII> vet;
int main()
{
    int _ = read;
    while(_ --)
    {
        memset(b,0x3f3f3f3f,sizeof b);
        int n = read, m = read;
        for(int i = 1; i <= m; i++) a[i] = read;
        for(int i = 1; i <= m; i++)
        {
            b[a[i]] = read;
        }
        for(int i = 1; i <= n; i++) b[i] = min(b[i], b[i - 1] + 1);
        for(int i = n - 1; i >= 1; i--) b[i] = min(b[i], b[i + 1] + 1);
        for(itn i = 1; i <= n; i++)
        {
            printf("%d ", b[i]);
        }
        puts("");
    }
    return 0;
}

另一种做法:

int n, m;
struct node{
    int u,v,w,nex;
}e[maxn];
int head[maxn << 1],cnt,dis[maxn << 1],a[maxn << 1];
bool vis[maxn << 1];
void init(int x){
    cnt = 0;
    for(int i=1;i<=x;i++) head[i] = -1,vis[i] = 0,dis[i] = INF;
}
void add(int u,int v,int w){
    e[cnt].u = u;
    e[cnt].v = v;
    e[cnt].w = w;
    e[cnt].nex = head[u];
    head[u] = cnt++;
}
typedef pair<int,int> PII;
void dijkstra(int x){
    priority_queue<PII,vector<PII>, greater<PII> > que;
    dis[x] = 0;
    vis[x] = 1;
    que.push({0,x});
    while(que.size()){
        PII t = que.top();que.pop();
        int u = t.second;
        int w = t.first;
        vis[u] = 0;
        for(int i=head[u];~i;i=e[i].nex){
            int to = e[i].v;
            int w = e[i].w;
            if(dis[to] > dis[u] + w){
                dis[to] = dis[u] + w;
                if(!vis[to]){
                    que.push({dis[to],to});
                }
            }
        }
    }
}
int main() {
    int _ = read;
    while(_ --){
        int n = read,k = read;
        init(n + 1000);
        int pt = n + 1;
        for(int i=1;i<=k;i++) a[i] = read;
        for(int i=1;i<=k;i++){
            int w = read;
            add(pt,a[i],w);
        }
        for(int i=1;i<=n;i++){
            if(i != 1) add(i,i-1,1);
            if(i != n) add(i,i+1,1);
        }
        dijkstra(pt);
        for(int i=1;i<=n;i++) printf("%d ",dis[i]);
        puts("");
    }
	return 0;
}

F. Array Stabilization (GCD version)

在这里插入图片描述
input:

5
4
16 24 10 5
4
42 42 42 42
3
4 6 4
5
1 2 3 4 5
6
9 9 27 9 9 63

output:

3
0
2
1
1

code:
首先是一个错误的代码,但是可以ac的

int n;
int st[maxn][21];
int a[maxn << 1],b[maxn << 1];
int get(int i,int j){
    int t = log2(j-i+1);
    return gcd(st[i][t], st[j-(1<<t)+1][t]);
}
int main() {
    int _ = read;
    while(_ --){
        n = read;
        for(int i=1;i<=n;i++)
            st[i][0] = st[i+n][0] = a[i+n] = a[i] = read,b[i] = 0;
        int ans = 0,flag = 0;
        for(int i=1;i<=n;i++){
            if(a[i] != a[1]) flag = 1;
        }
        if(!flag) {
            cout << 0 <<endl;
            continue;
        }
        int gd = a[1];
        for(int i=2;i<=n;i++) gd = gcd(gd,a[i]);
        ///cout << gd <<endl;
        int lim = 2 * n;
        for(int j=1;(1<<j) <= lim;j ++){
            for(int i=1;i + (1 << j) - 1 <= lim;i ++){
                st[i][j] = gcd(st[i][j-1],st[i+(1<<j-1)][j-1]);//错误!!!!!!
            }
        }
        for(int i=1;i<=n;i++){
            int l = i, r = n + i - 1;/// n + i - 1 - i + 1 == n
            while(l < r){
                int mid = l + r >> 1;
                if(get(i,mid) != gd) l = mid + 1;
                else r = mid;
            }
            ans = max(ans,l-i);
        }
        cout << ans <<endl;
    }
	return 0;
}

正确代码:

int n;
int st[maxn][21];
int a[maxn << 1],b[maxn << 1];
int get(int i,int j){
    int t = log2(j-i+1);
    return gcd(st[i][t], st[j-(1<<t)+1][t]);
}
int main() {
    int _ = read;
    while(_ --){
        n = read;
        for以上是关于2021年暑假康复性训练(Codeforces Round #731 (Div. 3))全题解的主要内容,如果未能解决你的问题,请参考以下文章

转载|膝骨性关节炎核心力量训练的必要性 ——北京清华长庚医院康复医学科KOA康复治疗的整体思考

暑假训练12010年湖南省赛题解

暑假训练12010年湖南省赛题解

Codeforces Round #540 Tanya and Candies 预处理

2021个人年度报告

2021年SWPUACM暑假集训day5单调栈算法