2024 秋季PAT认证甲级(题解A1-A4)
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;
}
B. A-2 Baggage Carousel
题目描述
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:
- Collect any one un-visited vertex v into \(S\).
- Delete all the vertices (and all the edges incident on them) that are adjacent to \(v\) from \(G\).
- 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";
}
}
}
2024 秋季PAT认证甲级(题解A1-A4)的更多相关文章
- 2019秋季PAT甲级_C++题解
2019 秋季 PAT (Advanced Level) C++题解 考试拿到了满分但受考场状态和知识水平所限可能方法不够简洁,此处保留记录,仍需多加学习.备考总结(笔记目录)在这里 7-1 Fore ...
- 2019秋季PAT甲级_备考总结
2019 秋季 PAT 甲级 备考总结 在 2019/9/8 的 PAT 甲级考试中拿到了满分,考试题目的C++题解记录在这里,此处对备考过程和考试情况做一个总结.如果我的方法能帮助到碰巧点进来的有缘 ...
- PAT甲级题解(慢慢刷中)
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- PAT(甲级)2017年秋季考试
PAT(甲级)2017年秋季考试 D题红黑树待补21/30 大佬的代码,看着想哭,这才是艺术啊 A Cut Integer 模拟题 #include<bits/stdc++.h> usin ...
- CCF计算机职业资格认证考试题解
CCF计算机职业资格认证考试题解系列文章为meelo原创,请务必以链接形式注明本文地址 CCF计算机职业资格认证考试题解 CCF计算机软件能力认证(简称CCF CSP认证)是CCF计算机职业资格认证系 ...
- 去西交大考PAT认证
这周六去了西交大去考浙大PAT认证,为什么要写这个博客呢.因为...我不是西交大的学生,找考场就花了我很多时间,各种搜都找不到PAT的考场在哪. 在此记录一下,希望有有缘人再去西交大考试,可以少走点弯 ...
- PAT(甲级)2017年春季考试
PAT(甲级)2017年春季考试 A.Raffle for Weibo Followers #include<bits/stdc++.h> using namespace std; int ...
- pat甲级题解(更新到1013)
1001. A+B Format (20) 注意负数,没别的了. 用scanf来补 前导0 和 前导的空格 很方便. #include <iostream> #include <cs ...
- PAT甲级题解分类byZlc
专题一 字符串处理 A1001 Format(20) #include<cstdio> int main () { ]; int a,b,sum; scanf ("%d %d& ...
- PAT甲级题解-1123. Is It a Complete AVL Tree (30)-AVL树+满二叉树
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6806292.html特别不喜欢那些随便转载别人的原创文章又不给 ...
随机推荐
- 一款EF Core下高性能、轻量级针对分表分库读写分离的解决方案
前言 今天大姚给大家分享一款EF Core下高性能.轻量级针对分表分库读写分离的解决方案,开源(Apache License)的EF Core拓展程序包:ShardingCore. ShardingC ...
- Unicode 和JS中的字符串
计算机内部使用二进制存储数据,只认识0和1两个数字,计算机的世界只有0和1.但我们的世界却充满着文字,如a, b, c.怎样才能让计算机显示文字,供我们使用和交流?只能先把文字转化成数字进行存储,然后 ...
- 关于Windows 10 LTSC 2019无法安装Edge的解决方案
最近新换了Windows 10 LTSC 2019系统,使用体验干净且流畅,但是在更新Edge时遇到了问题:系统内装的是9x版本的Edge浏览器,并且提示更新错误,有system level方面的问题 ...
- 基于Java:流浪动物领养信息系统设计实现(源码+lw+部署文档+讲解等)
\n文末获取源码联系 感兴趣的可以先收藏起来,大家在毕设选题,项目以及论文编写等相关问题都可以给我加好友咨询 系统介绍: 现代经济快节奏发展以及不断完善升级的信息化技术,让传统数据信息的管理升级为软件 ...
- oeasy教您玩转vim - 41 - # 各寄存器
各寄存器 回忆上节课内容 上次是复制粘贴 y就是把东西yank到寄存器里,就是复制 d就是把东西delete到寄存器里,就是剪切 yank也可以配合motion 不管是yank.delete都是把 ...
- AT_agc017_b 题解
洛谷链接&Atcoder 链接 本篇题解为此题较简单做法,请放心阅读. 题目简述 一共有 \(n\) 个格子,给定两个整数 \(A,B\) 分别位于第 \(1\) 和第 \(n\) 格,中间有 ...
- 人脸识别项目打包成exe的过程遇到的问题
我最近重新拾起了计算机视觉,借助Python的opencv还有face_recognition库写了个简单的图像识别demo,额外定制了一些内容,原本想打包成exe然后发给朋友,不过在这当中遇到了许多 ...
- 2023/4/18 SCRUM个人博客
1.我昨天的任务 初步学习dlib的安装,了解dlib的基础组件 2.遇到了什么困难 对pandas库了解不到位,需要学习其中的基础 3.我今天的任务 初步了解了pandas库,对series和dat ...
- 【MybatisPlus】 Field '主键' doesn't have a default value
使用MybatisPlus的 PoMapper执行Insert插入方法报错: 复原场景: 1.PO对象存在主键值(双主键) 2.表中数据为空 3.首次插入 这张表使用的是双主键,发现原因是因为PO设置 ...
- 【Java】树状节点结构的数据
数据库的菜单,权限表是具有多层级结构,有ID和PARENT_ID两个关键性的字段 通过PARENT_ID和ID相等构建层级结构: 然后需要在Java中构建出层级的数据结构,然后输出成JSON返回给前端 ...