PTA甲 1168~1171题解

1168 Prime Day

按题意模拟…

s = input()

def is_prime(x: int) -> bool:
    if x <= 1: return False
    y = 2
    while y * y <= x:
        if x % y == 0:
            return False
        y += 1
    return True

ok = True
for i in range(len(s)):
    a = s[i:]
    print(a, end=" ")
    res = is_prime(int(a))
    if res:
        print("Yes")
    else:
        print("No")
        ok = False
if ok:
    print("All Prime!")

1169 The Judger

题意不好理解,每名玩家输出现存的任何两个数字的差(同时不与现存的数字重复),思路很常规,用哈希表模拟。
但是这句 If the i-th player is kicked out in the k-th round, print in a line Round k: i is out.. The rest of the numbers given by the one who is out of the game will be ignored. \text{If the i-th player is kicked out in the k-th round, print in a line Round k: i is out.. The rest of the numbers given by the one who is out of the game will be ignored.} If the i-th player is kicked out in the k-th round, print in a line Round k: i is out.. The rest of the numbers given by the one who is out of the game will be ignored.多少有点歧义,那这名出局的玩家这轮的数字算 the rest \text{the rest} the rest吗,从结果上来看,是不算的。

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

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int num1, num2, n, m; cin >> num1 >> num2 >> n >> m;

    unordered_set<int> exist;
    set<int> player;
    exist.insert(num1), exist.insert(num2);

    vector<vector<int>> v(n, vector<int>(m));
    for (int i = 0; i < n; i++) {
        player.insert(i);
        for (int j = 0; j < m; j++) {
            cin >> v[i][j];
        }
    }

    for (int j = 0; j < m; j++) {
        for (int i = 0; i < n; i++) {
            if (!player.count(i)) continue; // 已经出局
            int x = v[i][j];

            bool ok = false;
            for (int y : exist) {
                if (exist.count(y + x)) {
                    ok = true;
                    break;
                }
            }

            // 出局
            if (!ok || exist.count(x)) {
                cout << "Round #" << j + 1 << ": " << i + 1 << " is out.\n";
                player.erase(i);
                ok = false;
            }
            if (ok) exist.insert(x);
        }
    }

    if (player.empty()) {
        cout << "No winner.";
    } else {
        cout << "Winner(s):";
        for (int x : player) cout << ' ' << x + 1;
    }
    cout << '\n';

    return 0;
}

1170 Safari Park

邻接表可以非常方便的知道一个点的相邻点。

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

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

    int N, M, K; cin >> N >> M >> K;
    vector<vector<int>> graph(N + 1);

    while (M--) {
        int a, b; cin >> a >> b;
        graph[a].push_back(b);
        graph[b].push_back(a);
    }

    auto check = [&graph](const vector<int>& w) -> bool {
        for (int i = 1; i < w.size(); i++) {
            int c = w[i];
            for (int x : graph[i]) {
                if (w[x] == c) return false;
            }
        }
        return true;
    };

    cin >> M;
    while (M--) {
        unordered_set<int> st;
        vector<int> w(N + 1);
        for (int i = 1; i <= N; i++) {
            cin >> w[i];
            st.insert(w[i]);
        }
        if (st.size() > K) {
            cout << "Error: Too many species.\n";
        } else if (st.size() < K) {
            cout << "Error: Too few species.\n";
        } else {
            bool ok = check(w);
            if (ok) cout << "Yes\n";
            else cout << "No\n";
        }
    }

    return 0;
}

1171 Replacement Selection

题意很抽象,但把给的样例手玩一下就懂了。(数组开大点,否则有个样例会段错误)

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

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N, K; cin >> N >> K;

    vector<vector<int>> res(N + 10);
    priority_queue<int, vector<int>, greater<>> pqs[N + 10];

    for (int i = 0; i < K; i++) {
        int x; cin >> x;
        pqs[0].push(x);
    }

    int cur = 0;
    for (int i = K; i < N; i++) {
        int x; cin >> x;
        auto& pq = pqs[cur];
        int now = pq.top();
        pq.pop();
        res[cur].push_back(now);
        if (x > now) pq.push(x);
        else {
            pqs[cur + 1].push(x);
        }
        if (pq.empty()) ++cur;

        if (i == N - 1) {
            while (!pqs[cur].empty()) {
                res[cur].push_back(pqs[cur].top());
                pqs[cur].pop();
            }
            while (!pqs[cur + 1].empty()) {
                res[cur + 1].push_back(pqs[cur + 1].top());
                pqs[cur + 1].pop();
            }
        }
    }

    for (const auto& v : res) {
        if (v.empty()) break;
        for (int i = 0; i < v.size(); i++) {
            if (i ^ 0) cout << ' ';
            cout << v[i];
        }
        cout << '\n';
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值