Educational Codeforces Round 55 题解

题解 CF1082A 【Vasya and Book】

史上最难A题,没有之一

从题意可以看出,翻到目标页只有三种办法

  • 先从\(x\)到\(1\),再从\(1\)到\(y\)
  • 先从\(x\)到\(n\),再从\(n\)到\(y\)
  • 直接从\(x\)到\(y\)

三种的必要条件分别是

  • \((y-1)\mod d \equiv 0\)
  • \((n-y)\mod d \equiv 0\)
  • \(|x-y|\mod d \equiv 0\)

所以如果上面三种都不满足的话就输出\(-1\)

不然就取最小的输出

# include <bits/stdc++.h>

int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        int n, x, y, d;
        scanf("%d%d%d%d", &n, &x, &y, &d);
        int ans = 0x7f7f7f7f;
        if(abs(x - y) % d == 0)
            ans = abs(x - y) / d;
        if((y - 1) % d == 0)
            ans = std::min(ans, (x - 1) / d + bool((x - 1) % d) + (y - 1) / d);
        if ((n - y) % d == 0)
            ans = std::min(ans, (n - x) / d + bool((n - x) % d) + (n - y) / d);
        if(ans == 0x7f7f7f7f)
        {
            printf("-1\n");
            continue;
        }
        printf("%d\n", ans);
    }
    return 0;
}

题解 CF1082B 【Vova and Trophies】

\(B\)比\(A\)水qwq

这题,对每一个‘‘\(G\)‘‘,求它这一块的左边界和右边界

然后对于每一个‘‘\(S\)‘‘,求一下他左边那块的大小,右边那块的大小,再判断一下他能不能把两块连在一起,不能就取大的那块,做完了

#include <bits/stdc++.h>

using std::string;

const int MaxN = 100010;

int a[MaxN];
int l[MaxN], r[MaxN];

int main()
{
    int n;
    string s;
    scanf("%d", &n);
    std::cin >> s;
    int len = s.length();
    int sum = 0, ans = 0;
    for (int i = 0; i < len; i++)
        a[i + 1] = s[i] == ‘S‘ ? 0 : 1, sum += a[i + 1];
    if (sum == 0)
        return printf("0") * 0;
    if (sum == n)
        return printf("%d\n", n) * 0;
    for (int i = 1; i <= n; i++)
    {
        if (a[i] == 1 && a[i - 1] == 1)
            l[i] = l[i - 1];
        else
            l[i] = i;
    }
    for (int i = n; i >= 1; i--)
    {
        if (a[i] == 1 && a[i + 1] == 1)
            r[i] = r[i + 1];
        else
            r[i] = i;
    }
    for (int i = 1; i <= n; i++)
    {
        if (a[i] == 0)
        {
            int tmp = 0;
            if (a[i - 1])
                tmp += r[i - 1] - l[i - 1] + 1;
            if (a[i + 1])
                tmp += r[i + 1] - l[i + 1] + 1;
            if(tmp == sum)
                ans = std::max(ans, tmp);
            if (tmp < sum)
                ans = std::max(ans, tmp + 1);
            if (a[i - 1] && r[i - 1] - l[i - 1] + 1 < sum)
                ans = std::max(r[i - 1] - l[i - 1] + 2, ans);
            if (a[i + 1] && r[i + 1] - l[i + 1] + 1 < sum)
                ans = std::max(r[i + 1] - l[i + 1] + 2, ans);

        }
    }
    printf("%d\n", ans);
    return 0;
}

原文地址:https://www.cnblogs.com/little-sun0331/p/10046308.html

时间: 2024-11-05 21:54:15

Educational Codeforces Round 55 题解的相关文章

Educational Codeforces Round 55 (Rated for Div. 2)

Educational Codeforces Round 55 (Rated for Div. 2) 链接 A Vasya and Book 傻逼题..注意判边界. #include<cstdio> #include<cstring> #include<algorithm> #include<queue> #include<set> #include<map> #include<vector> #include<cm

Codeforces Educational Codeforces Round 54 题解

题目链接:https://codeforc.es/contest/1076 A. Minimizing the String 题意:给出一个字符串,最多删掉一个字母,输出操作后字典序最小的字符串. 题解:若存在一个位置 i 满足 a[i] > a[i+1],若不删除 a[i] 则后续操作不可能更优. 1 #include <bits/stdc++.h> 2 using namespace std; 3 #define ll long long 4 #define ull unsigned

[Educational Codeforces Round 55 (Rated for Div. 2)][C. Multi-Subject Competition][时间复杂度]

https://codeforc.es/contest/1082/problem/C 题目大意:有m个类型,n个人,每个人有一个所属类型k和一个能力v,要求所选的类型的人个数相等并且使v总和最大(n,m<=1e5) 题解:用vector存下每种类型的各个v并且每种类型根据v按从大到小排序,然后处理出每种类型的前缀和,然后扫每种类型的所有前缀和,如果该类型在i处的前缀和大于0,则相应的ans[i]加上这个类型在i处的前缀和,最后求出max(ans[i])(1<=i<=n)即可. 注意:这题

Educational Codeforces Round 55 (Rated for Div. 2) C. Multi-Subject Competition 【vector 预处理优化】

传送门:http://codeforces.com/contest/1082/problem/C C. Multi-Subject Competition time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output A multi-subject competition is coming! The competition has mm 

Educational Codeforces Round 55 (Rated for Div. 2) B. Vova and Trophies

传送门 https://www.cnblogs.com/violet-acmer/p/10035971.html 题意: Vova有n个奖杯,这n个奖杯全部是金奖或银奖,Vova将所有奖杯排成一排,你最多可以交换其中两个奖杯,求最大的连续的金奖杯个数. 题解: 思路: 求出连续的金奖杯位置,找出每两个相邻的连续的金奖杯所能够形成的最大的连续的金奖杯的个数,输出最大值. 相关变量解释: 1 int n; 2 char trophy[maxn]; 3 struct Node 4 { 5 int l,

Codeforces 1082 C. Multi-Subject Competition-有点意思 (Educational Codeforces Round 55 (Rated for Div. 2))

C. Multi-Subject Competition time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output A multi-subject competition is coming! The competition has mm different subjects participants can choose from.

Codeforces 1082 A. Vasya and Book-题意 (Educational Codeforces Round 55 (Rated for Div. 2))

A. Vasya and Book time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Vasya is reading a e-book. The file of the book consists of nn pages, numbered from 11 to nn. The screen is currently disp

Codeforces 1082 D. Maximum Diameter Graph-树的直径-最长链-构造题 (Educational Codeforces Round 55 (Rated for Div. 2))

D. Maximum Diameter Graph time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Graph constructive problems are back! This time the graph you are asked to build should match the following proper

Educational Codeforces Round 55:B. Vova and Trophies

B. Vova and Trophies 题目链接:https://codeforc.es/contest/1082/problem/B 题意: 给出一个"GS"串,有一次交换两个字母的机会,问最大的连续"G"串是多少. 题解: 在末尾后面放一个哨兵"S",然后扫两遍,维护S左边和右边连续的"G"分别有多少个,然后求最大就可以了. 注意并不是所有的串都可以通过交换使长度变大这种情况,比如 "SGGGGS",