Educational Codeforces Round 155 (Rated for Div. 2)A~D

文章分析了四道Codeforces竞赛题目,涉及数组操作、最优化策略、计数和排列组合等,展示了C++编程技巧在解决此类问题中的应用。
摘要由CSDN通过智能技术生成

A题,Problem - A - Codeforces

没什么好说的,看了题就会做

#include<bits/stdc++.h>

using namespace std;
const int N = 100010;
using ll = long long;

void solve() {
    int n;
    cin >> n;
    vector<int>a(n);
    vector<int>b(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        cin >> b[i];
    }

    for (int i = 1; i < n; i++) {
        if (a[i] >= a[0]) {
            if (b[i] >= b[0]) {
                cout << -1 << '\n';
                return;
            }
        }
    }

    cout << a[0] << '\n';
}

signed main() {
    std::ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t=1;
    cin >> t;
    while (t--)
        solve();
}

B题,链接:Problem - B - Codeforces

由题意可转化为,每一个放置的块都可以影响和自己同一行同一列的格子,花费就是这个格子对应的行在数组里的数字加上对应的列在数组里的数字,可以分为行花费+列花费,很容易想到,最少放置n个格子就够了,怎么放呢?只要每个格子行数或者列数不同就行,行数都不同,那么行的花费确定了,列随便怎么放都行,就找花费最小的放置就找,列数不同同理!于是根据这两种情况求最小值。

#include<bits/stdc++.h>

using namespace std;
const int N = 100010;
using ll = long long;

void solve() {
    int n; cin >> n;
    vector<int>a(n);
    vector<int>b(n);
    ll sum1 = 0;
    ll sum2 = 0;
    int min1 = 1e9;
    int min2 = 1e9;


    for (int i = 0; i < n; i++) {
        cin >> a[i];
        min1 = min(min1, a[i]);
        sum1 += a[i];
    }
    for (int i = 0; i < n; i++) {
        cin >> b[i];
        min2 = min(min2, b[i]);
        sum2 += b[i];
    }

    cout << min(sum1 +1ll* min2 * n, sum2 +1ll* min1 * n) << '\n';

}

signed main() {
    std::ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t=1;
    cin >> t;
    while (t--)
        solve();
}

Problem - C - Codeforces

把每个01串中挨着的0和1的重复个数数出来,比如有m个1挨着,就要去除m-1个1,就有m种去法,分成相同连续的若干段,最后所有被删的字符还需要排列一下,用阶乘算。

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 100010;
using ll = long long;

int mod = 998244353;

int fac[1000000];

void solve() {
    int m = 0;
    string s; cin >> s;
    int cnt = 0;
    int n = s.size();
    for (int i = 1; i < n; i++) {
        if (s[i] == s[i - 1]) {
            m = 1;
            break;
        }
    }
    if (!m || n == 1) {
        cout << 0 << ' ' << 1 << '\n';
        return;
    }
    fac[0] = 1;
    for (int i = 1; i <= n; i++) {
        fac[i] = fac[i - 1] * i % mod;
    }
    int t = 0; m = 1;
    int ans = 1;
    int tt = 0;
    for (int i = 1; i < n; i++) {
        if (s[t]==s[i]) {
            cnt++;
            m++;
        }
        else {
            t = i;
            ans = ans * m % mod;
            tt += m - 1;
            m = 1;
        }
    }
    if (m != 1) { ans = ans * m % mod; tt += m - 1; }
    ans = ans * (fac[tt] % mod) % mod;
    cout << cnt << ' ' << ans % mod << '\n';
}

signed main() {
    std::ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t=1;
    cin >> t;

    while (t--)
        solve();
}

Problem - D - Codeforces

考虑每一位的贡献。先求这一位上的前缀异或和数组,对于当前位是1的,对答案有贡献的只能之前前缀和是0的位置。对于当前位是0的也是类似,对答案有贡献的只能是之前前缀和是1的位置。显然,这种做法不会重复计算某个位置的贡献,最后把答案累加输出。我没写出来,太菜了,看的题解。

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 100010;
using ll = long long;

int mod = 998244353;

int fac[1000000];
int a[300005];

void solve() {
    int n; cin >> n;
    for (int i = 1; i <= n; i++) cin >> a[i];
    int res = 0;
    for (int i = 30; i >= 0; i--) {
        (res <<= 1) %= mod;
        int t[2] = { 1 }, m[2] = { 1 };
        int XOR = 0;
        for (int j = 1; j <= n; j++) {
            XOR ^= a[j] >> i & 1;
            m[XOR] ++;
            (res += t[!XOR]) %= mod;
            (t[0] += m[0]) %= mod;
            (t[1] += m[1]) %= mod;
        }
    }
    cout << res << '\n';
}


signed main() {
    std::ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t=1;
    //cin >> t;

    while (t--)
        solve();
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值