![](https://img-blog.csdnimg.cn/20201014180756927.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
PTA
睡不着kkk
这个作者很懒,什么都没留下…
展开
-
PAT | T1027 Larry and Inversions
这里主要就是要注意:对于某一个排列[i … j],假设已知其逆序对数为X,将其reverse后的逆序对数为(i - j) * (i - j + 1) / 2而对于某个排列[1,…,i,…,j,…,n],假设已知其逆序对数为wholeSeq,将[i,…,j]reverse后,整个排列的逆序对数为wholeSeq - 2 * X + (i - j) * (i - j + 1) / 2;附用线段树...原创 2020-04-21 19:29:17 · 132 阅读 · 0 评论 -
PAT | T1026 String of Colorful Beads
啥玩意这是。。。最大珠子种类数和串最大长度咋都用N表示,整的我一直段错误,最后干脆直接全开最大了#include <iostream>#include <queue>using namespace std;const int maxn = 1e5+ 5;const int maxType = 1e5+ 5;int value[maxType];int s...原创 2020-04-20 23:00:11 · 105 阅读 · 0 评论 -
PAT | T1025 Keep at Most 100 Characters
动态规划dp#include <iostream>#include <string>#include <unordered_set>using namespace std;typedef long long ll;const int mod = 1000000007;ll dp[110][1010]; // dp[i][j] : s[0......原创 2020-04-20 21:36:37 · 123 阅读 · 0 评论 -
PAT | T1024 Currency Exchange Centers
总算遇到一道会做的#include <iostream>#include <string>#include <vector>#include <algorithm>#include <unordered_set>using namespace std;const int maxn = 1e4 + 5;struct E...原创 2020-04-20 20:25:40 · 404 阅读 · 1 评论 -
PAT | T1021 Safe Fruit
最后两个点超时了。。26分#include <iostream>#include <vector>#include <unordered_set>#include <climits>#include <algorithm>using namespace std;struct Fruit{ int id,price; ...原创 2020-04-19 22:07:48 · 594 阅读 · 0 评论 -
PAT | T1018 Subnumbers
这道题没想出来,借鉴了https://blog.csdn.net/richenyunqi/article/details/87891111大佬的算法#include <iostream>#include <string>using namespace std;typedef long long ll;const ll mod = 1000000007;co...原创 2020-04-19 14:11:37 · 103 阅读 · 0 评论 -
PAT | T1017 The Best Peak Shape
又是测试点3。。。34分#include <iostream>using namespace std;const int maxn = 1e4 + 5;int seq[maxn];int up[maxn]; // up[i] : 从前往后以seq[i]结尾的最长递增子序列int down[maxn];int main(){ int n; scanf("%d",...原创 2020-04-18 22:24:24 · 101 阅读 · 0 评论 -
PAT | T1016 Uniqueness of MST
测试点3答案错误,31分#include <iostream>#include <vector>#include <algorithm>#include <unordered_set>using namespace std;const int maxn = 510;typedef long long ll;struct Edge...原创 2020-04-18 21:36:16 · 442 阅读 · 0 评论 -
PAT | T1015 Letter-moving Game
越发觉得自己很菜。。。这道题参考了https://blog.csdn.net/jtjy568805874/article/details/53610545大佬的代码,自己没想出来,哭哭#include <iostream>#include <string>using namespace std;const int maxn = 1010;int dp[ma...原创 2020-04-18 20:52:49 · 132 阅读 · 0 评论 -
PAT | T1014 Circles of Friends
1013看不懂,来1014了hhh2次bfs找最深的深度(应该是两次?)#include <iostream>#include <unordered_set>#include <queue>#include <vector>using namespace std;bool m[1010][1010];bool vis[1010]...原创 2020-04-18 19:54:39 · 107 阅读 · 0 评论 -
PAT | T1012 Greedy Snake
累死了累死了,好多边界条件,一定要想清楚再写,后面修修补补,有很多地方应该都可以删掉的。好在数据量很小,直接dfs即可#include <iostream>#include <climits>using namespace std;bool map[20][20]; // true表示能走bool vis[20][20]; // 某个点有没有被访问过in...原创 2020-04-17 22:36:07 · 124 阅读 · 0 评论 -
PAT | T1011 Cut Rectangles
写了一个半小时,测试点3和4答案错误,应该是某个情况没考虑到(我觉得应该是翻转没考虑)扣了三分,还行吧,考试的时候这三分就不要了以下是AC代码(附详细注释):#include <iostream>#include <vector>#include <climits>#include <cmath>using namespace std;...原创 2020-04-17 20:36:01 · 106 阅读 · 0 评论 -
PAT | T1010 Lehmer Code
和1009一样的,直接套我1009的模板就行,嘻嘻#include <iostream>using namespace std;typedef long long ll;// ================================= Segment Tree ================================= const int maxn =...原创 2020-04-15 21:02:58 · 97 阅读 · 0 评论 -
PAT | T1009 Triple Inversions
线段树求逆序对,线段树模板可以直接用,是我在AgOH大佬那里摘下来的,加了一些注释啥的,只有少数地方需要改动。#include <iostream>using namespace std;typedef long long ll;// ================================= Segment Tree ======================...原创 2020-04-15 20:43:56 · 90 阅读 · 0 评论 -
PAT | T1008 Airline Routes
Edge数组最开始开成了Edge[maxn]导致最后一个点一直段错误。#include <iostream>#include <stack>#include <algorithm>using namespace std;const int maxn = 10010;const int maxm = 6 * maxn;struct E{ ...原创 2020-04-15 13:51:39 · 105 阅读 · 0 评论 -
PAT | T1007 Red-black Tree
去学了几天高级数据结构和算法,莫队,fhq Treap,线段树啥的,也不知道有没有用倒数第二个测试点超时了。33分 // Red-black Tree #include <iostream>using namespace std;const int maxn = 505;const int con = 1000000007;typedef long long ll;...原创 2020-04-15 12:10:39 · 193 阅读 · 0 评论 -
PAT | T1004 To Buy or Not to Buy - Hard Version(待优化)
28分,有3个测试点超时,暂时还没想出怎么优化。#include <iostream>#include <string>#include <unordered_map>#include <climits>using namespace std;struct Beads{ unordered_map<char,int> c...原创 2020-04-05 12:08:58 · 105 阅读 · 0 评论 -
PAT | T1003 Universal Travel Sites
网络流算法:关于网络流是什么#include <queue>#include <cstdio>#include <string>#include <iostream>#include <climits>#include <unordered_map>using namespace std;const int ...原创 2020-04-04 12:32:05 · 568 阅读 · 0 评论 -
PAT | T1002 Business
动态规划自己做出来了我好快乐。这道题是这样的,首先所有projects按截止日期排序,若截止日期相同则按所需时间排序,若所需时间相同则按利润排序。dp[i][j]表示:考虑0~i个project的时候,截止日期为j时,所能获得的最大利润。那么最后的答案就在dp[n - 1][maxD]中(maxD是在读入project的时候记录的最大截止日期)1.先初始化dp的第一行,即看第一个proje...原创 2020-04-03 12:35:45 · 130 阅读 · 0 评论 -
PAT | T1001 Battle Over Cities - Hard Version
Top确实难多了这道题我是将一开始就连通的路cost改成0,然后对每个陷落的城市,做一次kruskal,统计出最大的代价。一开始顶点编号写成了0~n - 1,导致很多测试点出错,下次一定要小心,不能再犯这种低级错误了!#include <iostream>#include <vector>#include <algorithm>#include &l...原创 2020-04-03 10:41:19 · 106 阅读 · 0 评论 -
PAT | A1155 Heap Paths
Ohhhhhhhh完结撒花!!!接下来就是Top了,嘻嘻#include <iostream>#include <vector>using namespace std;vector<int> seq;vector<int> trav;int DFS(int i,int n){ // return 0 : Not heap //...原创 2020-04-02 14:04:28 · 57 阅读 · 0 评论 -
PAT | A1154 Vertex Coloring
一开始开了个超大的图bool G[10010][10010]结果内存超限了。后面把它改成了vector<vector > G;发现最后两个点超时:#include <iostream>#include <vector>#include <unordered_map>using namespace std;vector<vecto...原创 2020-04-02 13:40:42 · 116 阅读 · 1 评论 -
PAT | A1153 Decode Registration Card of PAT
#include <iostream>#include <string>#include <vector>#include <unordered_map>#include <sstream>#include <algorithm>using namespace std;struct Testee{ stri...原创 2020-04-02 12:47:38 · 59 阅读 · 0 评论 -
PAT | A1152 Google Recruitment
暴力#include <iostream>#include <string>#include <cmath>#include <sstream>using namespace std;bool isPrime(int n){ if(n <= 1) return false; int sqr = (int)sqrt(1.0 *...原创 2020-04-01 11:39:34 · 102 阅读 · 0 评论 -
PAT | A1151 LCA in a Binary Tree
#include <iostream>#include <unordered_map>#include <unordered_set>using namespace std;unordered_map<int,int> posInInOrder;unordered_set<int> nodes;int preOrder[1...原创 2020-04-01 11:24:07 · 60 阅读 · 0 评论 -
PAT | A1150 Travelling Salesman Problem
#include <iostream>#include <unordered_set>#include <climits>using namespace std;int G[210][210];unordered_set<int> nodes;int main(){ int n,m; scanf("%d%d",&n,&a...原创 2020-04-01 10:58:08 · 84 阅读 · 0 评论 -
PAT | A1149 Dangerous Goods Packaging
#include <iostream>#include <vector>#include <unordered_set>#include <unordered_map>using namespace std;vector<vector<int> > G;unordered_map<int,int> ...原创 2020-04-01 10:23:49 · 80 阅读 · 0 评论 -
PAT | A1147 Heaps
#include <iostream>#include <vector>using namespace std;vector<int> postOrder;int keys[1010];int m,n;int isHeap(){ int heap = -1; int i = 0; // 从0开始遍历 while(2 * i + 1 <...原创 2020-03-31 11:55:48 · 69 阅读 · 0 评论 -
PAT | A1146 Topological Order
#include <iostream>#include <vector>#include <unordered_set>using namespace std;bool G[1010][1010];int topo[1010];unordered_set<int> vis;vector<int> res;int ma...原创 2020-03-31 11:21:12 · 52 阅读 · 0 评论 -
PAT | A1145 Hashing - Average Search Time
while(!isPrime(MSize))写成了if(!isPrime(MSize))找了我一个小时。。#include <iostream>#include <math.h>#include <algorithm>using namespace std;int hashTable[10010];bool isPrime(int...原创 2020-03-31 10:54:01 · 71 阅读 · 0 评论 -
PAT | A1144 The Missing Number
#include <iostream>#include <unordered_set>#include <algorithm>using namespace std;unordered_set<int> numbers;int main(){ int n; scanf("%d",&n); for(int i = 0;i ...原创 2020-03-30 12:17:21 · 53 阅读 · 0 评论 -
PAT | A1143 Lowest Common Ancestor
先建立一个BST树后再找LCA会导致最后三个测试点超时可以直接遍历先序序列:由于先序序列永远先访问子树根节点,所以第一个找到的u<root<v或u>root>v一定是u和v的LCA;若先找到u或先找到v,则先找到的那个一定是另一个的祖先。#include <iostream>#include <unordered_set>#include ...原创 2020-03-30 12:03:28 · 114 阅读 · 0 评论 -
PAT | A1142 Maximal Clique
#include <iostream>#include <vector>#include <unordered_set>using namespace std;bool G[210][210];vector<int> clique;unordered_set<int> C;int isClique(int n){ /...原创 2020-03-30 11:07:05 · 70 阅读 · 0 评论 -
PAT | A1141 PAT Ranking of Institutions
这道题如果在每一次读取的时候更新分数就把它强制转换成int,测试点5会答案错误#include <iostream>#include <string>#include <unordered_map>#include <vector>#include <algorithm>using namespace std;struc...原创 2020-03-30 10:25:56 · 89 阅读 · 0 评论 -
PAT | A1140 Look-and-say Sequence
#include <iostream>#include <string>using namespace std;int main(){ string str; int n; cin>>str; scanf("%d",&n); for(int i = 1;i < n;i++){ int len = str.length();...原创 2020-03-29 13:20:10 · 59 阅读 · 0 评论 -
PAT | A1139 First Contact(待优化)
一开始的思路:从源点开始进行深度不超过3的DFS,深度到达3时判断是否到达终点,到达则记录当前路径。想得太复杂了,且最后有三个点答案错误,也没有考虑正负零的差别。一开始的代码:#include <iostream>#include <unordered_set>#include <vector>#include <stack>#inc...原创 2020-03-29 13:06:16 · 173 阅读 · 0 评论 -
PAT | A1138 Postorder Traversal
#include <iostream>using namespace std;int preOrder[50010];int inOrder[50010];int findFirst(int preL,int preR,int inL,int inR){ int root = preOrder[preL]; if(preL == preR) return root...原创 2020-03-29 10:55:55 · 53 阅读 · 0 评论 -
PAT | A1137 Final Grading
#include <iostream>#include <string>#include <vector>#include <unordered_map>#include <algorithm>using namespace std;struct Student{ string name; int online; i...原创 2020-03-29 10:31:56 · 100 阅读 · 0 评论 -
PAT | A1136 A Delayed Palindrome
#include <iostream>#include <string>#include <algorithm>using namespace std;string process(string num,string reverseNum){ int len = num.length(); int c = 0; printf("%s + %s ...原创 2020-03-28 13:51:47 · 101 阅读 · 0 评论 -
PAT | A1135 Is It A Red-Black Tree
一开始是用动态链表做的,怎么都过不了,第二次看了网上柳婼大神的代码,用指针做过了。附柳婼大神链接动态链表代码:#include <iostream>#include <vector>#include <stack>#include <algorithm>using namespace std;struct Node{ int d...原创 2020-03-28 12:54:35 · 102 阅读 · 0 评论