1118. Birds in Forest (25)-PAT甲级真题(并查集)

Some scientists took pictures of thousands of birds in a forest. Assume that all the birds appear in the same picture belong to the same tree. You are supposed to help the scientists to count the maximum number of trees in the forest, and for any pair of birds, tell if they are on the same tree.

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 pictures. Then N lines follow, each describes a picture in the format:
K B1 B2 … BK
where K is the number of birds in this picture, and Bi’s are the indices of birds. It is guaranteed that the birds in all the pictures are numbered continuously from 1 to some number that is no more than 104.

After the pictures there is a positive number Q (<= 104) which is the number of queries. Then Q lines follow, each contains the indices of two birds.

Output Specification:

For each test case, first output in a line the maximum possible number of trees and the number of birds. Then for each query, print in a line “Yes” if the two birds belong to the same tree, or “No” if not.

Sample Input:
4
3 10 1 2
2 3 4
4 1 5 7 8
3 9 6 4
2
10 5
3 7
Sample Output:
2 10
Yes
No

题目大意:一幅画里面的鸟为同一棵树上的,问有多少棵树和多少只鸟,以及对于两只鸟判断是否在同一个树上~

分析:使用并查集就可以啦~将一幅画中鸟使用Union函数合并在同一个集合里面,cnt[i]数组保存以i为FindFather结点的集合里面的鸟的个数,exist[i]表示鸟的id——i在输入的鸟的序号里面是否出现过,遍历cnt数组并累加所有不为0的个数即可得知有多少棵树,累加所有出现过的鸟的id的cnt的值即可得知鸟的个数~判断两只鸟是否在同一棵树,只需要知道这两只鸟的findFather是否相等即可~

1060. 爱丁顿数(25)-PAT乙级真题

英国天文学家爱丁顿很喜欢骑车。据说他为了炫耀自己的骑车功力,还定义了一个“爱丁顿数”E,即满足有E天骑车超过E英里的最大整数E。据说爱丁顿自己的E等于87。

现给定某人N天的骑车距离,请你算出对应的爱丁顿数E(<=N)。

输入格式:

输入第一行给出一个正整数N(<=105),即连续骑车的天数;第二行给出N个非负整数,代表每天的骑车距离。

输出格式:

在一行中给出N天的爱丁顿数。

输入样例:
10
6 7 6 9 3 10 8 2 7 8
输出样例:
6

分析:从下标1开始存储n天的公里数在数组a中,对n个数据从大到小排序,i表示了骑车的天数,那么满足a[i] > i的最大值即为所求~

 

1117. Eddington Number(25)-PAT甲级真题

British astronomer Eddington liked to ride a bike. It is said that in order to show off his skill, he has even defined an “Eddington number”, E — that is, the maximum integer E such that it is for E days that one rides more than E miles. Eddington’s own E was 87.

Now given everyday’s distances that one rides for N days, you are supposed to find the corresponding E (<=N).

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N(<=105), the days of continuous riding. Then N non-negative integers are given in the next line, being the riding distances of everyday.

Output Specification:

For each case, print in a line the Eddington number for these N days.

Sample Input:
10
6 7 6 9 3 10 8 2 7 8
Sample Output:
6

题目大意:英国天文学家爱丁顿很喜欢骑车。据说他为了炫耀自己的骑车功力,还定义了一个“爱丁顿数”E,即满足有E天骑车超过E英里的最大整数E。据说爱丁顿自己的E等于87。
分析:在数组a中存储n天的公里数,对n个数据从大到小排序,根据排序后的数组可得知骑车大于等于a[i] 英里的天数有 i+1 天,因为题目中要求超过E英里且所有的英里数为整数,所以可得知骑车超过a[i]-1 英里的天数有 i+1 天,如样例,从大到小排序后结果为10、9、8、8、7、7、6、6、3、2,骑车超过9英里的有1天,超过8英里的有2天,超过7英里的有4天/5天,超过6英里的有5天/6天,超过5英里的有7天/8天…英里数不断减少,天数不断增加,既然已知公里数和天数对应的关系,要想满足题意,必须使超过的英里数a[i]-1大于等于天数i+1(比如样例中,超过6英里的有6天,超过5英里的有7天,前者的天数6满足题意这6天都超过了6英里,后者的天数7不满足因为这7天只满足骑车都超过5英里)即a[i]-1 >= i+1,也就是 a[i]  >= i + 2,也就是a[i] > i + 1~从头到尾遍历数组,那么满足a[i] > i+1的最大i+1即为所求~

1116. Come on! Let’s C (20)-PAT甲级真题

“Let’s C” is a popular and fun programming contest hosted by the College of Computer Science and Technology, Zhejiang University. Since the idea of the contest is for fun, the award rules are funny as the following:

0. The Champion will receive a “Mystery Award” (such as a BIG collection of students’ research papers…).
1. Those who ranked as a prime number will receive the best award — the Minions (小黄人)!
2. Everyone else will receive chocolates.

Given the final ranklist and a sequence of contestant ID’s, you are supposed to tell the corresponding awards.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (<=10000), the total number of contestants. Then N lines of the ranklist follow, each in order gives a contestant’s ID (a 4-digit number). After the ranklist, there is a positive integer K followed by K query ID’s.

Output Specification:

For each query, print in a line “ID: award” where the award is “Mystery Award”, or “Minion”, or “Chocolate”. If the ID is not in the ranklist, print “Are you kidding?” instead. If the ID has been checked before, print “ID: Checked”.

Sample Input:
6
1111
6666
8888
1234
5555
0001
6
8888
0001
1111
2222
8888
2222
Sample Output:
8888: Minion
0001: Chocolate
1111: Mystery Award
2222: Are you kidding?
8888: Checked
2222: Are you kidding?

分析:ran数组标记每个id对应的排名,集合ss存储所有已经询问过的id,如果发现当前id已经出现在ss中,则输出“Checked”,如果ran[id] == 0说明当前id不在排名列表中,所以输出“Are you kidding?”,如果ran[id]为1则输出“Minion”,如果ran[id]为素数则输出“Mystery Award”,否则输出“Chocolate”

 

1069. The Black Hole of Numbers (20)-PAT甲级真题

For any 4-digit integer except the ones with all the digits being the same, if we sort the digits in non-increasing order first, and then in non-decreasing order, a new number can be obtained by taking the second number from the first one. Repeat in this manner we will soon end up at the number 6174 — the “black hole” of 4-digit numbers. This number is named Kaprekar Constant.

For example, start from 6767, we’ll get:

7766 – 6677 = 1089
9810 – 0189 = 9621
9621 – 1269 = 8352
8532 – 2358 = 6174
7641 – 1467 = 6174
… …

Given any 4-digit number, you are supposed to illustrate the way it gets into the black hole.

Input Specification:

Each input file contains one test case which gives a positive integer N in the range (0, 10000).

Output Specification:

If all the 4 digits of N are the same, print in one line the equation “N – N = 0000”. Else print each step of calculation in a line until 6174 comes out as the difference. All the numbers must be printed as 4-digit numbers.

Sample Input 1:
6767
Sample Output 1:
7766 – 6677 = 1089
9810 – 0189 = 9621
9621 – 1269 = 8352
8532 – 2358 = 6174
Sample Input 2:
2222
Sample Output 2:
2222 – 2222 = 0000

分析:有一个测试用例注意点,如果当输入N值为6174的时候,依旧要进行下面的步骤,直到差值为6174才可以~所以用do while语句,无论是什么值总是要执行一遍while语句,直到遇到差值是0000或6174~

s.insert(0, 4 – s.length(), ‘0’);用来给不足4位的时候前面补0~

1090. Highest Price in Supply Chain (25)-PAT甲级真题(DFS)

A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)– everyone involved in moving a product from supplier to customer.

Starting from one root supplier, everyone on the chain buys products from one’s supplier in a price P and sell or distribute them in a price that is r% higher than P. It is assumed that each member in the supply chain has exactly one supplier except the root supplier, and there is no supply cycle.

Now given a supply chain, you are supposed to tell the highest price we can expect from some retailers.

Input Specification:

Each input file contains one test case. For each case, The first line contains three positive numbers: N (<=105), the total number of the members in the supply chain (and hence they are numbered from 0 to N-1); P, the price given by the root supplier; and r, the percentage rate of price increment for each distributor or retailer. Then the next line contains N numbers, each number Si is the index of the supplier for the i-th member. Sroot for the root supplier is defined to be -1. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print in one line the highest price we can expect from some retailers, accurate up to 2 decimal places, and the number of retailers that sell at the highest price. There must be one space between the two numbers. It is guaranteed that the price will not exceed 1010.

Sample Input:
9 1.80 1.00
1 5 4 4 -1 4 5 3 6
Sample Output:
1.85 2

题目大意:给一棵树,在树根处货物的价格为p,然后每往下走一层,价格增加r%,求所有叶子结点的最高价格,以及这个价格的叶子结点个数。
分析:用二维数组v[i][j]存储,对于每一个结点i,它的孩子结点的下标push_back存储在v[i]中。用深度优先搜索dfs,保存当前结点的下标index以及当前结点所在层数,当 当前结点的孩子结点个数为0的时候说明是叶子结点,更新maxdepth和maxnum的值,最后输出~
注意:如果采用保存某个结点的父结点的下标的形式,然后一直遍历到根结点的深度/广度优先,会出现三个超时。因为从叶子结点往上遍历将会把所有路径都走一遍,很多都是重复走的路径,会超时,没有从根结点往下遍历的方式快~~
记得r是百分比,要除以100之后再计算复利~