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)的更多相关文章

  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. node中的http内置模块

    Node.js开发的目的就是为了用JavaScript编写Web服务器程序.因为JavaScript实际上已经统治了浏览器端的脚本,其优势就是有世界上数量最多的前端开发人员.如果已经掌握了JavaSc ...

  2. ie8中使用ajax总是进入error解决办法

    试过很多种方法有的说是因为要把cache:false,但是本人遇到的情况可能不同最终结局的办法是 引用的是<script src="js/jquery-1.4.2.min.js&quo ...

  3. 使用io/ioutil进行读写文件

    读文件: package main import ( "fmt" "io/ioutil" ) func main() { b, err := ioutil.Re ...

  4. 预处理、const、static与sizeof-C++中const有什么作用(至少说出3个)

    1:作用如下: (1)const用于定义常量:const定义的常量编译器可以对其进行数据静态类型安全检查. (2)const修饰函数形式的参数:当输入参数为用户自定义类型和抽象数据类型时,应该将“值传 ...

  5. OUC_Summer Training_ DIV2_#2之解题策略 715

    这是第一天的CF,是的,我拖到了现在.恩忽视掉这个细节,其实这一篇只有一道题,因为这次一共做了3道题,只对了一道就是这一道,还有一道理解了的就是第一篇博客丑数那道,还有一道因为英语实在太拙计理解错了题 ...

  6. QString介绍

    QString stores a string of 16-bit QChars, where each QChar corresponds one Unicode 4.0 character. 一. ...

  7. mapReduce的优化-combiner

    mr的合成器,本质上就是reduce,在map端执行,称之为map端reduce,或者预聚合. 例子: job.setCombinerClass(WordCountCombiner.class);

  8. golang中mysql建立连接超时时间timeout 测试

    本文测试连接mysql的超时时间. 这里的"连接"是建立连接的意思. 连接mysql的超时时间是通过参数timeout设置的. 1.建立连接超时测试 下面例子中,设置连接超时时间为 ...

  9. 信息学竞赛一本通提高版AC题解—例题1.1活动安排

    书中代码有误.书中为sort(a+1,a+n+1,Cmp). // // Created by yuxi on 19-1-13. // /* * * <信息学竞赛一本通-提高版>全部AC解 ...

  10. 深度学习之强化学习Q-Learning

    1.知识点 """ 1.强化学习:学习系统没有像很多其他形式的机器学习方法一样被告知应该做什么行为, 必须在尝试之后才能发现哪些行为会导致奖励的最大化,当前的行为可能不仅 ...