首页 > 其他分享 >2024 秋季PAT认证甲级(题解A1-A4)

2024 秋季PAT认证甲级(题解A1-A4)

时间:2024-09-03 23:02:52浏览次数:3  
标签:PAT luggage int 题解 number A1 行李 each line

2024 秋季PAT认证甲级(题解A-D)

写在前面

这一次PAT甲级应该是最近几次最简单的一次了,3个小时的比赛差不多30分钟就ak了(也是拿下了整场比赛的rk1),下面是题解报告,每个题目差不多都是20-30行代码,难度在洛谷普及组左右(cf 1000-1200分)

A. A-1 Happy Patting

题目描述

The "Happy Patting" game is to arrange the children into n rows, with m people in each row. When the game starts, each child will receive a digital command on his/her mobile phone simultaneously: 1 for forward, 2 for backward, 3 for left, and 4 for right. The children must follow the command to pat the children in the specified direction. How many children are patted by more than one friend?
Note: In the plane diagram, the top of a grid is "front" and the bottom is "back". Children in the corners may pat the air, which is also allowed.

输入

Each input file contains one test case. The first line gives 2 positive integers n and m (2≤n,m≤100), which are the number of rows and the number of people in each row, respectively.
Then n lines follow, each contains m numbers, which represent the digital commands received by the children at the corresponding position.
All the numbers in a line are separated by a space.

输入

For each test case, print in a line the number of children who are patted by more than one friend.

样例

in:
3 5
1 2 3 4 1
4 1 4 3 3
3 2 1 1 3
out:
4

题意简述

给一个\(n * m\)的矩阵,每个矩阵上有一个数字,代表这个格子上的孩子向哪个方移动,最后问所有格子中,有多少个格子上有超过1个孩子

思路

直接模拟,使用一个二维数组来存每个位置孩子的数量,每次输入一个数,代表当前位置的孩子会向目标位置的格子移动,让目标位置的孩子数+1即可,最后遍历二维数组,统计大于1的数的个数即可(如果越界就不记录)

AC代码

#include<bits/stdc++.h>
using namespace std;
int main()
{
    cin.tie(0) -> sync_with_stdio(0);
    int n, m;
    cin >> n >> m;
    vector<vector<int>> graph(n + 1, vector<int>(m + 1));
    for(int i = 1; i <= n; i ++)
        for(int j = 1; j <= m ; j ++)
        {
            int t;
            cin >> t;
            int x = i, y = j;
            if(t == 1) x --;
            if(t == 2) x ++;
            if(t == 3) y --;
            if(t == 4) y ++;
            if(x < 1 || x > n || y < 1 || y > m) continue;
            graph[x][y]++;
        }
    int ans = 0;
    for(int i = 1; i <= n; i ++)
        for(int j = 1; j <= m ; j ++)
            ans += graph[i][j] > 1;
    cout << ans;
    
}

题目描述

When a flight arrives, the passengers will go to the Arrivals area to pick up their luggages from a baggage carousel(行李转盘).
Now assume that we have a special airport that has only one pickup window for each baggage carousel. The passengers are asked to line up to pick up their luggage one by one at the window. The carousel can hold at most m luggages, and each luggage has a unique tag number. When a passenger's tag number is matched by a luggage on the carousel, the luggage is then handed to the passenger and the next luggage will be sent onto the carousel.
But if one arrives at the window yet finds out that one's luggage is not on the carousel, one will have to move to the end of the queue and wait for the next turn. Suppose that each turn takes 1 minute, your job is to calculate the total time taken to clear the baggage carousel.

输入

Each input file contains one test case. The first line gives 2 positive integers \(n\) (≤500) and \(m\) (≤50), which are the number of passengers and the size of the baggage carousel, respectively.
Then n distinct tag number are given in the next line, each is an 8-digit number. The tag numbers are given in the order of being sent to the carousel. It is assumed that \(min(n,m)\) luggages are already on the carousel at the beginning.
The next line gives another sequence of n distinct tag numbers, which corresponds to the passengers in the queue.
All the numbers in a line are separated by a space.

输出

For each test case, print in a line the total time taken to clear the baggage carousel.

NOTE

If the tag number of a passenger cannot be found at all, that means the passenger's luggage is lost. In this case you must output in a line TagNumber is lost! where TagNumber is the tag number, and then remove this passenger from the queue right away. Since the number of luggages is the same as the number of passengers, if one passenger's luggage is lost, there must be one luggage left and the carousel can never be cleared. In this case, output in the last line the total time taken to clear the passengers' queue.

样例

in1:
10 4
00000001 00000002 00000003 00000004 00000005 00000006 00000007 00000008 00000009 00000010
00000010 00000008 00000006 00000001 00000004 00000007 00000003 00000009 00000005 00000002
out1:
16

in2:
10 4
00000001 00000002 00000003 00000004 00000005 00000006 00000007 00000008 00000009 00000010
00000008 12345678 00000006 00000001 00000004 00000007 87654321 00000009 00000005 00000002
out2:
12345678 is lost!
87654321 is lost!
14

题意简述

有\(n\)个人以及\(n\)个行李和一个能放最多\(m\)件行李的行李转盘,人和行李有唯一的编号,人按照给定顺序进行排队,行李按照给定顺序上行李转盘,每次队列最前面的人走上行李转盘,如果行李转盘上有他的行李,那么他会拿着行李直接离开,如果没有找到他的行李,就会回到队列的末尾,重新排队,但是如果他的行李不在这\(n\)件行李之中,你应该报告他的行李丢失了,然后他会直接离开。每个人走上前查看行李转盘操作的时间记作一个单位,问所有的人都离开时需要多少时间。

思路

本题实际上要你模拟一个行李转盘(可以用set维护),和两个队列(一个行李的队列和人的队列),首先可以用map容器记录出现过的每个行李,从行李队列中取出\(min(m, n)\)件行李放入行李转盘,每次取出人队列的队头,首先查询map中是否有这个人的行李,如果没有说明这个人的行李丢失了,直接跳转到下一个人,否则用set的count函数判断当前人的行李是否在行李转盘上,如果在,则把他的行李从行李转盘上删去(使用erase函数完成),同时从行李队列中取出队头填入到行李转盘中,然后继续下一个人;如果不在行李转盘上,则把这个人重新入队,继续下一个人。直到人的队列为空,记录时间即可。

AC代码

#include<bits/stdc++.h>
// #include<Windows.h>
using namespace std;
int main()
{
    cin.tie(0) -> sync_with_stdio(0);
    int n, m;
    cin >> n >> m;
    queue<string> luggage; //行李的队列
    map<string, int> has;
    for(int i = 0; i < n; i ++)
    {
        string s;
        cin >> s;
        luggage.push(s); // 按顺序入队每个行李
        has[s] = 1; // 记录当前行李出现过
    }
    queue<string> q; // 乘客的队列
    for(int i = 0; i < n; i ++)
    {
        string s;
        cin >> s;
        q.push(s);
    }
    set<string> now; // 行李转盘
    int ans = 0;
    while(m-- && luggage.size()) // 首先取m件行李放入转盘
    {
        now.insert(luggage.front());
        luggage.pop();
    }
    while(q.size())
    {
        ans++; // 每次操作时间+1
        string t = q.front(); // 取出乘客队头
        q.pop();
        if(!has[t]) // 如果这位乘客的行李没有出现过说明丢失了
        {
            cout << t << " is lost!\n";
            continue; // 直接下一步
        }
        if(!now.count(t)) q.push(t); // 如果行李不在转盘,则重新入队
        else //在行李转盘上
        {
            now.erase(t); // 从行李转盘上删去这件行李
            if(luggage.size()) // 从行李队列中补充一件行李到转盘上
            {
                now.insert(luggage.front());
                luggage.pop();
            }
        }
    }
    // 输出答案
    cout << ans;
}

C. A-3 Finding Independent Set

题面描述

Given a simple undirected graph \(G=(V,E)\). An independent set of G is a set \(S⊆V\) such that no two members of \(S\) are connected by an edge in \(E\). Finding the maximum independent set of \(G\) is an NP-hard problem. Here you are supposed to implement a greedy hueristic to find a near-maximum independent set.
The algorithm works in the following way:

  1. Collect any one un-visited vertex v into \(S\).
  2. Delete all the vertices (and all the edges incident on them) that are adjacent to \(v\) from \(G\).
  3. Repeat steps 1 and 2 until there is no un-visited vertex left in \(G\).

In order to obtain the unique solution, when there are many options in step 1, you must always choose the vertex with the smallest index.

输入

Each input file contains one test case. For each case, the first line contains 2 positive integers: n (≤1000), the number of vertices; and m, the number of edges. Then m lines follow, each gives indices of the two ends of an edge. The vertices are indexed from 1 to n.

输出

Print in a line the indices of the vertices in the independent set obtained by the given greedy algorithm. The indices must be in increasing order, and must be separated by exactly 1 space. There must be no extra space at the beginning or the end of the line.

样例

in:
8 7
1 5
5 4
4 2
2 3
3 6
6 1
6 2
out:
1 2 7 8

题意简述

给定一张图,按顺序求出一个独立集,每次选择一个点,删除所有与这个点相邻的点,直到没有点可删除,输出这个独立集。

独立集: 图中任意两点之间没有边相连的子图,或者说,选定一个集合,使得与集合中的点相邻的点不在集合中。

题意简述2

给定一张图,求出字典序最小的独立集

思路

按顺序选择一个点,如果这个点没有被标记,就将他加入到答案序列中,并且标记他所有的相邻点,如果被标记过则直接跳过。

AC代码

#include<bits/stdc++.h>
using namespace std;
const int N = 1e3 + 10;
vector<int> edge[N];
int main()
{
    int n,m;
    cin >> n >> m;
    while(m --)
    {
        int a,b;
        cin >>a >>b;
        edge[a].push_back(b);
        edge[b].push_back(a);
    }
    vector<int> vis(n + 1), ans;
    for(int i = 1; i <= n; i ++)
    {
        if(vis[i]) continue;
        vis[i] = 1;
        ans.push_back(i);
        for(auto v : edge[i]) vis[v] = 1;
    }
    for(int i = 0; i < ans.size(); i ++)
        cout << ans[i] <<" \n"[ans.size() - 1 == i];
}

D. A-4 The Smallest Open Interval

题面描述

Given a set \(S\) of points on the \(x\)-axis. For any point \(p\), you are suppose to find the smallest open interval that contains \(p\), provided that the two ends of the interval must be in \(S\).

输入

Each input file contains one test case. Each case consists of several lines of commands, where each command is given in the format:

cmd num

where cmd is either I for "insert", or Q for "query", or E for "end"; and num is an integer coordinate of a point on the x-axis. It is guaranteed that num is in the range \([−10^9, 10^9]\).
The input is ended by E. It is guaranteed that there are no more than \(10^5\) distinct points in \(S\), and so is the number of queries. The total number of commands (E not included) is no more than \(3×10^5\).

输出

For each I case, insert num into \(S\). For each \(Q\) case, output the smallest open interval that contains num in the format \((s1, s2)\), where both \(s_1\) and \(s_2\) must be in \(S\). On the other hand, if num is no larger than the smallest point in \(S\), s1 shall be replaced by \(-inf\), representing negative infinity; or if num is no smaller than the largest point in \(S\), \(s_2\) shall be replaced by \(+inf\), representing positive infinity.
It is guaranteed that there must be at least 1 point in S before the first query.

样例

in:
I 100
Q 100
I 0
I 33
I -200
Q 25
Q 33
Q -200
Q 200
E
out:
(-inf, +inf)
(0, 33)
(0, 100)
(-inf, 0)
(100, +inf)

题意简述

给定一个数轴上的点集,每次插入一个点,或者查询一个点,输出包含这个点的最小开区间。

题意简述2

维护一个平衡搜索树,支持插入和查询前驱后继点(不存在则输出+-inf)

思路

使用直接用C++的set容器维护即可

AC代码

#include<bits/stdc++.h>
using namespace std;
int main()
{
    set<int> tr;
    string op;
    while(cin >> op, op != "E")
    {
        int p;
        cin >>p;
        if(op == "I") tr.insert(p);
        else
        {
            if(tr.size() == 0)
            {
                cout << "(-inf, +inf)\n";
                continue;
            }
            auto next = tr.upper_bound(p);
            auto pre = tr.lower_bound(p);
            --pre;
            if(*pre >= p)
                cout << "(-inf,";
            else
            {
                cout << "(" << *pre << ",";
            }
            if(next == tr.end())
                cout << " +inf)\n";
            else cout << " " << *next <<")\n";
        }
    }
}

标签:PAT,luggage,int,题解,number,A1,行李,each,line
From: https://www.cnblogs.com/orangecodelog/p/18395623

相关文章

  • 力扣-968监控二叉树(Java贪心详细题解)
    题目链接:968.监控二叉树-力扣(LeetCode)前情提要:本题是一道名副其实的hard题目,他考察二叉树和贪心的综合运用能力。所以我们不仅要会贪心还要会二叉树的一些知识,如果没有写二叉树类型的题目,建议大家该题可以放放,去刷其他的题目。因为本人最近都来刷贪心类的题目所以该......
  • AtCoder ABC 369题解
    前言本题解部分思路来源于网络,仅供参考!A-369题目大意给定\(A\),\(B\)两个整数,求有多少个整数\(x\)使得可以通过某种排列使得\(A\),\(B\),\(x\)为等差数列。解题思路稍加分析即可得到:如果\(A=B\)则结果为\(1\)。如果\(A=B\)但\((A+B)\bmod......
  • 09-03 题解
    09-03题解比赛地址补题地址这回打算改变一下方式,从写"怎么做题"变成"怎么想题"T1什么样的两个\(a_i\)能被合并到一个Bug上?很简单(不过我也想了好一会),mod2同余的两个可以合并在一起为了培养最强Bug,肯定不能往上叠负数,所以上述内容针对序列中的所有正......
  • [蓝桥杯 2018 省 A] 付账问题--贪心题解
    题目重述:[蓝桥杯2018省A]付账问题-洛谷#[蓝桥杯2018省A]付账问题##题目描述几个人一起出去吃饭是常有的事。但在结帐的时候,常常会出现一些争执。现在有$n$个人出去吃饭,他们总共消费了$S$元。其中第$i$个人带了$a_i$元。幸运的是,所有人带的钱的总数是......
  • 蓝桥杯2019省A糖果题解
     附上链接:[蓝桥杯2019省A]糖果-洛谷,有兴趣的小伙伴可以去试试哦~#[蓝桥杯2019省A]糖果##题目描述糖果店的老板一共有$M$种口味的糖果出售。为了方便描述,我们将$M$种口味编号$1$∼$M$。小明希望能品尝到所有口味的糖果。遗憾的是老板并不单独出售糖果,而......
  • 8.30 上午 becoder 模拟赛总结 & 题解
    T1密码当时想到解法了,却依然认为自己不会做,我真是个人才。结论:对于$\foralli\in[1,n)$,满足密码不是$a_i$的因数,且密码是$a_k$的因数,设满足条件的最小值为$g$则答案为$\frac{n}{g}$。一种最好想的做法:枚举$\gcd(a_k,n)$的因数作为$g$,并枚举$i\in[1,n)$,判断是......
  • 8.31 上午 becoder 模拟赛总结 & 题解
    T1四个质数的和赛场亲测搜索+小剪枝可以得到70pts。考虑$O(p(V)^2)$枚举任意两个质数的和,其中$p(V)$表示$V$以内质数的个数。然后开个数组记录下对于每种和的记录有多少种情况,查询时for循环扫一遍即可,详见代码。复杂度去掉质数筛$O(p(V)^2+tn)$,代码贴在下面(100pts)......
  • 8.31 下午 梦熊联盟 NOIP 模拟赛总结 & 题解
    T1北极星一个比较好想到的点是从后往前枚举数,计算得出它需要的操作次数,然后给所有前面的数都加上这个操作次数,这样就把每个数独立出来了。所以这道题就变成了如何快速通过这些操作得到一个指定的数。观察大样例的输出,发现每一个数都是11?1?1?的形式,其中问号为+或c,我们可......
  • 9.1 上午 becoder 模拟赛总结 & 题解
    T1货车运输Kruskal重构树模板,没什么好说的,不会的把自己重构了算了,跳过。T2Slagalica可以发现拼图1和2、3拼起来还是拼图1,拼图4和2、3拼起来也还是拼图4,这两种拼图还都不能和自己拼,所以我们可以看作只有拼图1和拼图4来做。对于边界拼图分别是5、7的情况,只有......
  • 8.31 晚上 ABC369 总结 & 题解
    打了一天的比赛。ABCD太水了,直接放代码链接得了,点字母就能看对应代码。E-SightseeingTour看范围$N$只有$400$,所以我们可以先用floyd搞出任意两点间的距离。对于每次询问,发现$K_i$只有$5$,所以可以直接深搜应该走哪座桥,和应该走到哪一端。时间复杂度$O(N3+QK_i......