1. 程式人生 > 其它 >AcWing第24場周賽題解

AcWing第24場周賽題解

A. 4070. 異或

題目連結:https://www.acwing.com/problem/content/4073/

題目大意:略。

解題思路:簡單模擬。

示例程式:

#include <bits/stdc++.h>
using namespace std;

int n, a[11], res;

int main() {
    cin >> n;
    for (int i = 0; i < n; i++)
        cin >> a[i];
    res = a[n-1];
    sort(a, a+n);
    res ^= a[n-1];
    cout << res << endl;
    return 0;
}

B. 4071. 國際象棋

題目連結:https://www.acwing.com/problem/content/4074/

題目大意:略。

解題思路:簡單模擬,找不會攻擊到的位置即可。

示例程式:

#include <bits/stdc++.h>
using namespace std;

char s1[3], s2[3];
int x1, Y1, x2, y2, cnt;

bool f1(int x, int y) {
    return x == x1 || y == Y1;
}

bool f2(int x, int y, int x2, int y2) {
    if (x == x2 && y == y2) return true;
    int t1 = abs(x - x2), t2 = abs(y - y2);
    return t1==1 && t2==2 || t1==2 && t2==1;
}

int main() {
    cin >> s1 >> s2;
    x1 = s1[0] - 'a' + 1;
    Y1 = s1[1] - '0';
    x2 = s2[0] - 'a' + 1;
    y2 = s2[1] - '0';
    for (int i = 1; i <= 8; i++)
        for (int j = 1; j <= 8; j++)
            if (!f1(i, j) && !f2(i, j, x1, Y1) && !f2(i, j, x2, y2))
                cnt++;
    cout << cnt << endl;
    return 0;
}

C. 4072. 習題冊

題目連結:https://www.acwing.com/problem/content/4075/

題目大意:略。

解題思路:模擬,貪心。(主要這場比賽我是在公交車上面做的,過了一個禮拜再回來看,只記得還算簡單+用了優先佇列,其他大家自己看程式碼吧)

示例程式:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;

struct Book {
    int p, id;
    bool operator < (const Book& b) const {
        return p > b.p;
    }
} a[maxn];
bool vis[maxn];
priority_queue<Book> que[4];
int n, m, c;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cin >> n;
    for (int i = 0; i < n; i++) {
        a[i].id = i;
        cin >> a[i].p;
    }
    for (int i = 0; i < n; i++) {
        cin >> c;
        que[c].push({a[i].p, i});
    }
    for (int i = 0; i < n; i++) {
        cin >> c;
        que[c].push({a[i].p, i});
    }
    cin >> m;
    while (m--) {
        cin >> c;
        while (!que[c].empty() && vis[que[c].top().id]) que[c].pop();
        if (!que[c].empty()) {
            Book u = que[c].top();
            que[c].pop();
            vis[u.id] = true;
            cout << u.p << " ";
        } else cout << -1 << " ";
    }
    return 0;
}