Codeforces Round #741 (Div. 2) 个人题解 A~D

比赛链接:Here

1562A. The Miracle and the Sleeper

题意:

  • 给出 \(l,r\) 求出最大化的 \(a\ mod\ b\) (\(l\le b\le b\le a\le r\))

思路:
很容易就看出 \(l\le ⌊\frac r2⌋ + 1\) 时 \(r\bmod \left(\left\lfloor\frac{r}{2}\right\rfloor+1\right)=\left\lfloor\frac{r-1}{2}\right\rfloor\)​​ 就是最大可能的答案,

但如果 \(l > ⌊\frac r2⌋ + 1\) 的话最大值应该是 \(r\bmod l=r - l\)

时间复杂度:\(\mathcal{O}(1)\)

void solve() {
    ll l, r;
    cin >> l >> r;
    if (r / 2 >= l) cout << ((r - 1) / 2) << "\n";
    else cout << r % l << "\n";
}

1562B. Scenes From a Memory

题意:

  • 给出一个字符串 \(s\; (|s|\le50)\) 请问可以从字符串中删除的最大位数是多少使得字符串数字变为非素数? 如:\(237\) 删除第二位的 \(3\) 变为 \(27\)

思路:
说实话赛时想复杂了,因为在 \(|s|\le 50\)​ 的情况下其实素数并不多(实际也只要考虑 \(3\)​ 位数的情况就ok),即使直接去模拟情况都可以。

但利用素数筛就能很快AC了

bool prime[110];
int n;
string s;
void solve() {
    for (int i = 0; i < n; i++) {
        if (s[i] == '1' || s[i] == '4' || s[i] == '6' || s[i] == '8' || s[i] == '9') {
            cout << 1 << endl;
            cout << s[i] << endl;
            return;
        }
    }
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            if (!prime[(s[i] - '0') * 10 + (s[j] - '0')]) {
                cout << 2 << endl;
                cout << s[i] << s[j] << endl;
                return;
            }
        }
    }
}
int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    for (int i = 2; i < 100; ++i) {
        prime[i] = 1;
        for (int j = 2; j * j <= i; ++j)
            if (i % j == 0)prime[i] = 0;
    }
    int _; for (cin >> _; _--;) {
        cin >> n >> s;
        solve();
    }
}

1562C. Rings

题意:

  • 给出一个 \(01\) 字符串,以及函数 \(f(s):\) 将 $01 $ 转为十进制整数,请找出两对下标 \((l_1,r_1),(l_2,r_2)\) 使得 \(f(s_{(l_1,r_1)}) = k\times f(s_{(l_2,r_2)})\) 成立

思路:
麻了,前面应是没想到怎么去构造一个这样的等式,手写 \(11,..,1\) 和 \(00,...,0\) 等特殊情况的时候想到我只要定位到第一个 \(0(pos)\) 的位置就可以了,然后以 \(pos\) 为界划分为两部分就肯定能构造 \(f(s_{(l_1,r_1)}) = k\times f(s_{(l_2,r_2)})\)

const int N = 1e5 + 10;
int main() {
    // ! 记得注释掉,麻了,输入流乱了导致后面debug半天没找到问题
    // cin.tie(nullptr)->sync_with_stdio(false);
    int _; for (cin >> _; _--;) {
        ll n; cin >> n;
        char s[N];
        scanf("%s", s + 1);
        ll cnt0 = 0, cnt1 = 0;
        for (int i = 1; i <= n; ++i) if (s[i] == '1') cnt1 += 1;
        if (cnt1 == n) {
            cout << "1 " << n - 1 << " 2 " << n << "\n";
            continue;
        }
        int p = 0;
        for (int i = 1; i <= n; ++i)
            if (s[i] == '0') {
                p = i;
                break;
            }
        if (p - 1 < (n / 2)) cout << p << " " << n << " " << p + 1 << " " << n << "\n";
        else cout << 1 << " " << p << " " << 1 << " " <<  p - 1 << "\n";
    }
}

1562D. Two Hundred Twenty One (Easy and Hard)

题意:

题意待补

思路:

把 \(+,-\) 号转为 \(+1,-1\) 再维护前缀和,可以快速判断奇偶(D1)

核心在于利用前缀和进行二分(D2)

不过有人用 map + set 也过了就很神奇(TQL

// D1
const int N = 3e5 + 10;
int a[N], sum[N];
int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    int _; for (cin >> _; _--;) {
        int n, q;
        string s;
        cin >> n >> q >> s;
        int id = 0;
        for (int i = 0; i < s.size(); ++i) {
            if (i & 1) {
                if (s[i] == '+') a[++id] = -1;
                else a[++id] = 1;
            } else {
                if (s[i] == '-') a[++id] = -1;
                else a[++id] = 1;
            }
            sum[id] = sum[id - 1] + a[id];
        }
        while (q--) {
            int l, r; cin >> l >> r;
            if (abs(sum[r] - sum[l - 1]) == 0) cout << "0\n";
            else if (abs(sum[r] - sum[l - 1]) & 1) cout << "1\n";
            else cout << "2\n";
        }
    }
}

// D2
int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    int _; for (cin >> _; _--;) {
        int n, q;
        cin >> n >> q;
        string _s;
        cin >> _s;
        vector<int> which(n);
        for (int i = 0; i < n; i++) {
            which[i] = (_s[i] == '-') != (i & 1);
        }
        vector<int> psum(n + 1, 0);
        for (int i = 0; i < n; i++) {
            psum[i + 1] = psum[i] + (which[i] ? -1 : 1);
        }
        while (q--) {
            int l, r;
            cin >> l >> r; l--; r--;
            if (psum[r + 1] == psum[l])  cout << "0\n\n";
            else {
                if ((r + 1 - l) & 1) cout << "1\n";
                else {
                    cout << "2\n";
                    cout << (r + 1) << ' ';
                    r--;
                }
                int d = min(psum[l], psum[r + 1]) + abs(psum[l] - psum[r + 1]) / 2;
                // d -> d+1
                int s = l;
                int e = r + 1;
                while (s + 1 < e) {
                    int m = (s + e) / 2;
                    if ((psum[m] <= d) == (psum[s] <= d)) s = m;
                    else e = m;
                }
                cout << s + 1 << "\n";
            }
        }
    }
}
上一篇:预备役2.22学习总结


下一篇:noi.ac #741 code