PAT甲级_2021秋(97)

总结

第7次模考,7-1英语问题没读懂,导致没有AK。

第一题

33min

题意

用链表存int型数组,给出链表长度n和询问次数q。然后n行给出每个结点的初始地址和结点长度。
然后给出找q个目标元素下标,要求输出目标地址,如果超出范围则输出illegal access。
最后询问总共声明的多少块数据。

小结

注意题意:

  • at the very beginning,第一块已经读入了,所以cnt最小值是1,即使所有查询都失败
  • 如果目标超出范围,则都不声明直接跳过。

题解

#include<bits/stdc++.h>
#define int long long 
using namespace std;
int n, k, idx, cnt = 1;
struct array{
	long long addr, len;
}rec[10001];
bool st[10001];
signed main(){
	cin>>n>>k;
	rec[0].len = 0;
	for(int i = 1; i <= n; i ++){
		scanf("%d %d", &rec[i].addr, &rec[i].len);
		rec[i].len += rec[i - 1].len;
	}
	for(int i = 0; i < k; i ++){
		scanf("%d", &idx);
		if(idx >= rec[n].len || idx < 0) puts("Illegal Access");
		else{
			int j = 1;
			while(rec[j].len <= idx) j ++;
			st[j] = true;
			cnt = max(cnt, j);
			cout<<rec[j].addr + 4 * (idx - rec[j - 1].len)<<endl;
		}
	}
	cout<<cnt;
	return 0;
}

第二题

11min

题意

给出帽子和人的数量n。依次输入帽子的大小、人的重量。帽子大的对应人的重量就大。
然后输入帽子的大小是按栈的顺序给的。就是所有帽子落在一起,顶部的帽子排在数组后面,底部的帽子排在数组前面。
要求按拿帽子次序输出对应人员的下标。

思路

  • 输入栈深n,依次用h数组记录帽子大小,m数组记录人员重量,并用mtn数组记录重量对应的下标。
  • 存一份备份到h1、m1,然后对h1、m1排序。这样就知道了一一对应关系,h1下标i的帽子对应m1下标i的人。
  • 用mp建立h1和m1对应重量、大小的映射关系。
  • 再循环n次,从栈顶n-1到栈底0。通过mtn[mp[h[j]]]得到目标输出。h[j]是第j顶帽子,mp[h[j]]是第j顶帽子对应

小结

每个哈希表单列出key : value的意思,写清楚能使得逻辑更清晰。

题解

#include<bits/stdc++.h>
using namespace std;
int mtn[1000100], h[10001], m[10001], h1[10001], m1[10001], mp[1000100];
int main(){
	int n; cin>>n;
	for(int i = 0; i < n; i ++) scanf("%d", &h[i]);
	for(int i = 0; i < n; i ++){
		scanf("%d", &m[i]);
		mtn[m[i]] = i;
	}
	memcpy(h1, h, sizeof h1);
	memcpy(m1, m, sizeof m1);
	sort(h1, h1 + n);
	sort(m1, m1 + n);
	for(int i = 0; i < n; i ++)
		mp[h1[i]] = m1[i];
	for(int j = n - 1; j >= 0; j --){
		cout<<mtn[mp[h[j]]] + 1;
		if(j) printf(" ");
	}
	return 0;
}

第三题

10min

题意

给出结点数n和边数m。然后给出m条边信息(v1、v2),v1到v2是无向边。
从一个结点出发,只会走当前没到过的,编号最小的结点。不回溯不重复走。问从哪个结点出发能走最远,最远距离是多少。

思路

板子题。

  • int g[101][101]记录图。bool st[101]记录已经走过的点。
  • 从第一个点到最后一个点依次DFS即可。

题解

#include<bits/stdc++.h>
using namespace std;
int n, m, a, b, max_n, cnt[101];
bool g[101][101], st[101];
void DFS(int s, int num){
	max_n = max(max_n, num);
	for(int i = 1; i <= n; i ++){
		if(!st[i] && g[s][i]){
			st[i] = true;
			DFS(i, num + 1);
			st[i] = false;
			break;
		}
	}
}
int main(){
	cin>>n>>m;
	while(m--){
		cin>>a>>b;
		g[a][b] = g[b][a] = true;
	}
	for(int i = 1; i <= n; i ++){
		max_n = 1;
		st[i] = true;
		DFS(i, 1);
		st[i] = false;
		cnt[i] = max_n;
	}
	int ptr = 1;
	for(int i = 1; i <= n; i ++)
		if(cnt[i] > cnt[ptr]) ptr = i;
	cout<<ptr<<' '<<cnt[ptr];
}

第四题

44min

题意

给定结点数量n,然后依次给出这n个节点的信息(key : priority)。
这棵树根据key形成二叉排序树,根据priority形成小根堆。
要求输出层序遍历得到的key和priority数组。

思路

  • 先对n个节点进行排序,排好后就是中序序列,因为二叉排序树的中序遍历得到的序列是顺序的。
  • 然后造树,序列中最小的priority作为根,其左侧为左子树,右侧为右子树。
  • 然后层序遍历造好的树即可。

小结

  • 初始想法是用priority排序,然后找左右子树,想了很长时间。这条路显然不对的,因为根好求,但是左右子树很难分开界限。
  • 一般造树就是用中序+其他。

题解

#include<bits/stdc++.h>
using namespace std;
int n;
typedef pair<int, int> pii;
pii rec[101];
struct node{
	int key, pri;
	struct node*l, *r;
};
node* mt(int l, int r){
	if(l > r) return NULL;
	node * t = (node*)malloc(sizeof(node));
	int ptr = l;
	for(int i = l; i <= r; i ++)
		if(rec[i].second < rec[ptr].second) ptr = i;
	t->key = rec[ptr].first, t->pri = rec[ptr].second;
	t->l = mt(l, ptr - 1);
	t->r = mt(ptr + 1, r);
	return t;
}
int main(){
	cin>>n;
	for(int i = 0; i < n; i ++) cin>>rec[i].first>>rec[i].second;
	sort(rec, rec + n);
	node* t = mt(0, n - 1);
	queue<node*> que;
	que.push(t);
	vector<int> ans1, ans2;
	while(que.size()){
		auto tar = que.front();
		que.pop();
		ans1.push_back(tar->key);
		ans2.push_back(tar->pri);
		if(tar->l) que.push(tar->l);
		if(tar->r) que.push(tar->r);
	}
	for(int i = 0; i < ans1.size(); i ++)
		printf("%d%c", ans1[i], i == ans1.size() - 1 ? '\n' : ' ');
	for(int i = 0; i < ans2.size(); i ++)
		printf("%d%c", ans2[i], i == ans2.size() - 1 ? '\n' : ' ');
	return 0;
} 

7-1 Arrays and Linked Lists

Let’s design a data structure A that combines arrays and linked lists as the following:

At the very beginning, an integer array A0​ of length L0​ is initialized for the user. When the user tries to access the ith element A[i], if 0≤i<L0​, then A[i] is just A0​[i]. Now our system is supposed to return h0​+i×sizeof(int) as the accessed address, where h0​ is the initial address of A0​, and sizeof(int) is the size of the array element, which is simply int, taking 4 bytes.

In case there is an overflow of the user’s access (that is, i≥L0​), our system will declare another array A1​ of length L1​. Now A[i] corresponds to A1​[j] (It’s your job to figure out the relationship between i and j). If 0≤j<L1​, then h1​+j×sizeof(int) is returned as the accessed address, where h1​ is the initial address of A1​.

And if there is yet another overflow of the user’s access to A1​[j], our system will declare another array A2​ of length L2​, and so on so forth.

Your job is to implement this data structure and to return the address of any access.

Input Specification:

Each input file contains one test case. For each case, the first line gives 2 positive integers N (≤104) and K (≤103) which are the number of arrays that can be used, and the number of user queries, respectively.

Then N lines follow, each gives 2 positive integers, which are the initial address (≤107) and the length (≤100) of an array, respectively. The numbers are separated by spaces. It is guaranteed that there is no overlap of the spaces occupied by these arrays.

Finally, K indices of the elements queried by users are given in the last line. Each index is an integer in the range [0,220].

Output Specification:

For each query, print in a line the accessed address. If the queried index exceeds the range of all the N arrays, output Illegal Access instead, and this query must NOT be processed.

Print in the last line the total number of arrays that have been declared for the whole process.

Sample Input:

6 7
2048 5
128 6
4016 10
1024 7
3072 12
9332 10
2 12 25 50 28 8 39

Sample Output:

2056
4020
1040
Illegal Access
3072
140
3116
5

Hint:

A[2] is just A0​[2], so the accessed address is 2048+2×4=2056.

In order to access A[12], declaring A1​ is not enough, we need A2​ with initial address h2​=4016. Since A[12]=A2​[1], the accessed address is 4016+1×4=4020.

In order to access A[25], we need A3​ with initial address h3​=1024. Since A[25]=A3​[4], the accessed address is 1024+4×4=1040.

The access of A[50] exceeds the maximum boundary of all the arrays, and hence an illegal access. There is no extra array declared.

In order to access A[28], we need A4​ with initial address h4​=3072. Since A[28]=A4​[0], the accessed address is just 3072.

It is clear to see that A[8]=A1​[3] and hence the accessed address is 128+3×4=140; and A[39]=A4​[11] so the accessed address is 3072+11×4=3116.

All together there are 5 arrays used for the above queries.

7-2 Stack of Hats

hats.JPG

PATers believe that wearing hats makes them look handsome, so wherever they go, everyone of them would wear a hat. One day they came to a restaurant, a waiter collected their hats and piled them up. But then when they were ready to leave, they had to face a stack of hats as shown by the above figure. So your job is to help them line up so that everyone can pick up his/her hat one by one in order without any trouble.

It is known that every hat has a unique size, which is related to the weight of its owner – that is, the heavier one wears larger hat.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive number N (≤104) which is the number of PATers. The next line gives N distinct sizes of the hats, which are positive numbers no more than 105. The sizes correspond to the hats from bottom up on the stack. Finally in the last line, N distinct weights are given, correspond to the hat owners numbered from 1 to N. The weights are positive numbers no more than 106. All the numbers in a line are separated by spaces.

Output Specification:

For each test case, print in a line the indices of the hat owners in the order of picking up their hats. All the numbers must be separated by exactly 1 space, and there must be no extra space at the beginning or the end of the line.

Sample Input:

10
12 19 13 11 15 18 17 14 16 20
67 90 180 98 87 105 76 88 150 124

Sample Output:

3 4 8 6 10 2 1 5 9 7

Hint:

The 1st hat has the largest size 20, hence must correspond to the one with the largest weight, which is the 3rd one of weight 180. So No.3 must go first.

The 2nd hat has the 6th smallest size 16, hence corresponds to the 6th smallest weight, which is 98. So No.4 is the next to go.

And so on so forth.

7-3 Playground Exploration

pt.JPG

A playground is equipped with ball pits and tents connected by tunnels. Kids can crawl through the tunnels to meet their friends at a spot with a tent or a ball pit.

Now let’s mark each meeting spot (a tent or a ball pit) by a number. Assume that once a kid starts to explore the playground from any meeting spot, he/she will always choose the next destination with the smallest number, and he/she would never want to visit the same spot twice. Your job is to help the kids to find the best starting spot so they can explore as many spots as they can.

Input Specification:

Each input file contains one test case. For each case, the first line gives two positive integers N (≤100), the total number of spots, and M, the number of tunnels. Then M lines follow, each describes a tunnel by giving the indices of the spots (from 1 to N) at the two ends.

Output Specification:

Print in a line the best starting spot which leads to the maximum number of spots, and the number of spots a kid can explore. If the solution is not unique, output the one with the smallest index. There must be exactly 1 space between the two numbers, and there must be no extra space at the beginning or the end of the line.

Sample Input:

8 10
1 2
3 4
5 8
1 4
6 1
3 7
2 4
5 3
2 8
2 5

Sample Output:

6 7

Hint:

Actually there are two solutions. Kids can either start from 6 and go through 1->2->4->3->5->8, or from 7 to 3->4->1->2->5->8, both can visit 7 spots. Since 6 is a smaller index, we output 6 as the starting spot.

7-4 Sorted Cartesian Tree

Sorted Cartesian tree is a tree of (key, priority) pairs. The tree is heap-ordered according to the priority values, and an inorder traversal gives the keys in sorted order. For example, given the pairs { (55, 8), (58, 15), (62, 3), (73, 4), (85, 1), (88, 5), (90, 12), (95, 10), (96, 18), (98, 6) }, the increasing min-heap Cartesian tree is shown by the figure.

SCtree.jpg

Your job is to do level-order traversals on an increasing min-heap Cartesian tree.

Input Specification:

Each input file contains one test case. Each case starts from giving a positive integer N (≤30), and then N lines follow, each gives a pair in the format key priority. All the numbers are in the range of int.

Output Specification:

For each test case, print in the first line the level-order traversal key sequence and then in the next line the level-order traversal priority sequence of the min-heap Cartesian tree.

All the numbers in a line must be separated by exactly one space, and there must be no extra space at the beginning or the end of the line.

Sample Input:

10
88 5
58 15
95 10
62 3
55 8
98 6
85 1
90 12
96 18
73 4

Sample Output:

85 62 88 55 73 98 58 95 90 96
1 3 5 8 4 6 15 10 12 18
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值