PAT (Advanced Level) 1144~1147:1145Hash二次探查 1146拓扑排序 1147堆
1144 The Missing Number(20 分)
题意:给定N个数的序列,输出不在序列中的最小的正整数。
分析:
1、给定的N个数可能为正,可能为负,可能重复。
2、由于N≤105,所以,当N个数互不重复,且都为正的情况下,所输出的数最大,为105+1。
3、将序列中的数标注后,枚举1~105+1,遇到的第一个未标注的数即为答案。
4、注意标注序列中的数时,大于105+1的数没必要标注(因为给定的数在int范围内),否则会下标越界。
- #include<cstdio>
- #include<cstring>
- #include<cstdlib>
- #include<algorithm>
- #include<iostream>
- using namespace std;
- const int MAXN = 100000 + 10;
- int a[MAXN];
- bool vis[MAXN];
- int main(){
- int N;
- while(scanf("%d", &N) == 1){
- memset(vis, false, sizeof vis);
- for(int i = 0; i < N; ++i){
- scanf("%d", &a[i]);
- if(a[i] > 0 && a[i] < MAXN) vis[a[i]] = true;
- }
- for(int i = 1; i < MAXN; ++i){
- if(!vis[i]){
- printf("%d", i);
- break;
- }
- }
- }
- return 0;
- }
1145 Hashing - Average Search Time(25 分)
题意:给定N个数,插入到长度为MSize的哈希表中,计算查找M个数的平均查找时间。
分析:
1、哈希函数:H(key)=key%MSize
2、题目中要求通过二次探查法解决冲突,且只考虑正增量。
(1)二次探查法的探查序列为Hi=(H(key)+i)%MSize,i取值依次为1*1,-1*1,2*2,-2*2,3*3,-3*3,...,(MSize-1)*(MSize-1),-(MSize-1)*(MSize-1)
- #include<cstdio>
- #include<cstring>
- #include<cstdlib>
- #include<algorithm>
- #include<iostream>
- #include<map>
- #include<queue>
- #include<vector>
- #include<cmath>
- using namespace std;
- const int MAXN = 10000 + 10;
- int MSize, N, M;
- int Hash[MAXN];
- bool judge_prime(int x){
- if(x <= 1) return false;
- for(int i = 2; i <= (int)(sqrt(x + 0.5)) + 1; ++i){
- if(x % i == 0) return false;
- }
- return true;
- }
- int main(){
- while(scanf("%d%d%d", &MSize, &N, &M) == 3){
- memset(Hash, -1, sizeof Hash);
- while(!judge_prime(MSize)) ++MSize;
- int key;
- while(N--){
- scanf("%d", &key);
- int Hkey = key % MSize;
- bool ok = false;
- if(Hash[Hkey] == -1){
- Hash[Hkey] = key;
- ok = true;
- }
- else{
- for(int i = 1; i <= MSize - 1; ++i){
- int tmp = (Hkey + i * i) % MSize;
- if(Hash[tmp] == -1){
- Hash[tmp] = key;
- ok = true;
- break;
- }
- }
- }
- if(!ok) printf("%d cannot be inserted.\n", key);
- }
- int x;
- int sum = 0;
- for(int i = 0; i < M; ++i){
- scanf("%d", &x);
- int Hkey = x % MSize;
- bool ok = false;
- for(int j = 0; j < MSize; ++j){
- int tmp = (Hkey + j * j) % MSize;
- ++sum;
- if(Hash[tmp] == -1 || Hash[tmp] == x){
- ok = true;
- break;
- }
- }
- if(!ok) ++sum;
- }
- printf("%.1lf\n", sum * 1.0 / M);
- }
- return 0;
- }
1146 Topological Order(25 分)
题意:给定一个有向图,判断所给的K个选项中哪个不是该图的拓扑排序。
分析:
1、遍历所给的拓扑序,边遍历边将该点所指向的点入度-1。
2、在进行上述操作的前提下,如果遍历到的每个点入度都为0,则该序列一定是拓扑序。
- #include<cstdio>
- #include<cstring>
- #include<cstdlib>
- #include<algorithm>
- #include<iostream>
- #include<map>
- #include<queue>
- #include<vector>
- using namespace std;
- const int MAXN = 1000 + 10;
- vector<int> G[MAXN];
- int indegree[MAXN];
- int tmpindegree[MAXN];
- vector<int> ans;
- vector<int> v;
- int N, M;
- void init(){
- for(int i = 1; i <= N; ++i) G[i].clear();
- memset(indegree, 0, sizeof indegree);
- ans.clear();
- }
- bool judge(){//判断是否为拓扑序列
- int len = v.size();
- for(int i = 0; i < len; ++i){
- int cur = v[i];
- if(!tmpindegree[cur]){
- int l = G[cur].size();
- for(int j = 0; j < l; ++j){
- --tmpindegree[G[cur][j]];
- }
- }
- else return false;
- }
- return true;
- }
- int main(){
- while(scanf("%d%d", &N, &M) == 2){
- init();
- int st, ed;
- for(int i = 0; i < M; ++i){
- scanf("%d%d", &st, &ed);
- G[st].push_back(ed);
- ++indegree[ed];
- }
- int K, x;
- scanf("%d", &K);
- for(int i = 0; i < K; ++i){
- memcpy(tmpindegree, indegree, sizeof indegree);
- v.clear();
- for(int j = 0; j < N; ++j){
- scanf("%d", &x);
- v.push_back(x);
- }
- if(!judge()) ans.push_back(i);
- }
- int len = ans.size();
- for(int i = 0; i < len; ++i){
- if(i) printf(" ");
- printf("%d", ans[i]);
- }
- printf("\n");
- }
- return 0;
- }
1147 Heaps(30 分)
题意:给定一个完全二叉树的层次遍历序列,问该二叉树是否为堆结构,并进一步判断是大顶堆还是小顶堆,最后输出该二叉树的后序遍历序列。
分析:
1、由于给定的N (1<N≤1000)个点数字各不相同,且大小都在int范围内,首先对N个点进行坐标离散化。
2、利用queue边读取边判断该二叉树是否为堆结构,同时记录每个点的左右子结点。
3、进行后序遍历。
- #include<cstdio>
- #include<cstring>
- #include<cstdlib>
- #include<algorithm>
- #include<iostream>
- #include<map>
- #include<queue>
- using namespace std;
- const int MAXN = 100000 + 10;
- int lchild[MAXN], rchild[MAXN];
- map<int, int> mp1;
- map<int, int> mp2;
- queue<int> q;
- vector<int> ans;
- int root;
- int cnt;
- void init(){
- memset(lchild, 0, sizeof lchild);
- memset(rchild, 0, sizeof rchild);
- mp1.clear();
- mp2.clear();
- while(!q.empty()) q.pop();
- ans.clear();
- }
- void get_id(int x){//坐标离散化
- ++cnt;
- mp1[x] = cnt;
- mp2[cnt] = x;
- }
- int judge(int type_l, int type_r){
- if(type_l == 1 && type_r != 2){
- return 1;//Min Heap
- }
- else if(type_l == 2 && type_r != 1){
- return 2;//Max Heap
- }
- else{
- return 3;
- }
- }
- void print_postorder(int x){
- if(lchild[x]) print_postorder(lchild[x]);
- if(rchild[x]) print_postorder(rchild[x]);
- ans.push_back(mp2[x]);
- }
- int main(){
- int M, N;
- while(scanf("%d%d", &M, &N) == 2){
- while(M--){
- init();
- cnt = 0;
- int x;
- scanf("%d", &x);
- q.push(x);
- get_id(x);
- root = mp1[x];
- int type = 0;
- bool ok = true;
- while(!q.empty()){
- int top = q.front();
- q.pop();
- int type_l = 0;
- int type_r = 0;
- if(cnt < N){
- scanf("%d", &x);
- get_id(x);
- lchild[mp1[top]] = mp1[x];
- q.push(x);
- if(top < x) type_l = 1;
- else type_l = 2;
- }
- if(cnt < N){
- scanf("%d", &x);
- get_id(x);
- rchild[mp1[top]] = mp1[x];
- q.push(x);
- if(top < x) type_r = 1;
- else type_r = 2;
- }
- if(type == 0) type = judge(type_l, type_r);
- else{
- int tmptype = judge(type_l, type_r);
- if(tmptype != type || type == 3) ok = false;
- }
- if(cnt == N) break;
- }
- if(!ok) printf("Not Heap\n");
- else{
- if(type == 1) printf("Min Heap\n");
- else printf("Max Heap\n");
- }
- print_postorder(root);
- int len = ans.size();
- for(int i = 0; i < len; ++i){
- if(i) printf(" ");
- printf("%d", ans[i]);
- }
- printf("\n");
- }
- }
- return 0;
- }
PAT (Advanced Level) 1144~1147:1145Hash二次探查 1146拓扑排序 1147堆的更多相关文章
- PAT (Advanced Level) Practice 1028 List Sorting (25 分) (自定义排序)
Excel can sort records according to any column. Now you are supposed to imitate this function. Input ...
- PAT (Advanced Level) Practice(更新中)
Source: PAT (Advanced Level) Practice Reference: [1]胡凡,曾磊.算法笔记[M].机械工业出版社.2016.7 Outline: 基础数据结构: 线性 ...
- PAT (Advanced Level) Practice 1001-1005
PAT (Advanced Level) Practice 1001-1005 PAT 计算机程序设计能力考试 甲级 练习题 题库:PTA拼题A官网 背景 这是浙大背景的一个计算机考试 刷刷题练练手 ...
- PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642
PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642 题目描述: The task is really simple: ...
- PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分) 凌宸1642
PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分) 凌宸1642 题目描述: Shuffling is a procedure us ...
- PAT (Advanced Level) Practice 1041 Be Unique (20 分) 凌宸1642
PAT (Advanced Level) Practice 1041 Be Unique (20 分) 凌宸1642 题目描述: Being unique is so important to peo ...
- PAT (Advanced Level) Practice 1035 Password (20 分) 凌宸1642
PAT (Advanced Level) Practice 1035 Password (20 分) 凌宸1642 题目描述: To prepare for PAT, the judge someti ...
- PAT (Advanced Level) Practice 1031 Hello World for U (20 分) 凌宸1642
PAT (Advanced Level) Practice 1031 Hello World for U (20 分) 凌宸1642 题目描述: Given any string of N (≥5) ...
- PAT (Advanced Level) Practice 1027 Colors in Mars (20 分) 凌宸1642
PAT (Advanced Level) Practice 1027 Colors in Mars (20 分) 凌宸1642 题目描述: People in Mars represent the c ...
随机推荐
- 【NGINX】LINUX安装NGINX
安装依赖() · yum install gcc · yum install pcre-devel · yum install zlib zlib-devel · yum install openss ...
- Python实现图片识别加翻译【高薪必学】
Python使用百度AI接口实现图片识别加翻译 另外很多人在学习Python的过程中,往往因为没有好的教程或者没人指导从而导致自己容易放弃,为此我建了个Python交流.裙 :一久武其而而流一思(数字 ...
- 笔记-爬虫部署及运行工具-scrapydweb
笔记-爬虫部署及运行工具-scrapydweb 1. 简介 scrapyd是爬虫部署工具,但它的ui比较简单,使用不是很方便. scrapydweb以scrapyd为基础,增加了ui界面和监 ...
- mongodb插入性能
转自 https://blog.csdn.net/asdfsadfasdfsa/article/details/60872180 MongoDB与MySQL的插入.查询性能测试 7.1 平均 ...
- nginx访问目录是没加/的重定向控制
static 模块提供了root与alias功能:发现目标是目录时:但URI末尾未加/时:会返回301重定向:重定向后会加/ 指令 Syntax: server_name_in_redirect on ...
- [蓝桥杯2017初赛]跳蚱蜢 BFS
题目描述 如图所示: 有9只盘子,排成1个圆圈.其中8只盘子内装着8只蚱蜢,有一个是空盘. 我们把这些蚱蜢顺时针编号为 1~8.每只蚱蜢都可以跳到相邻的空盘中,也可以再用点力,越过一个相邻的蚱蜢跳到空 ...
- day20-Python运维开发基础(装饰器 / 类中的方法 / 类的方法变属性)
1. 装饰器 / 类中的方法 / 类的方法变属性 # ### 装饰器 """ 定义:装饰器用于拓展原来函数功能的一种语法,返回新函数替换旧函数 优点:在不更改原函数代码的 ...
- android 简单列表对话框(AlertDialog.Builder().setItems())
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 ...
- Linux centosVMware Nginx访问日志、Nginx日志切割、静态文件不记录日志和过期时间
一.Nginx访问日志 vim /usr/local/nginx/conf/nginx.conf //搜索log_format 日至格式 改为davery格式 $remote_addr 客户端IP ...
- Go语言的流程控制(条件,选择,控制,跳转,闭包)
1.条件语句: 跟C和python又不同了Go的if -else是这样的 if a<5{ return 0 } else { reutrn 1 } 1.条件不需要用括号括起来 2.左边的花括号必 ...