Codeforces Round #820 (Div. 3) A~F泛做

一套题学到不少东西

A.Two Elevators

模拟

#include <bits/stdc++.h>

using namespace std;

#define endl '\n'
#define cerr(x) std::cerr << (#x) << " is " << (x) << '\n'
#define IOS std::ios::sync_with_stdio(false);std::cin.tie(nullptr);
#define PII pair<int, int>
#define pdd pair<double,double>
#define PLL pair<LL,LL>

#define LL long long

const double CLOCKS_PER_SECOND = ((clock_t) 1000);
const double CLOCKS_PER_MILLISECOND = ((clock_t) 1);
const int N = 2e5 + 10, M = 1e8, mod = 1e9 + 7, inf = 0x3f3f3f3f;
const double eps = 1e-6;
//#define x first
//#define y second

int T;

void solve() {
    int a, b, c;
    cin >> a >> b >> c;
    int timea = 0, timeb = 0;
    timea = a - 1;
    timeb = abs(c - b) + c - 1;
    if (timea < timeb) cout << 1 << endl;
    else if (timea > timeb) cout << 2 << endl;
    else cout << 3 << endl;
    return;
}

int main() {
    IOS;
    cin >> T;
    while (T--) {
        solve();
    }
    return 0;
}

B.Decode String

把类似150230的数字单独处理,也是模拟题

#include <bits/stdc++.h>

using namespace std;

#define endl '\n'
#define cerr(x) std::cerr << (#x) << " is " << (x) << '\n'
#define IOS std::ios::sync_with_stdio(false);std::cin.tie(nullptr);
#define PII pair<int, int>
#define pdd pair<double,double>
#define PLL pair<LL,LL>

#define LL long long

const double CLOCKS_PER_SECOND = ((clock_t) 1000);
const double CLOCKS_PER_MILLISECOND = ((clock_t) 1);
const int N = 50 + 5, M = 1e8, mod = 1e9 + 7, inf = 0x3f3f3f3f;
const double eps = 1e-6;
//#define x first
//#define y second

int T;
char ch[30] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
               'u', 'v', 'w', 'x', 'y', 'z'};
int n, b[N];
char a[N];

bool cmp(PII a, PII b) {
    return a.first < b.first;
}

void solve() {
    cin >> n;
    string s;
    map<int, bool> m;
    vector<PII > v;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        b[i] = a[i] - '0';
    }
    for (int i = 1; i <= n; i++) {
        if (b[i] == 0) {
            if (b[i + 1] == 0 && i + 1 <= n) continue;
            v.push_back({i - 2, b[i - 2] * 10 + b[i - 1]});
            m[i - 1] = m[i - 2] = m[i] = 1;
        }
    }

    for (int i = 1; i <= n; i++)
        if (!m[i]) v.push_back({i, b[i]});
    sort(v.begin(), v.end(), cmp);
    for (int i = 0; i < v.size(); i++)
        cout << ch[v[i].second - 1];
    cout << endl;
}

int main() {
    IOS;
    cin >> T;
    while (T--) {
        solve();
    }
    return 0;
}

C.Jumping on Tiles

首先易知最短距离就是1n的距离,要求经过点最大且是最短距离,显然经过的数一定是非严格单调的。

所以经过的数即为路径上所有初始值的数。

#include <bits/stdc++.h>

using namespace std;

#define endl '\n'
#define cerr(x) std::cerr << (#x) << " is " << (x) << '\n'
#define IOS std::ios::sync_with_stdio(false);std::cin.tie(nullptr);
#define PII pair<int, int>
#define pdd pair<double,double>
#define PLL pair<LL,LL>

#define LL long long

const double CLOCKS_PER_SECOND = ((clock_t) 1000);
const double CLOCKS_PER_MILLISECOND = ((clock_t) 1);
const int N = 2e5 + 10, M = 1e8, mod = 1e9 + 7, inf = 0x3f3f3f3f;
const double eps = 1e-6;
//#define x first
//#define y second

int T;

bool cmp(PII a, PII b) {
    return a.second > b.second;
}

bool cmp2(PII a, PII b) {
    return a.second < b.second;
}

int b[N];

void solve() {
    string s;
    vector<PII > v;
    cin >> s;
    int n = s.length();
    for (int i = 0; i < n; i++)
        b[i] = s[i] - '0' + 1;
    int sum = 0, ans = abs(b[0] - b[n - 1]);
    if (b[0] > b[n - 1]) {
        for (int i = 1; i < n - 1; i++) {
            if (b[i] <= b[0] && b[i] >= b[n - 1]) v.push_back({i + 1, b[i]}), sum++;
        }
        cout << ans << " " << sum + 2 << endl;
        sort(v.begin(), v.end(), cmp);
        cout << 1 << " ";
        for (auto i: v) cout << i.first << " ";
        cout << n << endl;
    } else {
        for (int i = 1; i < n - 1; i++) {
            if (b[i] >= b[0] && b[i] <= b[n - 1]) v.push_back({i + 1, b[i]}), sum++;
        }
        cout << ans << " " << sum + 2 << endl;
        sort(v.begin(), v.end(), cmp2);
        cout << 1 << " ";
        for (auto i: v) cout << i.first << " ";
        cout << n << endl;
    }
    return;

}

int main() {
    IOS;
    cin >> T;
    while (T--) {
        solve();
    }
    return 0;
}

D.Friends and the Restaurant

ki=yixi,如果选取若干数满足yx,则ki0

注意到要求分组最大且每组不少于两人,故最优情况一定是两两分组。

排序ki,如果max{ki}+min{ki}0,那么min{ki}一定是无法去餐厅的,我们考虑舍弃。

故基本思路:two points法,每次选择kl+kr判断大小,如果kl+kr<0l++

#include <bits/stdc++.h>

using namespace std;

#define endl '\n'
#define cerr(x) std::cerr << (#x) << " is " << (x) << '\n'
#define IOS std::ios::sync_with_stdio(false);std::cin.tie(nullptr);
#define PII pair<int, int>
#define pdd pair<double,double>
#define PLL pair<LL,LL>

#define LL long long

const double CLOCKS_PER_SECOND = ((clock_t) 1000);
const double CLOCKS_PER_MILLISECOND = ((clock_t) 1);
const int N = 2e5 + 10, M = 1e8, mod = 1e9 + 7, inf = 0x3f3f3f3f;
const double eps = 1e-6;
//#define x first
//#define y second

int T;

int x[N], y[N];

bool cmp(PII a, PII b) {
    return a.second > b.second;
}

void solve() {
    int n;
    cin >> n;
    vector<int> v;
    int sum = 0;
    for (int i = 1; i <= n; i++)
        cin >> x[i];
    for (int i = 1; i <= n; i++)
        cin >> y[i];
    for (int i = 1; i <= n; i++) {
        v.push_back(y[i] - x[i]);
    }
    sort(v.begin(), v.end());
    int l = 1, r = n;
    while (l < r) {
        if (v[l - 1] + v[r - 1] >= 0) {
            sum++;
            l++, r--;
        } else l++;
    }
    cout << sum << endl;
    return;

}

int main() {
    IOS;
    cin >> T;
    while (T--) {
        solve();
    }
    return 0;
}

E. Guess the Cycle Size

第一次做交互题,还蛮神奇的。

我们发现,如果对于询问{1,i}和询问{i,1},两者的回答不一致,那我们认为我们已经找到了环的最大边数。因为两者之和等于m

如果对于当前预设m,回答超出了m,那么也认定找到了最大边数。

询问的上限是50,每次询问给出相同回答的概率是12,50次询问后仍无不同答案的概率是1250,在交互题的随机数据中属于极低概率,所以完全可以通过本题。

#include <bits/stdc++.h>

using namespace std;

#define endl '\n'
#define cerr(x) std::cerr << (#x) << " is " << (x) << '\n'
#define IOS std::ios::sync_with_stdio(false);std::cin.tie(nullptr);
#define PII pair<int, int>
#define pdd pair<double,double>
#define PLL pair<LL,LL>

#define LL long long

const double CLOCKS_PER_SECOND = ((clock_t) 1000);
const double CLOCKS_PER_MILLISECOND = ((clock_t) 1);
const int N = 2e5 + 10, M = 1e8, mod = 1e9 + 7, inf = 0x3f3f3f3f;
const double eps = 1e-6;
//#define x first
//#define y second

int T;

void solve() {
    LL x, x2;
    for (int i = 1; i <= 49; i++) {
        printf("? 1 %d\n", i + 1);
        fflush(stdout);
        scanf("%lld", &x);
        if (x == -1) {
            printf("! %d\n", i);
            fflush(stdout);
            return;
        }
        printf("? %d 1\n", i + 1);
        fflush(stdout);
        scanf("%lld", &x2);
        if (x != x2) {
            printf("! %lld\n", x + x2);
            fflush(stdout);
            return;
        }
    }

}

signed main() {
    //IOS;
    //freopen("input.txt",'r',stdin);
    //freopen("output.txt",'w',stdout);
    solve();
}

F.Kirei and the Linear Function

字符串哈希好题。

阅读理解可知v(l,r)就是构造一个p=10hash区间,v(l,r)[0,8]

注意到t=v(l,r)是定值,故(tv(L1,R1)+v(L2,R2))%9=k可以通过枚举v[0,8]来判断。

#include <bits/stdc++.h>

using namespace std;

#define endl '\n'
#define cerr(x) std::cerr << (#x) << " is " << (x) << '\n'
#define IOS std::ios::sync_with_stdio(false);std::cin.tie(nullptr);
#define PII pair<int, int>
#define pdd pair<double,double>
#define PLL pair<LL,LL>

#define LL long long

const double CLOCKS_PER_SECOND = ((clock_t) 1000);
const double CLOCKS_PER_MILLISECOND = ((clock_t) 1);
const int N = 2e5 + 10, M = 1e8, mod = 1e9 + 7, inf = 0x3f3f3f3f;
const double eps = 1e-6;
//#define x first
//#define y second

int T;
int p[N], h[N];

int calc(int l, int r) {
    return (h[r] - h[l - 1] * p[r - l + 1] % 9 + 9) % 9;
}

void solve() {
    int n, w, m;
    vector<int> v[10 + 5];
    string s;
    cin >> s;
    n = s.length();
    s = "!" + s;
    cin >> w >> m;
    p[0] = 1;
    for (int i = 1; i <= n; i++) {
        p[i] = p[i - 1] * 10 % 9;
        h[i] = (h[i - 1] * 10 + s[i] - '0') % 9;
    }
    for (int l = 1, r = l + w - 1; r <= n; l++, r++) {
        v[calc(l, r)].push_back(l);
    }
    while (m--) {
        int l, r, k;
        cin >> l >> r >> k;
        PII res = {inf, inf};
        for (int i = 0; i <= 9; i++) {
            if (v[i].empty()) continue;
            for (int j = 0; j <= 9; j++) {
                if (v[j].empty()) continue;
                int t = calc(l, r);
                if ((i * t + j) % 9 == k) {
                    if (i == j && v[i].size() >= 2) res = min(res, {v[i][0], v[i][1]});
                    if (i != j) res = min(res, {v[i][0], v[j][0]});
                }
            }
        }
        if (res.first == inf) {
            cout << -1 << " " << -1 << endl;
            continue;
        }
        cout << res.first << " " << res.second << endl;
    }
}

signed main() {
    IOS;
    cin >> T;
    while (T--)
        solve();
}
posted @   SxtoxA  阅读(17)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
12 13
点击右上角即可分享
微信分享提示