1121 Damn Single

模拟

  1. // 1121 Damn Single
  2. #include <map>
  3. #include <vector>
  4. #include <cstdio>
  5. #include <iostream>
  6. #include <algorithm>
  7. using namespace std;
  8.  
  9. map<int, int> m, vis;
  10. vector<int> p;
  11. const int N = 1e4 + ;
  12. int a[N];
  13.  
  14. int main() {
  15. int n, x, y;
  16. scanf("%d", &n);
  17. while (n--) {
  18. scanf("%d %d", &x, &y);
  19. x++; y++;
  20. m[x] = y;
  21. m[y] = x;
  22. }
  23. scanf("%d", &n);
  24. for (int i = ; i <= n; i++) {
  25. scanf("%d", &a[i]);
  26. a[i]++;
  27. vis[a[i]] = ;
  28. }
  29. for (int i = ; i <= n; i++) {
  30. if (vis[ m[a[i]] ]) continue;
  31. p.push_back(a[i]);
  32. }
  33. sort(p.begin(), p.end());
  34. printf("%d\n", p.size());
  35. for (int i = ; i < p.size(); i++) {
  36. if (i != ) printf(" ");
  37. printf("%05d", p[i] - );
  38. }
  39. return ;
  40. }

1122 Hamiltonian Cycle

模拟

  1. // 1122 Hamiltonian Cycle
  2. #include <set>
  3. #include <cstdio>
  4. #include <cstring>
  5. #include <iostream>
  6. #include <algorithm>
  7. using namespace std;
  8.  
  9. const int N = ;
  10. int MAP[N][N];
  11. set<int> s;
  12.  
  13. int main() {
  14. memset(MAP, -, sizeof(MAP));
  15. int n, m, k;
  16. scanf("%d %d", &n, &m);
  17. for (int i = ; i <= m; i++) {
  18. int u, v;
  19. scanf("%d %d", &u, &v);
  20. MAP[u][v] = ; MAP[v][u] = ;
  21. }
  22. scanf("%d", &k);
  23. while (k--) {
  24. bool f = ;
  25. int u, v, root;
  26. scanf("%d", &m);
  27. scanf("%d", &root);
  28. s.insert(root);
  29. u = root;
  30. for (int i = ; i < m; i++) {
  31. scanf("%d", &v);
  32. s.insert(v);
  33. if (MAP[u][v] == -) f = ;
  34. u = v;
  35. }
  36. if (root != u || s.size() != n || m != n + ) f = ;
  37. if (!f) printf("NO\n");
  38. else printf("YES\n");
  39. s.clear();
  40. }
  41. return ;
  42. }

1124 Raffle for Weibo Followers

MAP标记

  1. // 1124 Raffle for Weibo Followers
  2. #include <map>
  3. #include <cstdio>
  4. #include <vector>
  5. #include <iostream>
  6. #include <algorithm>
  7. using namespace std;
  8.  
  9. map<string, int> vis;
  10. string str;
  11.  
  12. int main() {
  13. bool f = ;
  14. int n, m, s, cnt;
  15. cin >> n >> m >> s;
  16. cnt = m;
  17. for (int i = ; i < s; i++) cin >> str;
  18. for (int i = s; i <= n; i++) {
  19. cin >> str;
  20. if (cnt == m && !vis[str]) {
  21. f = ;
  22. cout << str << endl;
  23. cnt = ;
  24. vis[str] = ;
  25. }
  26. if (!vis[str]) cnt++;
  27. }
  28. if (!f) cout << "Keep going..." << endl;
  29. return ;
  30. }

1125 Chain the Ropes

思维

  1. // 1125 Chain the Ropes
  2. #include <cstdio>
  3. #include <iostream>
  4. #include <algorithm>
  5. using namespace std;
  6.  
  7. const int N = 1e4 + ;
  8. int a[N];
  9.  
  10. int main() {
  11. int n, ans = ;
  12. cin >> n;
  13. for (int i = ; i <= n; i++) cin >> a[i];
  14. sort(a + , a + + n);
  15. ans = a[];
  16. for (int i = ; i <= n; i++) {
  17. ans += a[i];
  18. ans /= ;
  19. }
  20. cout << ans << endl;
  21. return ;
  22. }

1126 Eulerian Path 

给定m条边关系,判断是欧拉回路还是半欧拉回路或者不是欧拉回路。

如果一个连通图,具有0个奇度顶点为欧拉回路,具有2个奇度顶点为半欧拉,其他均不为欧拉图。

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int N = 1e5 + ;
  5. int cnt[N], fa[N];
  6.  
  7. void init() {
  8. for (int i = ; i < N; i++) fa[i] = i;
  9. }
  10.  
  11. int fi(int x) {
  12. return fa[x] == x ? fa[x] : fa[x] = fi(fa[x]);
  13. }
  14.  
  15. void Union(int x, int y) {
  16. int fx = fi(x), fy = fi(y);
  17. if (fx != fy) {
  18. fa[fx] = fy;
  19. }
  20. }
  21.  
  22. int main() {
  23. init();
  24. int n, m, ans = , s = ;
  25. scanf("%d %d", &n, &m);
  26. for (int i = ; i <= m; i++) {
  27. int u, v;
  28. scanf("%d %d", &u, &v);
  29. Union(u, v);
  30. cnt[u]++; cnt[v]++;
  31. }
  32. for (int i = ; i <= n; i++) {
  33. if (cnt[i] % ) ans++;
  34. if (fa[i] == i) s++;
  35. if (i != ) printf(" ");
  36. printf("%d", cnt[i]);
  37. }
  38. printf("\n");
  39. if (s == && ans <= ) {
  40. if (ans == ) printf("Eulerian\n");
  41. else if (ans == ) printf("Semi-Eulerian\n");
  42. else printf("Non-Eulerian\n");
  43. } else printf("Non-Eulerian\n");
  44. return ;
  45. }

1127 ZigZagging on a Tree

中序后序建树,层次遍历。(参考了柳神的做法,简洁多了。)

  1. #include <queue>
  2. #include <vector>
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. vector<int> in, post, result[];
  7. int n, tree[][], root;
  8.  
  9. struct node {
  10. int index, depth;
  11. };
  12.  
  13. void dfs(int &index, int inLeft, int inRight, int postLeft, int postRight) {
  14. if (inLeft > inRight) return;
  15. index = postRight;
  16. int i = ;
  17. while (in[i] != post[postRight]) i++;
  18. dfs(tree[index][], inLeft, i - , postLeft, postLeft + (i - inLeft) - );
  19. dfs(tree[index][], i + , inRight, postLeft + (i - inLeft), postRight - );
  20. }
  21.  
  22. void bfs() {
  23. queue<node> q;
  24. q.push(node{root, });
  25. while (!q.empty()) {
  26. node temp = q.front();
  27. q.pop();
  28. result[temp.depth].push_back(post[temp.index]);
  29. if (tree[temp.index][] != )
  30. q.push(node{tree[temp.index][], temp.depth + });
  31. if (tree[temp.index][] != )
  32. q.push(node{tree[temp.index][], temp.depth + });
  33. }
  34. }
  35.  
  36. int main() {
  37. cin >> n;
  38. in.resize(n + ), post.resize(n + );
  39. for (int i = ; i <= n; i++) cin >> in[i];
  40. for (int i = ; i <= n; i++) cin >> post[i];
  41. dfs(root, , n, , n);
  42. bfs();
  43. printf("%d", result[][]);
  44. for (int i = ; i < ; i++) {
  45. if (i % == ) {
  46. for (int j = ; j < result[i].size(); j++)
  47. printf(" %d", result[i][j]);
  48. } else {
  49. for (int j = result[i].size() - ; j >= ; j--)
  50. printf(" %d", result[i][j]);
  51. }
  52. }
  53. return ;
  54. }

1128 N Queens Puzzle

同行,同列,同斜判断下即可。

  1. // 1128 N Queens Puzzle
  2. #include <cstdio>
  3. #include <iostream>
  4. #include <algorithm>
  5. using namespace std;
  6.  
  7. const int N = 1e5 + ;
  8. bool vis1[N], vis2[N];
  9.  
  10. int main() {
  11. int t, n, x;
  12. cin >> t;
  13. while (t--) {
  14. bool ok = ;
  15. cin >> n;
  16. for (int i = ; i <= * n; i++) vis1[i] = vis2[i] = ;
  17. for (int i = ; i <= n; i++) {
  18. cin >> x;
  19. if (vis1[x] || vis2[x + i]) {
  20. ok = ;
  21. }
  22. vis1[x] = ;
  23. vis2[x + i] = ;
  24. }
  25. if (ok) cout << "YES" << endl;
  26. else cout << "NO" << endl;
  27. }
  28. return ;
  29. }

1129 Recommendation System 

STL set应用

  1. // 1129 Recommendation System
  2. #include <set>
  3. #include <cstdio>
  4. #include <iostream>
  5. #include <algorithm>
  6. using namespace std;
  7.  
  8. const int N = 5e4 + ;
  9. struct node {
  10. int id, time;
  11. friend bool operator < (node x,node y){
  12. if(x.time == y.time) return x.id < y.id;
  13. return x.time > y.time;
  14. }
  15. };
  16.  
  17. int cnt[N];
  18. set<node> se;
  19. set<node> ::iterator it;
  20.  
  21. int main() {
  22. int n, k, x;
  23. cin >> n >> k;
  24. for (int i = ; i <= n; i++) {
  25. cin >> x;
  26. if(i == ) {
  27. cnt[x]++;
  28. se.insert({x, cnt[x]});
  29. continue;
  30. }
  31. else {
  32. cout << x << ":";
  33. int c = ;
  34. for (auto y : se) {
  35. c++;
  36. if(c > k) break;
  37. cout << " " << y.id;
  38. }
  39. cout << endl;
  40. }
  41. if (se.find({x, cnt[x]}) != se.end()) {
  42. it = se.find({x, cnt[x]});
  43. se.erase(it);
  44. }
  45. cnt[x]++;
  46. se.insert({x, cnt[x]});
  47. }
  48. return ;
  49. }

1130 Infix Expression

给定中缀表达式二叉树,输出中缀表达式。(记得CSP也考过,当时是直接暴力A的。)

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define N 21
  5. int n, Root;
  6. int vis[N];
  7.  
  8. struct Node {
  9. string s;
  10. int l, r;
  11. } node[N];
  12.  
  13. void dfs(int root) {
  14. if (root == -) return ;
  15. if (root != Root && (node[root].l != - || node[root].r != -)) cout << "(";
  16. dfs(node[root].l);
  17. cout << node[root].s;
  18. dfs(node[root].r);
  19. if ( root != Root && (node[root].l != - || node[root].r != -)) cout<<")";
  20. }
  21.  
  22. int main() {
  23. scanf("%d", &n);
  24. memset(vis, , sizeof(vis));
  25. for (int i = ; i <= n; i++) {
  26. cin >> node[i].s >> node[i].l >> node[i].r;
  27. vis[node[i].l] = vis[node[i].r] = ;
  28. }
  29. Root = ;
  30. while (vis[Root]) Root++;
  31. dfs(Root);
  32. return ;
  33. }

1131 Subway Map

DFS暴力找最短路。同时保证题目中的那些要求。

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define MAX 10005
  5. #define INF 0x3f3f3f3f
  6.  
  7. int N, M, K;
  8. int S, E;
  9.  
  10. struct Stop {
  11. int line;
  12. int id;
  13. } stops[MAX];
  14.  
  15. vector<Stop> stopVec[MAX];
  16. int flag[MAX];
  17. int cntMax = INF;
  18. vector<Stop> stopAns, stopsVec;
  19. int subMin = ;
  20.  
  21. void dfs(int x, int cnt) {
  22. if(x == E) {
  23. int sub = ;
  24. if(cntMax > cnt) {
  25. stopAns = stopsVec;
  26. cntMax = cnt;
  27. for(int i = ; i < stopsVec.size(); i++) {
  28. if(stopsVec[i].line != stopsVec[i-].line) {
  29. sub++;
  30. }
  31. }
  32. subMin = sub;
  33. }
  34. if(cntMax == cnt) {
  35. for(int i = ; i < stopsVec.size(); i++) {
  36. if(stopsVec[i].line != stopsVec[i-].line) {
  37. sub++;
  38. }
  39. }
  40. if(sub < subMin) {
  41. stopAns = stopsVec;
  42. subMin = sub;
  43. }
  44. }
  45. return;
  46. }
  47. for(int i = ; i < stopVec[x].size(); i++) {
  48. if(flag[stopVec[x][i].id] == ) {
  49. flag[stopVec[x][i].id] = ;
  50. stopsVec.push_back(stopVec[x][i]);
  51. dfs(stopVec[x][i].id, cnt+);
  52. stopsVec.pop_back();
  53. flag[stopVec[x][i].id] = ;
  54. }
  55. }
  56. }
  57.  
  58. int main()
  59. {
  60. cin>>N;
  61. for(int i = ; i <= N; i++) {
  62. scanf("%d", &M);
  63. for(int j = ; j < M; j++) {
  64. scanf("%d", &stops[j].id);
  65. stops[j].line = i;
  66. if(j != ) {
  67. stopVec[stops[j].id].push_back(stops[j-]);
  68. stopVec[stops[j-].id].push_back(stops[j]);
  69. }
  70. }
  71. }
  72. cin>>K;
  73. for(int i = ; i < K; i++) {
  74. scanf("%d%d", &S, &E);
  75. cntMax = INF;
  76. dfs(S, );
  77. printf("%d\n", cntMax);
  78. int line = stopAns[].line;
  79. int id = S;
  80. for(int i = ; i < stopAns.size(); i++) {
  81. if(line != stopAns[i].line) {
  82. printf("Take Line#%d from %04d to %04d.\n", line, id, stopAns[i-].id);
  83. line = stopAns[i].line;
  84. id = stopAns[i-].id;
  85. }
  86. }
  87. printf("Take Line#%d from %04d to %04d.\n", line, id, E);
  88. }
  89. return ;
  90. }

1132 Cut Integer  

注意判断拆开后是否有0。

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int cal(string s) {
  5. int res = ;
  6. for (int i = ; i < s.size(); i++) {
  7. res = res * + (s[i] - '');
  8. }
  9. return res;
  10. }
  11.  
  12. int main() {
  13. int n;
  14. cin >> n;
  15. while (n--) {
  16. string m;
  17. int a, b, c;
  18. cin >> m;
  19. c = cal(m);
  20. a = cal(m.substr(, m.size() / ));
  21. b = cal(m.substr(m.size() / , m.size() / ));
  22. if (a * b != && c % (a * b) == ) cout << "Yes" << endl;
  23. else cout << "No" << endl;
  24. }
  25. return ;
  26. }

1133 Splitting A Linked List

从root开始,保存小于0的,大于等于0小于等于K的和大于K的链表,最后按顺序输出结果即可。

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int INF = 0x3f3f3f3f;
  5.  
  6. struct Node {
  7. int data, next;
  8. } node[];
  9.  
  10. int root, a, N, K;
  11. vector<int> v[];
  12.  
  13. int main() {
  14. scanf("%d%d%d", &root, &N, &K);
  15. for (int i = ; i < N; i++) {
  16. scanf("%d", &a);
  17. scanf("%d %d", &node[a].data, &node[a].next);
  18. }
  19. while (root != -) {
  20. if (node[root].data < ) v[].push_back(root);
  21. else if (node[root].data <= K) v[].push_back(root);
  22. else v[].push_back(root);
  23. root = node[root].next;
  24. }
  25. int f = ;
  26. for (int i = ; i < ; i++) {
  27. for (int j = ; j < v[i].size(); j++) {
  28. if (!f) printf("%05d %d",v[i][j],node[v[i][j]].data), f = ;
  29. else printf(" %05d\n%05d %d",v[i][j],v[i][j],node[v[i][j]].data), f = ;
  30. }
  31. }
  32. printf(" -1\n");
  33. return ;
  34. }

PAT 甲级真题题解(121-155)的更多相关文章

  1. PAT 甲级真题题解(1-62)

    准备每天刷两题PAT真题.(一句话题解) 1001 A+B Format  模拟输出,注意格式 #include <cstdio> #include <cstring> #in ...

  2. PAT 甲级真题题解(63-120)

    2019/4/3 1063 Set Similarity n个序列分别先放进集合里去重.在询问的时候,遍历A集合中每个数,判断下该数在B集合中是否存在,统计存在个数(分子),分母就是两个集合大小减去分 ...

  3. 1080 Graduate Admission——PAT甲级真题

    1080 Graduate Admission--PAT甲级练习题 It is said that in 2013, there were about 100 graduate schools rea ...

  4. PAT甲题题解-1107. Social Clusters (30)-PAT甲级真题(并查集)

    题意:有n个人,每个人有k个爱好,如果两个人有某个爱好相同,他们就处于同一个集合.问总共有多少个集合,以及每个集合有多少人,并按从大到小输出. 很明显,采用并查集.vis[k]标记爱好k第一次出现的人 ...

  5. PAT甲题题解-1111. Online Map (30)-PAT甲级真题(模板题,两次Dijkstra,同时记下最短路径)

    题意:给了图,以及s和t,让你求s到t花费的最短路程.最短时间,以及输出对应的路径.   对于最短路程,如果路程一样,输出时间最少的. 对于最短时间,如果时间一样,输出节点数最少的.   如果最短路程 ...

  6. PAT甲级真题及训练集

    正好这个"水水"的C4来了 先把甲级刷完吧.(开玩笑-2017.3.26) 这是一套"伪题解". wacao 刚才登出账号测试一下代码链接,原来是看不到..有空 ...

  7. PAT甲级真题 A1025 PAT Ranking

    题目概述:Programming Ability Test (PAT) is organized by the College of Computer Science and Technology o ...

  8. 1018 Public Bike Management (30分) PAT甲级真题 dijkstra + dfs

    前言: 本题是我在浏览了柳神的代码后,记下的一次半转载式笔记,不经感叹柳神的强大orz,这里给出柳神的题解地址:https://blog.csdn.net/liuchuo/article/detail ...

  9. PAT 甲级真题

    1019. General Palindromic Number 题意:求数N在b进制下其序列是否为回文串,并输出其在b进制下的表示. 思路:模拟N在2进制下的表示求法,“除b倒取余”,之后判断是否回 ...

随机推荐

  1. 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 ...

  2. B/S上传大文件的解决方案

    第一点:Java代码实现文件上传 FormFile file = manform.getFile(); String newfileName = null; String newpathname =  ...

  3. git log/show/HEAD step(2)

    git log can see all commit log #git logcommit 2737cfa37f81810072f074dcf19964be0a5eea2e (HEAD -> m ...

  4. jQuery系列(九):JS的事件流的概念

    1.事件概念 HTML中与javascript交互是通过事件驱动来实现的,例如鼠标点击事件.页面的滚动事件onscroll等等,可以向文档或者文档中的元素添加事件侦听器来预订事件.想要知道这些事件是在 ...

  5. 关于 ESIM 网络的 资料 集合

    1.https://blog.csdn.net/wcy23580/article/details/84990923 原理及Python keras 实现 2.https://www.kaggle.co ...

  6. python 系统模块 OS

    os.system("系统命令")  调用系统命令 os.system("task kill /f /im 系统的进程") 关闭系统进程 os.listdir( ...

  7. Yet Another Division Into Teams

    E. Yet Another Division Into Teams 首先要想明白一个东西,就是当一个小组达到六个人的时候,它一定可以拆分成两个更优的小组. 这个题可以用动态规划来写,用一个数组来保存 ...

  8. Nginx-HTTP之框架的初始化

    http 框架的初始化与 nginx-rtmp 框架的初始化类似: Nginx-rtmp之配置项的管理 1. ngx_http_module_t ngx_http_module 核心模块定义了新的模块 ...

  9. 异常值检验实战1--风控贷款年龄变量(附python代码)

    python风控评分卡建模和风控常识(博客主亲自录制视频教程) https://study.163.com/course/introduction.htm?courseId=1005214003&am ...

  10. hibernate关联映射之多对多

    package loaderman.c_many2many; import java.util.HashSet; import java.util.Set; /** * 开发人员 * * */ pub ...