PAT 1127 ZigZagging on a Tree(30分)

2020-05-02
阅读 3 分钟
2.1k
Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can be determined by a given pair of postorder and inorder traversal sequences. And it is a simple standard routine to print the numbers in level-order. However, if you think the problem is too simple,...

PAT 1030 Travel Plan(30分)

2020-03-20
阅读 5 分钟
1.3k
A traveler's map gives the distances between cities along the highways, together with the cost of each highway. Now you are supposed to write a program to help a traveler to decide the shortest path between his/her starting city and the destination. If such a shortest path is not unique, you are ...

PAT 1003 Emergency(25分)

2020-03-19
阅读 3 分钟
1.9k
As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the length of each road between any pair of cities are marked on the map. When there is an emergency c...

Dijkstra 算法(邻接矩阵 + 邻接表实现)

2020-03-19
阅读 3 分钟
3.9k
图 input {代码...} output {代码...} 邻接矩阵实现 {代码...} 邻接表实现 {代码...} 参考资料《算法笔记》.

PAT 1076Forwards on Weibo(30分)

2020-03-17
阅读 3 分钟
1.2k
Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and may follow many other users as well. Hence a social network is formed with followers relations. When a user makes a post on Weibo, all his/her followers can view and forward his/her post, which can th...

PAT 1034Head of a Gang(30分)

2020-03-17
阅读 4 分钟
1k
One way that the police finds the head of a gang is to check people's phone calls. If there is a phone call betweenAandB, we say thatAandBis related. The weight of a relation is defined to be the total time length of all the phone calls made between the two persons. A "Gang" is a cluster of more ...

PAT 1107 Social Clusters(30分)

2020-03-15
阅读 3 分钟
1.4k
When register on a social network, you are always asked to specify your hobbies in order to find some potential friends with the same hobbies. Asocial clusteris a set of people who have some of their hobbies in common. You are supposed to find all the clusters.

PAT 1066 Root of AVL Tree(25分)

2020-03-15
阅读 4 分钟
1.9k
An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this property. Figures 1-4 illustrate the rotation rules.

PAT 1099 Build A Binary Search Tree(30分)

2020-03-15
阅读 3 分钟
1.3k
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:

PAT 1094 The Largest Generation

2020-03-13
阅读 4 分钟
1.2k
A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level belong to the same generation. Your task is to find the generation with the largest population.

PAT 1064 Complete Binary Search Tree(30分)

2020-03-13
阅读 2 分钟
1.3k
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:

PAT 1004 Counting Leaves

2020-03-13
阅读 3 分钟
1.5k
A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child.

PAT 1090 Highest Price in Supply Chain(25分)

2020-03-12
阅读 4 分钟
1.5k
A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone involved in moving a product from supplier to customer.

PAT 1079 Total Sales of Supply Chain

2020-03-12
阅读 4 分钟
1.1k
A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone involved in moving a product from supplier to customer.

PAT 1102 Invert a Binary Tree

2020-03-10
阅读 4 分钟
1.1k
Each input file contains one test case. For each case, the first line gives a positive integerN(≤10) which is the total number of nodes in the tree -- and hence the nodes are numbered from 0 toN−1. ThenNlines follow, each corresponds to a node from 0 toN−1, and gives the indices of the left and r...

PAT 1086 Tree Traversals Again

2020-03-10
阅读 3 分钟
1.7k
An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when a 6-node binary tree (with the keys numbered from 1 to 6) is traversed, the stack operations are: push(1); push(2); push(3); pop(); pop(); push(4); pop(); pop(); push(5); push(6...

PAT A1043 Is It a Binary Search Tree

2020-03-07
阅读 4 分钟
1.5k
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:

PAT-1091Acute Stroke(30分)

2020-02-29
阅读 3 分钟
1.2k
One important factor to identify acute stroke (急性脑卒中) is the volume of the stroke core. Given the results of image analysis in which the core regions are identified in each MRI slice, your job is to calculate the volume of the stroke core.

PAT-A1103.Integer Factorization

2020-02-28
阅读 3 分钟
1.1k
TheK−Pfactorization of a positive integerNis to writeNas the sum of theP-th power ofKpositive integers. You are supposed to write a program to find theK−Pfactorization ofNfor any positive integersN,KandP.

归并、快速排序

2020-02-15
阅读 4 分钟
1.1k
先将序列两两分组,把序列归并为$$\lceil\frac{n}{2}\rceil$$个组,组内单独排序;然后将这些组两两归并,生成$$\lceil\frac{n}{4}\rceil$$个组,组内再单独排序;以此类推,直到只剩下一个组为止。归并排序的时间复杂度为O(nlogn)。

二分算法(详细分类版)

2020-02-12
阅读 4 分钟
3.2k
注意1.每次循环的搜索空间是[left, right]左闭右闭,[left, right)左闭右开也可。3.当left == right时while循环中止,此时搜索空间是[left, left]为空,返回 left 或 right 都可。如果元素存在则返回满足条件的位置,反之返回它应该在的位置。4.二分的初始区间应当能覆盖到所有可能返回的结果。二分下界是很显然是0,但...

区间贪心

2020-02-08
阅读 2 分钟
1.6k
给出 N 个开区间(x, y),从中选择尽可能多的开区间,使得这些开区间两两没有交集。例如对于开区间(1, 3)、(2, 4)、(3, 5)、(6, 7)来说,可以选出最多三个区间(1, 3)、(3, 5)、(6, 7),它们之间没有交集。

AcWing 122.糖果传递(贪心)

2020-02-06
阅读 1 分钟
1.4k
AcWing 122.糖果传递 1.问题 有n个小朋友坐成一圈,每人有a[i]个糖果。 每人只能给左右两人传递糖果。 每人每次传递一个糖果代价为1。 求使所有人获得均等糖果的最小代价。 输入格式 第一行输入一个正整数n,表示小朋友的个数。 接下来n行,每行一个整数a[i],表示第i个小朋友初始得到的糖果的颗数。 输出格式 输出一个...

AcWing 104.货舱选址(贪心)

2020-02-06
阅读 1 分钟
1.6k
AcWing 104.货舱选址 1.问题 在一条数轴上有N家商店,它们的坐标分别为A1~AN。 现在需要在数轴上建立一家货仓,每天清晨,从货仓到每家商店都要运送一车商品。 为了提高效率,求把货仓建在何处,可以使得货仓到每家商店的距离之和最小。 输入格式 第一行输入整数N。 第二行N个整数A1~AN。 输出格式 输出一个整数,表示距...

n 皇后问题(递归-暴力法+回溯法)

2020-02-06
阅读 2 分钟
3.5k
在n×n格的棋盘上放置彼此不受攻击的n个皇后。按照国际象棋的规则,皇后可以攻击与之处在同一行或同一列或同一斜线上的棋子。n后问题等价于再n×n的棋盘上放置n个皇后,任何2个皇后不妨在同一行或同一列或同一斜线上。

递归实现组合型枚举

2020-02-04
阅读 1 分钟
2.6k
递归实现组合型枚举 问题 从 1~n 这 n 个整数中随机选出 m 个,输出所有可能的选择方案。 输入格式 两个整数n,m在同一行用空格隔开。 输出格式 按照从小到大的顺序输出所有方案,每行1个。 首先,同一行内的数升序排列,相邻两个数用一个空格隔开。 其次,对于两个不同的行,对应下标的数一一比较,字典序较小的排在前面...

递归实现全排列

2020-02-04
阅读 1 分钟
3.7k
递归实现全排列 问题 把 1~n这n个整数排成一行后随机打乱顺序,输出所有可能的次序。 输入格式 一个整数n。 输出格式 按照从小到大的顺序输出所有方案,每行1个。 首先,同一行相邻两个数用一个空格隔开。 其次,对于两个不同的行,对应下标的数一一比较,字典序较小的排在前面。 数据范围 1≤n≤9 输入样例: {代码...} ...

PAT (A) 1012 The Best Rank

2020-01-31
阅读 4 分钟
1.2k
To evaluate the performance of our first year CS majored students, we consider their grades of three courses only:C- C Programming Language,M- Mathematics (Calculus or Linear Algrbra), andE- English. At the mean time, we encourage students by emphasizing on their best ranks -- that is, among the ...

选择、插入、冒泡排序(原理和代码实现)

2020-01-29
阅读 2 分钟
1.1k
含义:对一个序列 A 中的元素 A[1] ~ A[n] ,令 i 从 1 到 n 枚举,进行 n 趟操作,每趟从待排序部分 [i, n] 中选择最小的元素,令其与待排序部分的第一个元素 A[i] 进行交换,这样元素 A[i]就会与当前有序区间 [1, i -1]形成新的有序区间 [1, i]。示意图:代码:(复杂度:O(n^2) )

AcWing 1101. 献给阿尔吉侬的花束 《信息学奥赛一本通》

2020-01-28
阅读 2 分钟
1.3k
1.问题 阿尔吉侬是一只聪明又慵懒的小白鼠,它最擅长的就是走各种各样的迷宫。 今天它要挑战一个非常大的迷宫,研究员们为了鼓励阿尔吉侬尽快到达终点,就在终点放了一块阿尔吉侬最喜欢的奶酪。 现在研究员们想知道,如果阿尔吉侬足够聪明,它最少需要多少时间就能吃到奶酪。 迷宫用一个R×C的字符矩阵来表示。 字符 S 表...