2025.2.9——1400

2025.2.9——1400


A 1400

B 1400

C 1400

D 1400

E 1400

------------------------------------------------

  • 二进制/贪心+博弈/结论+结论/贪心/栈+二分+字符串


A

  1. 入手:分别考虑每一位。
  2. 关键:贪心。
  3. 巧妙:交换。

B

  1. 关键:换一种方式考虑每一个选与不选对答案的影响,从而发现结论。

C

  1. 入手:n==2 时模拟发现。
  2. 关键:贪心匹配的方式,排序后对于每一个 r 匹配最近且未配对的 l
  3. 巧妙:用栈去优化配对方式。

D

  1. 入手:二分很明显。
  2. 关键:维护一个区间而不是一个点。
  3. 巧妙:一种代码适用于两种情况。

E

  1. 入手:模拟一下。每次操作使子串中的最后一个字母去掉。
  2. 关键:必要次数将单调不增的子串操作至单调不减。次数为原始长度-前缀相同的个数。

------------------------代码------------------------

A

#include <bits/stdc++.h>
#define int long long //
#define endl '\n'     // attention: interactive/debug
#define el cout << endl
using namespace std;
#define bug(BUG) cout << "bug:# " << (BUG) << endl
#define bug2(BUG1, BUG2) cout << "bug:# " << (BUG1) << " " << (BUG2) << endl
#define bug3(BUG1, BUG2, BUG3) cout << "bug:# " << (BUG1) << ' ' << (BUG2) << ' ' << (BUG3) << endl
#define bugv(VEC, ST)                         \
    {                                         \
        for (int I = ST; I < VEC.size(); I++) \
            cout << VEC[I] << ' ';            \
        el;                                   \
    }

void _();
signed main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cout << fixed << setprecision(10);
    int T = 1;
    cin >> T;
    while (T--)
        _();
    return 0;
}

void _()
{
    int a, b, r;
    cin >> a >> b >> r;

    if (a < b)
        swap(a, b);

    bool fir = 1;
    int res = 0;
    for (int i = 60; i >= 0; i--)
    {
        int bit_a = a >> i & 1;
        int bit_b = b >> i & 1;
        if (bit_a - bit_b)
        {
            if (fir)
            {
                fir = 0;
                res += 1ll << i;
            }
            else
            {
                if (bit_b)
                    res -= 1ll << i;
                else if (r - (1ll << i) >= 0)
                    res -= 1ll << i, r -= 1ll << i;
                else
                    res += 1ll << i;
            }
        }
    }
    cout << res;
    el;
}

B

#include <bits/stdc++.h>
#define int long long //
#define endl '\n'     // attention: interactive/debug
#define el cout << endl
using namespace std;
#define bug(BUG) cout << "bug:# " << (BUG) << endl
#define bug2(BUG1, BUG2) cout << "bug:# " << (BUG1) << " " << (BUG2) << endl
#define bug3(BUG1, BUG2, BUG3) cout << "bug:# " << (BUG1) << ' ' << (BUG2) << ' ' << (BUG3) << endl
#define bugv(VEC, ST)                         \
    {                                         \
        for (int I = ST; I < VEC.size(); I++) \
            cout << VEC[I] << ' ';            \
        el;                                   \
    }

void _();
signed main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cout << fixed << setprecision(10);
    int T = 1;
    cin >> T;
    while (T--)
        _();
    return 0;
}

void _()
{
    int n;
    cin >> n;
    struct Node
    {
        int a, b;
    };
    vector<Node> a(n);
    for (auto &[a, b] : a)
        cin >> a;
    for (auto &[a, b] : a)
        cin >> b;
    sort(a.begin(), a.end(), [](Node &e1, Node &e2)
         { return e1.a + e1.b > e2.a + e2.b; });
    int res = 0;
    int f = 1;
    for (auto [x, y] : a)
    {
        if (f)
            res += x - 1;
        else
            res -= y - 1;
        f ^= 1;
    }
    cout << res;
    el;
}

C

#include <bits/stdc++.h>
#define int long long //
#define endl '\n'     // attention: interactive/debug
#define el cout << endl
using namespace std;
#define bug(BUG) cout << "bug:# " << (BUG) << endl
#define bug2(BUG1, BUG2) cout << "bug:# " << (BUG1) << " " << (BUG2) << endl
#define bug3(BUG1, BUG2, BUG3) cout << "bug:# " << (BUG1) << ' ' << (BUG2) << ' ' << (BUG3) << endl
#define bugv(VEC, ST)                         \
    {                                         \
        for (int I = ST; I < VEC.size(); I++) \
            cout << VEC[I] << ' ';            \
        el;                                   \
    }

void _();
signed main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cout << fixed << setprecision(10);
    int T = 1;
    cin >> T;
    while (T--)
        _();
    return 0;
}

void _()
{
    int n;
    cin >> n;
    vector<pair<int, int>> a(n << 1);
    vector<int> w(n);
    int cnt = n;
    for (auto &[x, y] : a)
    {
        cin >> x;
        y = cnt > 0 ? 1 : -1;
        cnt--;
    }
    for (auto &x : w)
        cin >> x;
    sort(w.begin(), w.end());
    sort(a.begin(), a.end());
    vector<int> stk, len;
    for (auto [x, y] : a)
    {
        if (y == 1)
            stk.push_back(x);
        else
        {
            len.push_back(x - stk.back());
            stk.pop_back();
        }
    }
    sort(len.rbegin(), len.rend());
    int res = 0;
    for (int i = 0; i < n; i++)
        res += len[i] * w[i];
    cout << res;
    el;
}

D

#include <bits/stdc++.h>
#define int long long //
#define endl '\n'     // attention: interactive/debug
#define el cout << endl
using namespace std;
#define bug(BUG) cout << "bug:# " << (BUG) << endl
#define bug2(BUG1, BUG2) cout << "bug:# " << (BUG1) << " " << (BUG2) << endl
#define bug3(BUG1, BUG2, BUG3) cout << "bug:# " << (BUG1) << ' ' << (BUG2) << ' ' << (BUG3) << endl
#define bugv(VEC, ST)                         \
    {                                         \
        for (int I = ST; I < VEC.size(); I++) \
            cout << VEC[I] << ' ';            \
        el;                                   \
    }

void _();
signed main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cout << fixed << setprecision(10);
    int T = 1;
    cin >> T;
    while (T--)
        _();
    return 0;
}

void _()
{
    int n;
    cin >> n;
    vector<pair<int, int>> a(n + 1);
    for (int i = 1; i <= n; i++)
        cin >> a[i].first >> a[i].second;

    auto ok = [&](int x)
    {
        int l = 0, r = 0;
        for (int i = 1; i <= n; i++)
        {
            l = max(a[i].first, l - x); // 找到一种方式适用于两种情况
            r = min(a[i].second, r + x);
            if (l > r)
                return 0;
        }
        return 1;
    };
    int l = -1, r = 1e12;
    while (r - l - 1)
    {
        int mid = l + r >> 1;
        if (ok(mid))
            r = mid;
        else
            l = mid;
    }
    cout << r;
    el;
}

E

#include <bits/stdc++.h>
#define int long long //
#define endl '\n'     // attention: interactive/debug
#define el cout << endl
using namespace std;
#define bug(BUG) cout << "bug:# " << (BUG) << endl
#define bug2(BUG1, BUG2) cout << "bug:# " << (BUG1) << " " << (BUG2) << endl
#define bug3(BUG1, BUG2, BUG3) cout << "bug:# " << (BUG1) << ' ' << (BUG2) << ' ' << (BUG3) << endl
#define bugv(VEC)               \
    {                           \
        for (auto Vec : VEC)    \
            cout << Vec << ' '; \
        el;                     \
    }

void _();
signed main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cout << fixed << setprecision(10);
    int T = 1;
    cin >> T;
    while (T--)
        _();
    return 0;
}

void _()
{
    int n;
    cin >> n;
    string s;
    cin >> s;
    string t = s;
    sort(t.begin(), t.end());
    if (t == s)
    {
        cout << 0;
        el;
        return;
    }
    char op = s[n - 1];
    vector<int> id{n - 1};
    for (int i = n - 2; i >= 0; i--)
        if (s[i] >= s[id.back()])
            id.push_back(i);
    int res = id.size() - 1;
    for (int i = id.size() - 2; i >= 0; i--)
        if (s[id[i]] == s[id[id.size() - 1]])
            res--;
        else
            break;
    for (int l = 0, r = id.size() - 1; l < r; l++, r--)
        swap(s[id[l]], s[id[r]]);
    // bugv(id);
    // bug(s);
    t = s;
    sort(t.begin(), t.end());
    if (t != s)
        res = -1;
    cout << res;
    el;
}

posted @   Jkke  阅读(3)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
点击右上角即可分享
微信分享提示