PAT 甲级真题题解(121-155)
1121 Damn Single
模拟
- // 1121 Damn Single
- #include <map>
- #include <vector>
- #include <cstdio>
- #include <iostream>
- #include <algorithm>
- using namespace std;
- map<int, int> m, vis;
- vector<int> p;
- const int N = 1e4 + ;
- int a[N];
- int main() {
- int n, x, y;
- scanf("%d", &n);
- while (n--) {
- scanf("%d %d", &x, &y);
- x++; y++;
- m[x] = y;
- m[y] = x;
- }
- scanf("%d", &n);
- for (int i = ; i <= n; i++) {
- scanf("%d", &a[i]);
- a[i]++;
- vis[a[i]] = ;
- }
- for (int i = ; i <= n; i++) {
- if (vis[ m[a[i]] ]) continue;
- p.push_back(a[i]);
- }
- sort(p.begin(), p.end());
- printf("%d\n", p.size());
- for (int i = ; i < p.size(); i++) {
- if (i != ) printf(" ");
- printf("%05d", p[i] - );
- }
- return ;
- }
1122 Hamiltonian Cycle
模拟
- // 1122 Hamiltonian Cycle
- #include <set>
- #include <cstdio>
- #include <cstring>
- #include <iostream>
- #include <algorithm>
- using namespace std;
- const int N = ;
- int MAP[N][N];
- set<int> s;
- int main() {
- memset(MAP, -, sizeof(MAP));
- int n, m, k;
- scanf("%d %d", &n, &m);
- for (int i = ; i <= m; i++) {
- int u, v;
- scanf("%d %d", &u, &v);
- MAP[u][v] = ; MAP[v][u] = ;
- }
- scanf("%d", &k);
- while (k--) {
- bool f = ;
- int u, v, root;
- scanf("%d", &m);
- scanf("%d", &root);
- s.insert(root);
- u = root;
- for (int i = ; i < m; i++) {
- scanf("%d", &v);
- s.insert(v);
- if (MAP[u][v] == -) f = ;
- u = v;
- }
- if (root != u || s.size() != n || m != n + ) f = ;
- if (!f) printf("NO\n");
- else printf("YES\n");
- s.clear();
- }
- return ;
- }
1124 Raffle for Weibo Followers
MAP标记
- // 1124 Raffle for Weibo Followers
- #include <map>
- #include <cstdio>
- #include <vector>
- #include <iostream>
- #include <algorithm>
- using namespace std;
- map<string, int> vis;
- string str;
- int main() {
- bool f = ;
- int n, m, s, cnt;
- cin >> n >> m >> s;
- cnt = m;
- for (int i = ; i < s; i++) cin >> str;
- for (int i = s; i <= n; i++) {
- cin >> str;
- if (cnt == m && !vis[str]) {
- f = ;
- cout << str << endl;
- cnt = ;
- vis[str] = ;
- }
- if (!vis[str]) cnt++;
- }
- if (!f) cout << "Keep going..." << endl;
- return ;
- }
1125 Chain the Ropes
思维
- // 1125 Chain the Ropes
- #include <cstdio>
- #include <iostream>
- #include <algorithm>
- using namespace std;
- const int N = 1e4 + ;
- int a[N];
- int main() {
- int n, ans = ;
- cin >> n;
- for (int i = ; i <= n; i++) cin >> a[i];
- sort(a + , a + + n);
- ans = a[];
- for (int i = ; i <= n; i++) {
- ans += a[i];
- ans /= ;
- }
- cout << ans << endl;
- return ;
- }
1126 Eulerian Path
给定m条边关系,判断是欧拉回路还是半欧拉回路或者不是欧拉回路。
如果一个连通图,具有0个奇度顶点为欧拉回路,具有2个奇度顶点为半欧拉,其他均不为欧拉图。
- #include <bits/stdc++.h>
- using namespace std;
- const int N = 1e5 + ;
- int cnt[N], fa[N];
- void init() {
- for (int i = ; i < N; i++) fa[i] = i;
- }
- int fi(int x) {
- return fa[x] == x ? fa[x] : fa[x] = fi(fa[x]);
- }
- void Union(int x, int y) {
- int fx = fi(x), fy = fi(y);
- if (fx != fy) {
- fa[fx] = fy;
- }
- }
- int main() {
- init();
- int n, m, ans = , s = ;
- scanf("%d %d", &n, &m);
- for (int i = ; i <= m; i++) {
- int u, v;
- scanf("%d %d", &u, &v);
- Union(u, v);
- cnt[u]++; cnt[v]++;
- }
- for (int i = ; i <= n; i++) {
- if (cnt[i] % ) ans++;
- if (fa[i] == i) s++;
- if (i != ) printf(" ");
- printf("%d", cnt[i]);
- }
- printf("\n");
- if (s == && ans <= ) {
- if (ans == ) printf("Eulerian\n");
- else if (ans == ) printf("Semi-Eulerian\n");
- else printf("Non-Eulerian\n");
- } else printf("Non-Eulerian\n");
- return ;
- }
1127 ZigZagging on a Tree
中序后序建树,层次遍历。(参考了柳神的做法,简洁多了。)
- #include <queue>
- #include <vector>
- #include <iostream>
- using namespace std;
- vector<int> in, post, result[];
- int n, tree[][], root;
- struct node {
- int index, depth;
- };
- void dfs(int &index, int inLeft, int inRight, int postLeft, int postRight) {
- if (inLeft > inRight) return;
- index = postRight;
- int i = ;
- while (in[i] != post[postRight]) i++;
- dfs(tree[index][], inLeft, i - , postLeft, postLeft + (i - inLeft) - );
- dfs(tree[index][], i + , inRight, postLeft + (i - inLeft), postRight - );
- }
- void bfs() {
- queue<node> q;
- q.push(node{root, });
- while (!q.empty()) {
- node temp = q.front();
- q.pop();
- result[temp.depth].push_back(post[temp.index]);
- if (tree[temp.index][] != )
- q.push(node{tree[temp.index][], temp.depth + });
- if (tree[temp.index][] != )
- q.push(node{tree[temp.index][], temp.depth + });
- }
- }
- int main() {
- cin >> n;
- in.resize(n + ), post.resize(n + );
- for (int i = ; i <= n; i++) cin >> in[i];
- for (int i = ; i <= n; i++) cin >> post[i];
- dfs(root, , n, , n);
- bfs();
- printf("%d", result[][]);
- for (int i = ; i < ; i++) {
- if (i % == ) {
- for (int j = ; j < result[i].size(); j++)
- printf(" %d", result[i][j]);
- } else {
- for (int j = result[i].size() - ; j >= ; j--)
- printf(" %d", result[i][j]);
- }
- }
- return ;
- }
1128 N Queens Puzzle
同行,同列,同斜判断下即可。
- // 1128 N Queens Puzzle
- #include <cstdio>
- #include <iostream>
- #include <algorithm>
- using namespace std;
- const int N = 1e5 + ;
- bool vis1[N], vis2[N];
- int main() {
- int t, n, x;
- cin >> t;
- while (t--) {
- bool ok = ;
- cin >> n;
- for (int i = ; i <= * n; i++) vis1[i] = vis2[i] = ;
- for (int i = ; i <= n; i++) {
- cin >> x;
- if (vis1[x] || vis2[x + i]) {
- ok = ;
- }
- vis1[x] = ;
- vis2[x + i] = ;
- }
- if (ok) cout << "YES" << endl;
- else cout << "NO" << endl;
- }
- return ;
- }
1129 Recommendation System
STL set应用
- // 1129 Recommendation System
- #include <set>
- #include <cstdio>
- #include <iostream>
- #include <algorithm>
- using namespace std;
- const int N = 5e4 + ;
- struct node {
- int id, time;
- friend bool operator < (node x,node y){
- if(x.time == y.time) return x.id < y.id;
- return x.time > y.time;
- }
- };
- int cnt[N];
- set<node> se;
- set<node> ::iterator it;
- int main() {
- int n, k, x;
- cin >> n >> k;
- for (int i = ; i <= n; i++) {
- cin >> x;
- if(i == ) {
- cnt[x]++;
- se.insert({x, cnt[x]});
- continue;
- }
- else {
- cout << x << ":";
- int c = ;
- for (auto y : se) {
- c++;
- if(c > k) break;
- cout << " " << y.id;
- }
- cout << endl;
- }
- if (se.find({x, cnt[x]}) != se.end()) {
- it = se.find({x, cnt[x]});
- se.erase(it);
- }
- cnt[x]++;
- se.insert({x, cnt[x]});
- }
- return ;
- }
1130 Infix Expression
给定中缀表达式二叉树,输出中缀表达式。(记得CSP也考过,当时是直接暴力A的。)
- #include <bits/stdc++.h>
- using namespace std;
- #define N 21
- int n, Root;
- int vis[N];
- struct Node {
- string s;
- int l, r;
- } node[N];
- void dfs(int root) {
- if (root == -) return ;
- if (root != Root && (node[root].l != - || node[root].r != -)) cout << "(";
- dfs(node[root].l);
- cout << node[root].s;
- dfs(node[root].r);
- if ( root != Root && (node[root].l != - || node[root].r != -)) cout<<")";
- }
- int main() {
- scanf("%d", &n);
- memset(vis, , sizeof(vis));
- for (int i = ; i <= n; i++) {
- cin >> node[i].s >> node[i].l >> node[i].r;
- vis[node[i].l] = vis[node[i].r] = ;
- }
- Root = ;
- while (vis[Root]) Root++;
- dfs(Root);
- return ;
- }
1131 Subway Map
DFS暴力找最短路。同时保证题目中的那些要求。
- #include <bits/stdc++.h>
- using namespace std;
- #define MAX 10005
- #define INF 0x3f3f3f3f
- int N, M, K;
- int S, E;
- struct Stop {
- int line;
- int id;
- } stops[MAX];
- vector<Stop> stopVec[MAX];
- int flag[MAX];
- int cntMax = INF;
- vector<Stop> stopAns, stopsVec;
- int subMin = ;
- void dfs(int x, int cnt) {
- if(x == E) {
- int sub = ;
- if(cntMax > cnt) {
- stopAns = stopsVec;
- cntMax = cnt;
- for(int i = ; i < stopsVec.size(); i++) {
- if(stopsVec[i].line != stopsVec[i-].line) {
- sub++;
- }
- }
- subMin = sub;
- }
- if(cntMax == cnt) {
- for(int i = ; i < stopsVec.size(); i++) {
- if(stopsVec[i].line != stopsVec[i-].line) {
- sub++;
- }
- }
- if(sub < subMin) {
- stopAns = stopsVec;
- subMin = sub;
- }
- }
- return;
- }
- for(int i = ; i < stopVec[x].size(); i++) {
- if(flag[stopVec[x][i].id] == ) {
- flag[stopVec[x][i].id] = ;
- stopsVec.push_back(stopVec[x][i]);
- dfs(stopVec[x][i].id, cnt+);
- stopsVec.pop_back();
- flag[stopVec[x][i].id] = ;
- }
- }
- }
- int main()
- {
- cin>>N;
- for(int i = ; i <= N; i++) {
- scanf("%d", &M);
- for(int j = ; j < M; j++) {
- scanf("%d", &stops[j].id);
- stops[j].line = i;
- if(j != ) {
- stopVec[stops[j].id].push_back(stops[j-]);
- stopVec[stops[j-].id].push_back(stops[j]);
- }
- }
- }
- cin>>K;
- for(int i = ; i < K; i++) {
- scanf("%d%d", &S, &E);
- cntMax = INF;
- dfs(S, );
- printf("%d\n", cntMax);
- int line = stopAns[].line;
- int id = S;
- for(int i = ; i < stopAns.size(); i++) {
- if(line != stopAns[i].line) {
- printf("Take Line#%d from %04d to %04d.\n", line, id, stopAns[i-].id);
- line = stopAns[i].line;
- id = stopAns[i-].id;
- }
- }
- printf("Take Line#%d from %04d to %04d.\n", line, id, E);
- }
- return ;
- }
1132 Cut Integer
注意判断拆开后是否有0。
- #include <iostream>
- using namespace std;
- int cal(string s) {
- int res = ;
- for (int i = ; i < s.size(); i++) {
- res = res * + (s[i] - '');
- }
- return res;
- }
- int main() {
- int n;
- cin >> n;
- while (n--) {
- string m;
- int a, b, c;
- cin >> m;
- c = cal(m);
- a = cal(m.substr(, m.size() / ));
- b = cal(m.substr(m.size() / , m.size() / ));
- if (a * b != && c % (a * b) == ) cout << "Yes" << endl;
- else cout << "No" << endl;
- }
- return ;
- }
1133 Splitting A Linked List
从root开始,保存小于0的,大于等于0小于等于K的和大于K的链表,最后按顺序输出结果即可。
- #include <bits/stdc++.h>
- using namespace std;
- const int INF = 0x3f3f3f3f;
- struct Node {
- int data, next;
- } node[];
- int root, a, N, K;
- vector<int> v[];
- int main() {
- scanf("%d%d%d", &root, &N, &K);
- for (int i = ; i < N; i++) {
- scanf("%d", &a);
- scanf("%d %d", &node[a].data, &node[a].next);
- }
- while (root != -) {
- if (node[root].data < ) v[].push_back(root);
- else if (node[root].data <= K) v[].push_back(root);
- else v[].push_back(root);
- root = node[root].next;
- }
- int f = ;
- for (int i = ; i < ; i++) {
- for (int j = ; j < v[i].size(); j++) {
- if (!f) printf("%05d %d",v[i][j],node[v[i][j]].data), f = ;
- else printf(" %05d\n%05d %d",v[i][j],v[i][j],node[v[i][j]].data), f = ;
- }
- }
- printf(" -1\n");
- return ;
- }
PAT 甲级真题题解(121-155)的更多相关文章
- PAT 甲级真题题解(1-62)
准备每天刷两题PAT真题.(一句话题解) 1001 A+B Format 模拟输出,注意格式 #include <cstdio> #include <cstring> #in ...
- PAT 甲级真题题解(63-120)
2019/4/3 1063 Set Similarity n个序列分别先放进集合里去重.在询问的时候,遍历A集合中每个数,判断下该数在B集合中是否存在,统计存在个数(分子),分母就是两个集合大小减去分 ...
- 1080 Graduate Admission——PAT甲级真题
1080 Graduate Admission--PAT甲级练习题 It is said that in 2013, there were about 100 graduate schools rea ...
- PAT甲题题解-1107. Social Clusters (30)-PAT甲级真题(并查集)
题意:有n个人,每个人有k个爱好,如果两个人有某个爱好相同,他们就处于同一个集合.问总共有多少个集合,以及每个集合有多少人,并按从大到小输出. 很明显,采用并查集.vis[k]标记爱好k第一次出现的人 ...
- PAT甲题题解-1111. Online Map (30)-PAT甲级真题(模板题,两次Dijkstra,同时记下最短路径)
题意:给了图,以及s和t,让你求s到t花费的最短路程.最短时间,以及输出对应的路径. 对于最短路程,如果路程一样,输出时间最少的. 对于最短时间,如果时间一样,输出节点数最少的. 如果最短路程 ...
- PAT甲级真题及训练集
正好这个"水水"的C4来了 先把甲级刷完吧.(开玩笑-2017.3.26) 这是一套"伪题解". wacao 刚才登出账号测试一下代码链接,原来是看不到..有空 ...
- PAT甲级真题 A1025 PAT Ranking
题目概述:Programming Ability Test (PAT) is organized by the College of Computer Science and Technology o ...
- 1018 Public Bike Management (30分) PAT甲级真题 dijkstra + dfs
前言: 本题是我在浏览了柳神的代码后,记下的一次半转载式笔记,不经感叹柳神的强大orz,这里给出柳神的题解地址:https://blog.csdn.net/liuchuo/article/detail ...
- PAT 甲级真题
1019. General Palindromic Number 题意:求数N在b进制下其序列是否为回文串,并输出其在b进制下的表示. 思路:模拟N在2进制下的表示求法,“除b倒取余”,之后判断是否回 ...
随机推荐
- Solution: The process cannot access the file [filename] because it is being used by another process.
http://www.brianstevenson.com/blog/solution-the-process-cannot-access-the-file-filename-because-it-i ...
- B/S上传大文件的解决方案
第一点:Java代码实现文件上传 FormFile file = manform.getFile(); String newfileName = null; String newpathname = ...
- git log/show/HEAD step(2)
git log can see all commit log #git logcommit 2737cfa37f81810072f074dcf19964be0a5eea2e (HEAD -> m ...
- jQuery系列(九):JS的事件流的概念
1.事件概念 HTML中与javascript交互是通过事件驱动来实现的,例如鼠标点击事件.页面的滚动事件onscroll等等,可以向文档或者文档中的元素添加事件侦听器来预订事件.想要知道这些事件是在 ...
- 关于 ESIM 网络的 资料 集合
1.https://blog.csdn.net/wcy23580/article/details/84990923 原理及Python keras 实现 2.https://www.kaggle.co ...
- python 系统模块 OS
os.system("系统命令") 调用系统命令 os.system("task kill /f /im 系统的进程") 关闭系统进程 os.listdir( ...
- Yet Another Division Into Teams
E. Yet Another Division Into Teams 首先要想明白一个东西,就是当一个小组达到六个人的时候,它一定可以拆分成两个更优的小组. 这个题可以用动态规划来写,用一个数组来保存 ...
- Nginx-HTTP之框架的初始化
http 框架的初始化与 nginx-rtmp 框架的初始化类似: Nginx-rtmp之配置项的管理 1. ngx_http_module_t ngx_http_module 核心模块定义了新的模块 ...
- 异常值检验实战1--风控贷款年龄变量(附python代码)
python风控评分卡建模和风控常识(博客主亲自录制视频教程) https://study.163.com/course/introduction.htm?courseId=1005214003&am ...
- hibernate关联映射之多对多
package loaderman.c_many2many; import java.util.HashSet; import java.util.Set; /** * 开发人员 * * */ pub ...