2021 Jiangsu Collegiate Programming Contest

A
签到题

#include <bits/stdc++.h>
using namespace std;
int T, n;
int main() {
    cin >> T;
    while (T -- ) {
        cin >> n;
        int sum = 0;
        vector<string> v1, v2;
        string s, str;
        for (int i = 1; i <= n; i ++ ) 
            cin >> s, v1.push_back(s);
        for (int i = 1; i <= n; i ++ )
            cin >> s, v2.push_back(s);
        if (v1.size() != v2.size()) {cout << "NO" << endl; continue;}
        for (int i = 0; i < v1.size(); i ++ ) {
            int p1 = v1[i].size() - 1;
            int p2 = v2[i].size() - 1;
            int flag = 0;
            if (i == v1.size() - 1) {
                if ((v1[i][p1] == '3' || v1[i][p1] == '4') && ((v2[i][p2] == '1' || v2[i][p2] == '2')))   sum ++;
                continue;
            }
            if (v1[i][p1] == '3' || v1[i][p1] == '4')   flag = 1;
            if (v2[i][p2] == '1' || v2[i][p2] == '2')   flag ^= 0;
            else flag ^= 1; 
            sum += flag;
        }
        if (sum == v1.size())   cout << "YES" << endl;
        else    cout << "NO" << endl;
    }
    return 0;
}

C
由于所有数的和不超过\(10^5\)
首先枚举第一个数字(\(1 \rightarrow 9\), 0的情况特判)
然后通过一个PII数组存储每次操作后每个数的剩余情况
1.如果数量最多的那个数的数量超过所有数的和的一半加一,则直接结束当前情况
2.如果数量最多的那个数的数量等于所有数的和的一半加一,则当前必须选择数量最多的那个数
3.如果数量最多的那个数的数量等于所有数的和的一半加一,则可以从小到大选择第一个满足情况的数字,如果无法选择那么同样直接结束当前情况

#include <bits/stdc++.h>
#define x first
#define y second
#define PII pair<int, int>
using namespace std;
int T, n;
int a[10];
PII p[10];
int main() {
    cin >> T;
    while (T -- ) {
        int m = 0;
        for (int i = 0; i <= 9; i ++ )  cin >> a[i],  m += a[i];
        if (m == 1 && a[0] == 1)    {cout << 0 << endl; continue;}
        vector<int> ans;
        bool ok = 0;
        function<bool(PII, PII)> cmp = [&](PII a, PII b) {
            if (a.x == b.x) return a.y < b.y;
            return a.x > b.x;
        };
        for (int i = 1; i <= 9; i ++ ) {
            if (a[i] == 0)  continue;
            int tot = m - 1;
            int last = i;
            ans.push_back(i);
            for (int j = 0; j <= 9; j ++ )
                p[j].x = a[j], p[j].y = j;
            p[i].x --;
            for (int j = 1; j <= m - 1; j ++ ) {
                sort(p, p + 10, cmp);
                if (p[0].x * 2 > tot + 1) {
                    ans.clear();
                    break;
                }
                if (p[0].x * 2 == tot + 1) {
                    int num = p[0].y;
                    if (num == last) {
                        ans.clear();
                        break;
                    }
                    else {
                        p[0].x --;
                        last = num;
                        tot --;
                        ans.push_back(num);
                        continue;
                    }
                }
                bool flag = 0;
                for (int k = 0; k <= 9; k ++ ) {
                    int pos = -1;
                    for (int l = 0; l <= 9; l ++ ) 
                        if (p[l].y == k) {pos = l; break;}
                    if (p[pos].x && k != last) {
                        p[pos].x --;
                        last = k;
                        tot --;
                        ans.push_back(k);
                        flag = 1;
                        break;
                    }
                }
                if (!flag) {
                    ans.clear();
                    break;
                }
            }
            if (ans.size()) break;
        }
        if (ans.size())
            for (auto x : ans)  cout << x;
        else    cout << -1;
        cout << endl;
    }
    return 0;
}

I
分类讨论

#include <bits/stdc++.h>
#define LL long long
using namespace std;
LL T, n, m;
int main() {
    cin >> T;
    while (T -- ) {
        cin >> m >> n;
        LL res = 1;
        for (int i = 1; i <= m; i ++ )
            res *= 2;
        if (n == 0 && m == 1)   cout << 1 << endl;
        else if (n == 1 && m == 1)  cout << 2 << endl;
        else if (n == 0)    cout << res << endl;
        else    cout << res - 1 << endl;
    }
    return 0;
}

J
简化版黑白染色

#include <bits/stdc++.h>
#define PII pair<int, int>
using namespace std;
const int N = 510;
int n, m, type, sum;
bool vis[N][N];
vector<PII> ans;
int g[N][N], color[N][N];
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
struct node {
    int x, y, color;
};
int main() {
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i ++ ) 
        for (int j = 1; j <= m; j ++ )
            cin >> g[i][j];
    function<void(int, int)> bfs = [&](int x, int y) {
        queue<node> q;
        q.push({x, y, 0});
        vis[x][y] = true;
        vector<PII> a, b;
        a.push_back({x, y});
        while (q.size()) {
            auto t = q.front();
            q.pop();
            for (int i = 0; i < 4; i ++ ) {
                int tx = t.x + dx[i];
                int ty = t.y + dy[i];
                if (tx > n || tx < 1 || ty < 1 || ty > m || vis[tx][ty] || g[tx][ty] != g[x][y])   continue;
                vis[tx][ty] = 1;
                if (t.color == 1)   a.push_back({tx, ty});
                else    b.push_back({tx, ty});
                q.push({tx, ty, !t.color});
            }
        }
        if (a.size() >= b.size())   {
            for (auto x : b)
                ans.push_back(x);
        }
        else {
            for (auto x : a)
                ans.push_back(x);
        }
    };
    for (int i = 1; i <= n; i ++ ) 
        for (int j = 1; j <= m; j ++ ) 
            if (!vis[i][j]) 
                bfs(i, j);
    type = (ans.size() == 0 ? 0 : 1);
    sum = (int)ans.size();
    printf("%d %d\n", type, sum);
    for (auto x : ans)
        printf("%d %d 1\n", x.first, x.second);
    return 0;
}

K
找规律

#include <bits/stdc++.h>
using namespace std;
int T, n;
int main() {
    cin >> T;
    while (T -- ) {
        cin >> n;
        int res = 2;
        int temp = 1, p = 1;
        while (res < n) {
            temp ++, p *= 2;
            res = res + temp * p;
        }
        if (n == 1)   cout << 0 << endl;
        else if (n == 2)    cout << 1 << endl;
        else cout << temp << endl;    
    }
    return 0;
}

剩下的题目待补

上一篇:usingcomponents 未找到


下一篇:芯科BG22学习笔记:2-如何添加GPIO输入