日常刷题2025-2-9

日常刷题2025-2-9

小L的位运算

rating:1400

https://ac.nowcoder.com/acm/contest/95337/C

思路:贪心+小巧思

有关贪心的小思维题。 由于反置代价小于等于交换代价,那么一定不能用交换去代替反置。 首先,如果交换的代价大于了两次反置的代价,那么直接全部使用反置即可。 否则,我们将不匹配的位置分类,发现最多只有四类,ab 分别是 00, 01, 10, 11 ,任意 两个不同种类不匹配的话,我们一定可以交换其中的某一位 0 和 1 使之两两匹配,那么我 们就只看最多的一类不匹配的位置的数量有没有超过总数的一半即可。 如果超过就超过的部分反置,其余匹配,否则一定存在一种分配方式使之匹配后至多 仅剩一个。 循环枚举一下即可,复杂度 O(n) 。

代码

#include<iostream>
#include<cstring>
#include<vector>
using namespace std;
using LL = long long;

int main(){

#ifdef LOCAL
    freopen("data.in", "r", stdin);
    freopen("data.out", "w", stdout);
#endif

    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(0);

    int n; LL x, y;
    cin >> n >> x >> y;
    y = min(y, 2 * x);
    string a, b, c;
    cin >> a >> b >> c;
    int cnt[2][2]{};
    int s = 0;
    for(int i = 0; i < n; i++){
        int b1 = a[i] - '0';
        int b2 = b[i] - '0';
        int b3 = c[i] - '0';
        if ((b1 ^ b2) != b3){
            cnt[b1][b2] += 1;
            s += 1;
        }
    }
    int mx = 0;
    for(int i = 0; i < 2; i++){
        for(int j = 0; j < 2; j++){
            mx = max(mx, cnt[i][j]);
        }
    }
    int match = s / 2;
    if (mx * 2 > s) match = s - mx;
    cout << match * y + (s - 2 * match) * x << '\n';

}

小L的井字棋

rating:1000

https://ac.nowcoder.com/acm/contest/95337/E

思路:模拟

先判断根据已有的棋盘情况能否直接出两个棋子取胜。

如果不能再判断能否先出一个棋子,再一次出两个棋子取胜。一行或者一列或者一条对角线只要存在一个 O 存在那么这条线一定没有价值了,如果没有 O 存在那么就是有价值的位置,所以我们如果能找到一个 G 位置有两个以上有价值的线那么就能取胜。

代码

#include <bits/stdc++.h>

using u64 = unsigned long long;
using i64 = long long;
typedef std::pair<int, int> pii;
const int INF = 0x3f3f3f3f;
const int mod = 998244353;
const long long LINF = 1e18;

void solve(){
    std::vector<std::string> d(3);
    for (int i = 0; i < 3; i++){
        std::cin >> d[i];
    }

    int x = 0, o = 0;
    for (int i = 0; i < 3; i++){
        x = 0, o = 0;
        for (int j = 0; j < 3; j++){
            if (d[i][j] == 'O'){
                o++;
            }else if (d[i][j] == 'X'){
                x++;
            }
        }
        if (o != 0) continue;
        if (x != 0){
            std::cout << "Yes\n";
            return;
        }
    }

    for (int j = 0; j < 3; j++){
        x = 0, o = 0;
        for (int i = 0; i < 3; i++){
            if (d[i][j] == 'O') o++;
            else if (d[i][j] == 'X') x++;
        }
        if (o != 0) continue;
        if (x != 0){
            std::cout << "Yes\n";
            return;
        }
    }

    x = 0, o = 0;
    for (int i = 0; i < 3; i++){
        if (d[i][i] == 'O') o++;
        else if (d[i][i] == 'X') x++;
    }
    if (x != 0 && o == 0){
        std::cout << "Yes\n";
        return;
    }

    x = 0, o = 0;
    for (int i = 0; i < 3; i++){
        int j = 2 - i;
        if (d[i][j] == 'O') o++;
        else if (d[i][j] == 'X') x++;
    }
    if (x != 0 && o == 0){
        std::cout << "Yes\n";
        return;
    }

    std::vector<int> row(3), col(3);
    int aa = 0, bb = 0;

    for (int i = 0; i < 3; i++){
        for (int j = 0; j < 3; j++){
            if (d[i][j] == 'O') {
                row[i] = 1;
                col[j] = 1;
                if (i == j){
                    aa = 1;
                }
                if (i + j == 2) bb = 1;
            }
        }
    }

    // for (int j = 0; j < 3; j++){
    //     for (int i = 0; i < 3; i++){
    //         if (d[i][j] == 'O'){
    //             row[i] = 1;
    //             col[j] = 1;
    //             if (i == j) aa = 1;
    //             else if (i + j == 2) bb = 1;
    //         }
    //     }
    // }

    // for (int i = 0; i < 3; i++){
    //     std::cout << row[i] << ' ';
    // }
    // std::cout<< '\n';
    // for (int i = 0; i < 3; i++){
    //     std::cout << col[i] << ' ';
    // }
    // std::cout<< '\n';
    // std::cout << aa << ' ' << bb << '\n';

    for (int i = 0; i < 3; i++){
        for (int j = 0; j < 3; j++){
            if (d[i][j] == 'G'){
                int ok = 0;
                if (row[i] == 0) ok++;
                if (col[j] == 0) ok++;
                if (i == j && aa == 0) ok++;
                if (i + j == 2 && bb == 0) ok++;
                if (ok >= 2){
                    std::cout << "Yes\n";
                    return;
                }
            }
        }
    }

    std::cout << "No\n";
}

signed main(){
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::cout<<std::setiosflags(std::ios::fixed)<<std::setprecision(15);

    int t = 1, i;
    std::cin >> t; 
    for (i = 0; i < t; i++){
        solve();
    }

    return 0;
}
posted @ 2025-02-09 15:52  califeee  阅读(29)  评论(0)    收藏  举报