2023 PAT exam (Advanced level) in winter Dec.12nd

发布时间:2024年01月08日

欢迎查看复盘情况:【攀拓计算机能力测评-程序设计-2023年冬季考试 - 甲级 分享和复盘-哔哩哔哩】 https://b23.tv/RwaGeiE

  1. A-1 Fill in the Numbers
  2. A-2 PeekMax in Stack
  3. A-3 A+B with Binary Search Trees
  4. A-4 Transportation Hub

A-1 Fill in the Numbers

diagonal this word is the most import key in this question. As many people don’t know the meaning of that including me, there’re lots of wrong guessed ideas appearing. However, the question is so easy that I don’t want to say any other words about it.

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1123;



int n, m;
int a[N][N];

vector<int > res;

int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};

inline bool check()
{
    int sum = 0;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            bool flag = false;
            for (int k = 0; k < 4; k++) {
                int ii = i + dx[k];
                int jj = j + dy[k];
                if (ii < 1 || ii > n || jj < 1 || jj > n) continue;
                if (a[ii][jj] - 1 == a[i][j]) flag = true;
                if (a[ii][jj] == 1 && a[i][j] == n * n) flag = true;
            }

            if (flag) sum++;
        }
    }

//    cout << sum << '\n';

    return sum == n * n;
}

int maxd[N];

int main()
{
    cin >> n >> m;

    int maxn = 0;

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

        int sum1 = 0, sum2 = 0;
        for (int i = 1; i <= n; i++) {
            sum1 += a[i][i];
            sum2 += a[i][n - i + 1];
        }

        maxd[cas] = max(sum1, sum2);



        if (check()) {
            res.push_back(cas);
            maxn = max(maxn, maxd[cas]);
        }
    }

//    cout << maxn << '\n';

//    cout << res.size() << '\n';

    vector<int > newres;
    for (int i = 0; i < res.size(); i++) {
        if (maxd[res[i]] == maxn) newres.push_back(res[i]);
    }

    cout << newres.size() << '\n';
    for (int i = 0; i < newres.size(); i++)
        printf("%d%c", newres[i], i == newres.size() - 1 ? '\n' : ' ');


    return 0;
}

A-2 PeekMax in Stack

Easy operations in stack, but the first idea I thought in the exam is through the multiset (I don’t know if other people also have thought so.). However, if "scanf"s are not used, “Time Limit Exceeded” you will get.

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1123;

multiset<int > se;
stack<int > sk;

int main()
{
    int n; cin >> n;

    while (n--) {
        string s; cin >> s;

        if (s == "Pop") {
            if (sk.empty()) puts("ERROR");
            else {
                printf("%d\n", sk.top());
//                cout << sk.top() << '\n';
                se.erase(se.find(sk.top()));
                sk.pop();
            }

        } else if (s == "Push") {
            int x; scanf("%d", &x);
            sk.push(x);
            se.insert(x);
        }
        else {
            if (sk.empty()) puts("ERROR");
            else printf("%d\n", *(--se.end()));
//            else cout << *(--se.end()) << '\n';
        }
    }


    return 0;
}

A-3 A+B with Binary Search Trees

An easy problem, but there are many trees created I really do think!

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 212345;

set<int > t1;
map<int, bool > t2;

int n1, n2;
int a1[N], a2[N];
vector<int > tree1[N], tree2[N];
vector<int > r1, r2;

inline bool cmp1(int &a, int &b)
{
    return a1[a] < a1[b];
}

inline bool cmp2(int &a, int &b)
{
    return a2[a] < a2[b];
}

inline void preorder1(int root)
{
    r1.push_back(a1[root]);
    sort(tree1[root].begin(), tree1[root].end(), cmp1);
    for (int i = 0; i < tree1[root].size(); i++) preorder1(tree1[root][i]);
}

inline void preorder2(int root)
{
    r2.push_back(a2[root]);
    sort(tree2[root].begin(), tree2[root].end(), cmp2);
    for (int i = 0; i < tree2[root].size(); i++) preorder2(tree2[root][i]);
}

int main()
{
    scanf("%d", &n1);

    int rt1 = -1, rt2 = -1;

    for (int i = 0; i < n1; i++) {
        int k, p; scanf("%d%d", &k, &p);

        t1.insert(k);

        a1[i] = k;

        if (p == -1) {
            rt1 = i;
        } else {
            tree1[p].push_back(i);
        }
    }

    scanf("%d", &n2);

    for (int i = 0; i < n2; i++) {
        int k, p; scanf("%d%d", &k, &p);

        t2[k] = 1;

        a2[i] = k;

        if (p == -1) {
            rt2 = i;
        } else {
            tree2[p].push_back(i);
        }
    }

    int N; scanf("%d", &N);

    bool flag = false;
    vector<int > res;
    for (auto it : t1) {
        if (t2[N - it]) res.push_back(it), flag = true;
    }

    if (flag) puts("true");
    else puts("false");

    for (int i = 0; i < res.size(); i++)
        printf("%d = %d + %d\n", N, res[i], N - res[i]);

    preorder1(rt1);
    preorder2(rt2);

    for (int i = 0; i < r1.size(); i++)
        printf("%d%c", r1[i], i == r1.size() - 1 ? '\n' : ' ');

    for (int i = 0; i < r2.size(); i++)
        printf("%d%c", r2[i], i == r2.size() - 1 ? '\n' : ' ');

    return 0;
}

A-4 Transportation Hub

An easy problem too, but time limited will be considered. In this problem, I used Dijkstra algorithm which I used many times. However, guessing the meaning of the problem will also be a giant idea.

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 512;

struct Edge
{
    int v, w;
};
vector<Edge > e[N];

int n, m, k;
int sum[N][N];

struct Node
{
    int w, v;
    bool operator < (const Node &other) const {
        return w > other.w;
    }
};

priority_queue<Node > que;
bool vis[N];
int dist[N][N];

inline void dijkstra(int s)
{
    memset(vis, 0, sizeof vis);

    que.push({0, s});
    sum[s][s] = 1;
    dist[s][s] = 0;

    while (!que.empty()) {
        int u = que.top().v; que.pop();

        if (vis[u]) continue;
        vis[u] = true;

        for (int i = 0; i < e[u].size(); i++) {
            int v = e[u][i].v, w = e[u][i].w;
            if (dist[s][v] > dist[s][u] + w) {
                dist[s][v] = dist[s][u] + w;
                que.push({dist[s][v], v});
                sum[s][v] = sum[s][u];
            } else if (dist[s][v] == dist[s][u] + w) {
                sum[s][v] += sum[s][u];
            }
        }
    }
}

int main()
{
    scanf("%d%d%d", &n, &m, &k);

    while (m--) {
        int u, v, w; scanf("%d%d%d", &u, &v, &w);
        e[u].push_back({v, w}); e[v].push_back({u, w});
    }

    memset(dist, 0x3f, sizeof dist);

    for (int i = 0; i < n; i++) {
        dijkstra(i);
    }

    int T; scanf("%d", &T);

    while (T--) {
        int s, d; scanf("%d%d", &s, &d);

        vector<int > res;

        for (int i = 0; i < n; i++) {
            if (i == s || i == d) continue;
            if (dist[s][i] + dist[i][d] != dist[s][d]) continue;
            int suml = sum[s][i];
            int sumr = sum[i][d];
            if ((ll)suml * sumr >= k) {
                res.push_back(i);
            }
        }

        if (!res.size()) puts("None");
        else {
            for (int i = 0; i < res.size(); i++)
                printf("%d%c", res[i], i == res.size() - 1 ? '\n' : ' ');
        }
    }

    return 0;
}

文章来源:https://blog.csdn.net/YingShen_xyz/article/details/135319049
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。