Educational Codeforces Round 71

发布时间 2023-07-24 18:48:35作者: PHarr

A. There Are Two Types Of Burgers

#include <bits/stdc++.h>

using namespace std;

#define int long long

void solve() {
    int b, p, f, h, c;
    cin >> b >> p >> f>> h >> c;
    b /= 2;
    int res = 0;
    for (int i = 0, j; i <= min(b, p); i++) {
        j = min(b - i, f);
        res = max(res, i * h + j * c);
    }
    cout << res << "\n";
}

int32_t main() {
    ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
    int t;
    cin >> t;
    while (t--)
        solve();
    return 0;
}

B. Square Filling

#include <bits/stdc++.h>

using namespace std;

#define int long long

const int N = 55;
int a[N][N], b[N][N];

int32_t main() {
    ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
    int n, m;
    cin >> n >> m;

    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            cin >> a[i][j];

    vector<pair<int, int>> res;
    for (int i = 1; i < n; i++)
        for (int j = 1; j < m; j++){
            if( a[i][j] == 1 && a[i][j+1] == 1 && a[i+1][j] == 1 && a[i+1][j+1] == 1 )
                b[i][j] = b[i+1][j] = b[i][j+1] = b[i+1][j+1] = 1 , res.emplace_back( i , j );
        }
    for( int i = 1 ; i <= n ; i ++ )
        for( int j = 1 ; j <= m ; j ++ )
            if( a[i][j] != b[i][j] ){
                cout << "-1";
                return 0;
            }
    cout << res.size() << "\n";
    for( auto [x,y] : res )
        cout << x << " " << y << "\n";

            return 0;
}

C. Gas Pipeline

\(f[i][0/1]\)表示第\(i\)位在低或高时的花费,然后枚举前一位在高还是低即可

#include <bits/stdc++.h>

using namespace std;

#define int long long

const int inf = 1e18;

void solve() {
    int n, a, b;
    string s;
    cin >> n >> a >> b >> s;
    vector<array<int, 2>> f(n + 1, {inf, inf});
    f[0][0] = b;
    for (int i = 1; i <= n; i++) {
        if (s[i - 1] == '0') {
            f[i][0] = min({f[i][0], f[i - 1][0] + a + b, f[i - 1][1] + 2 * a + b});
            f[i][1] = min({f[i][1], f[i - 1][0] + 2 * a + 2 * b, f[i - 1][1] + a + 2 * b});
        } else {
            f[i][1] = min(f[i][1], f[i - 1][1] + a + 2 * b);
        }
    }
    cout << f[n][0] << "\n";
}

int32_t main() {
    ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
    int t;
    cin >> t;
    while (t--)
        solve();
    return 0;
}

D. Number Of Permutations

先排序,然后发现连续若干个相同的可以交换位置,因此我们只需按\(a_i,b_i,(a_i,b_i)\)三种分别排序,然后依次求出可以交换出多少情况即可。

然后用容斥原理求一下答案即可。

#include <bits/stdc++.h>

using namespace std;

#define int long long

const int mod = 998244353;

int32_t main() {
    ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
    int n;
    cin >> n;
    vector<int> fact(n + 1);
    fact[0] = 1;
    for (int i = 1; i <= n; i++)
        fact[i] = fact[i - 1] * i % mod;
    vector<pair<int, int>> a(n);

    for (auto &[x, y]: a)
        cin >> x >> y;
    sort(a.begin(), a.end());
    int res = fact[n];

    int ans = 1;
    for (int i = 1, cnt = 1; i < n; i++) {
        if (a[i].first == a[i - 1].first) cnt++;
        else cnt = 1;
        ans = ans * cnt % mod;
    }
    res = (res - ans + mod) % mod;
    ans = 1;
    for (int i = 1; i < n; i++)
        if (a[i].second < a[i - 1].second) ans = 0;
    for (int i = 1, cnt = 1; ans && i <= n; i++) {
        if (a[i] == a[i - 1]) cnt++;
        else cnt = 1;
        ans = ans * cnt % mod;
    }
    res = (res + ans) % mod;
    sort(a.begin(), a.end(), [](pair<int, int> x, pair<int, int> y) {
        return x.second < y.second;
    });

    ans = 1;
    for (int i = 1, cnt = 1; i < n; i++) {
        if (a[i].second == a[i - 1].second) cnt++;
        else cnt = 1;
        ans = ans * cnt % mod;
    }
    res = (res - ans + mod) % mod;
    cout << res << "\n";
    return 0;
}

E. XOR Guessing

先询问高七位都是\(0\),这样可以得到高七位值,在用同样的方法求低七位即可

#include <bits/stdc++.h>

using namespace std;

#define int long long

int32_t main() {
    cout << "?" << flush;
    for (int i = 1; i <= 100; i++)
        cout << " " << i;
    cout << endl;
    int x;
    cin >> x;

    cout << "?" << flush;
    for (int i = 1; i <= 100; i++ )
        cout << " " << (i<<7);
    cout << endl;
    int y;
    cin >> y;

    int t = (1<<7)-1 , res = 0;
    res |= x & ( t << 7 );
    res |= y & t;
    cout << "! " << res << endl;
    return 0;
}

F. Remainder Problem

分段暴力即可。

#include <bits/stdc++.h>

using namespace std;

#define int long long

const int N = 500005, M = 710;
int a[N] ;
int cnt[M][M];

int32_t main() {
    ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
    int q;
    cin >> q;
    for( int op , x , y ; q ; q -- ){
        cin >> op >> x >> y;
        if( op == 1 ){
            a[x] += y;
            for( int i = 1 ; i < M ; i ++ )
                cnt[i][x%i] += y;
        }else{
            if( x < M )
                cout << cnt[x][y] << "\n";
            else{
                int ans = 0;
                for( int i = y ; i < N ; i += x )
                    ans += a[i];
                cout << ans << "\n";
            }
        }
    }
    return 0;
}