我的PAT-ADVANCED代码仓:https://github.com/617076674/PAT-ADVANCED

原题链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805356599820288

题目描述:

题目翻译:

1114 家庭资产

这一次,你需要帮助我们收集家庭财产的数据。给出每个人的家庭成员以及他/她自己名下的房产信息,我们需要知道每个家庭的规模,以及他们房地产的平均面积和数量。

输入格式:

每个输入文件包含一个测试用例。对于每个测试用例,第一行给出正整数N(<= 1000)。接下来是N行,每行都按以下格式给出拥有财产的人的信息:

ID Father Mother k Child​1​​⋯Child​k​​ M​estate​​ Area

其中ID是一个每个人的编号,是一个4位数;Father和Mother是其爸爸和妈妈的ID值(如果Father或Mother过世了,相应的位置由-1代替);k(0 <= k <= 5)是其孩子数量;Childi是他/她的孩子ID值;Mestate是其名下的房地产数量;Area是其名下的房地产面积。

输出格式:

对于每种情况,首先在一行中打印家庭数量(所有直接或间接相关的人都被视为同一家庭)。然后以以下格式输出家庭信息:

ID M AVG​sets​​ AVG​area​​

其中ID是家庭中最小的ID;M是家庭成员的总数;AVGsets是家族房地产的平均数量;AVGarea是家族房地产的平均面积。平均数字必须精确到小数点后3位。这些家庭必须按其平均面积的降序排列,并且如果存在相同的情况,则按照ID的升序排列。

输入样例:

10
6666 5551 5552 1 7777 1 100
1234 5678 9012 1 0002 2 300
8888 -1 -1 0 1 1000
2468 0001 0004 1 2222 1 500
7777 6666 -1 0 2 300
3721 -1 -1 1 2333 2 150
9012 -1 -1 3 1236 1235 1234 1 100
1235 5678 9012 0 1 50
2222 1236 2468 2 6661 6662 1 300
2333 -1 3721 3 6661 6662 6663 1 100

输出样例:

3
8888 1 1.000 1000.000
0001 15 0.600 100.000
5551 4 0.750 100.000

知识点:并查集

思路:将相同家族的人归类进一个集合中

对于平均房产数量和平均房产面积,存储的时候存储总量,输出时再转换为double类型输出平均值。

时间复杂度和空间复杂度均是O(N)。

C++代码:

#include<iostream>
#include<set>
#include<algorithm>using namespace std;struct person {int id;int Mestate;int Area;person() {Mestate = 0;Area = 0;}person(int _id, int _Mestate, int _Area) : id(_id), Mestate(_Mestate), Area(_Area) {};
};struct family{int ID;int M;int Totalsets;int Totalarea;family() {};family(int _ID, int _M, int _Totalsets, int _Totalarea) : ID(_ID), M(_M), Totalsets(_Totalsets), Totalarea(_Totalarea) {};
};bool flag[10000];
int parent[10000];
person people[10000];int findFather(int x);
void unionTwo(int a, int b);
bool cmp(family f1, family f2);int main() {fill(flag, flag + 10000, false);for(int i = 0; i < 10000; i++) {  //并查集初始化parent[i] = i;}int N;scanf("%d", &N);int ID, Father, Mother, k, child, Mestate, Area;for(int i = 0; i < N; i++) {scanf("%d %d %d %d", &ID, &Father, &Mother, &k);flag[ID] = true;if(Father != -1) {unionTwo(ID, Father);flag[Father] = true;}if(Mother != -1) {unionTwo(ID, Mother);flag[Mother] = true;}for(int j = 0; j < k; j++){scanf("%d", &child);unionTwo(ID, child);flag[child] = true;}scanf("%d %d", &Mestate, &Area);people[ID] = person(ID, Mestate, Area);}set<int> fatherSet;for(int i = 0; i < 10000; i++){if(!flag[i]){continue;}fatherSet.insert(findFather(i));}printf("%d\n", fatherSet.size());family families[fatherSet.size()];int index = 0;for(set<int>::iterator it = fatherSet.begin(); it != fatherSet.end(); it++){int ID = -1, M = 0, Totalsets = 0, Totalarea = 0;for(int i = 0; i < 10000; i++){if(!flag[i] || findFather(i) != *it){continue;}if(ID == -1){ID = i;}M++;Totalsets += people[i].Mestate;Totalarea += people[i].Area;}families[index++] = family(ID, M, Totalsets, Totalarea);}sort(families, families + fatherSet.size(), cmp);for(int i = 0; i < fatherSet.size(); i++){printf("%04d %d %.3f %.3f\n", families[i].ID, families[i].M, families[i].Totalsets * 1.0 / families[i].M, families[i].Totalarea * 1.0 / families[i].M);}return 0;
}int findFather(int x) {int a = x;while(x != parent[x]) {x = parent[x];}while(a != parent[a]) { //路径压缩 int z = a;a = parent[a];parent[z] = x;}return x;
}void unionTwo(int a, int b) {int aFather = findFather(a);int bFather = findFather(b);if(aFather != bFather) {parent[aFather] = bFather;}
}bool cmp(family f1, family f2){if(f1.Totalarea * 1.0 / f1.M == f2.Totalarea * 1.0 / f2.M){return f1.ID < f2.ID;}else{return f1.Totalarea * 1.0 / f1.M > f2.Totalarea * 1.0 / f2.M;}
}

C++解题报告:

PAT-ADVANCED1114——Family Property相关推荐

  1. PAT 1114 Family Property 并查集

    This time, you are supposed to help us collect the data for family-owned property. Given each person ...

  2. PAT甲级1114 Family Property:[C++题解]结构体、并查集、测试点3、4、5有问题的进来!!

    文章目录 题目分析 题目链接 题目分析 来源:acwing 分析: 先建边.读入每家的信息,在本人和父母(如果有的话),本人与子女(如果有的话)之间分别建边.边用结构体来存,边记录两个端点. 遍历每条 ...

  3. 1114. Family Property (25)-PAT甲级真题(并查集)

    This time, you are supposed to help us collect the data for family-owned property. Given each person ...

  4. 【PAT】A1114 Family Property (25 point(s))

    文章目录 A1114 Family Property (25 point(s)) Input Specification: Output Specification: Sample Input: Sa ...

  5. PAT甲级真题目录(按题型整理)(转自柳神)

    转载自:https://www.liuchuo.net/archives/2502?tdsourcetag=s_pcqq_aiomsg 最短路径 1003. Emergency (25)-PAT甲级真 ...

  6. PAT 十一章 模拟 17-24 自用

    十一模拟十7 1595. 螺旋矩阵 PAT甲级真题1105 1595. 螺旋矩阵 给定一个包含 N 个正整数的序列,请你将序列中的元素以非递增顺序填充到螺旋矩阵中. 从左上角的第一个元素开始填充,并按 ...

  7. 【PAT甲级】A1001-A1050刷题记录

    文章目录 A1001 A+B Format (20 分) 0.25 ★(一元多项式加法) A1002 A+B for Polynomials (25 分) 0.21 (单源最短路Dijkstra+边权 ...

  8. 【PAT甲级】A1051-A1100刷题记录

    文章目录 (栈) A1051 Pop Sequence (25 分) 0.47 (静态链表) A1052 Linked List Sorting (25 分) 0.21 (静态树+先根遍历DFS) A ...

  9. 【PAT甲级】A1101-A1155刷题记录

    文章目录 (递推) A1101 Quick Sort (25 分) 0.23 (静态二叉树+遍历) A1102 Invert a Binary Tree (25 分) 0.51 (数学问题) A110 ...

  10. 刷PAT甲级的各题思路、细节以及遇到的问题记录

    1001 A+B Format (20分) 因为一定会用到字符串,而string非常好用,但是用的时候一定要注意不能越界访问,否则会在运行时出现abort() has been called. 100 ...

最新文章

  1. linux下eclipse的使用教程,linux下Eclipse的使用方法总结.doc
  2. 加拿大留学商科好还是计算机科学好,去加拿大读商科专业就是要选择这些才最好!...
  3. Python sorted 和 sort() 的区别
  4. jquery 的ajax请求示例和注意事项
  5. jQuery 3.0 的 setter/getter 模式
  6. static{}静态代码块与{}普通代码块和构造代码块之间的区别
  7. Office 365有个AI「工作场所分析」掌握组织人力资源
  8. 11. 配置ZooKeeper ensemble
  9. python win32api win32gui win32con PyUserInput实现自动化脚本
  10. Git - 教程(廖雪峰)
  11. \x3c\x73\x63\x72\x69\x70\x74\x3ealert('xss');\x3c\x2f\x73\x63\x72\x69\x70\x74\x3e
  12. 2022.02.10
  13. 谷歌神经网络机器翻译NMT:人人可利用TensorFlow快速建立翻译模型
  14. 02【代词】人称代词,指示代词,不定代词?
  15. uva 12307 - Smallest Enclosing Rectangle(旋转卡壳)
  16. 基于XMPP协议的Android即时通信系(http://blog.csdn.net/lnb333666/article/details/7471292)...
  17. Python调用HEG批量转换hdf影像为tiff
  18. Linux下screen命令实操
  19. Android屏幕常亮防息屏
  20. [自编码器:理论+代码]:自编码器、栈式自编码器、欠完备自编码器、稀疏自编码器、去噪自编码器、卷积自编码器

热门文章

  1. 程序员在上海税前12000的工资,真实发到手能拿到多少?
  2. google浏览器打包扩展程序
  3. Java-项目1-家庭收支记账软件
  4. 支付宝支付成功之后的接口_继支付宝微信之后,51信用卡还款也开始收费了,手续费再创新高...
  5. 新出炉的 100+ 篇技术热文,在微信热传,别错过哦
  6. Startup攻略秘籍,从头到尾,一篇搞定!
  7. AWS免费套餐服务器部署NETCORE网站
  8. 从零开始用 Windows C++ 桌面程序制作方舟同人游戏(一)
  9. springboot 项目启动报Has been loaded by XML or SqlProvider, ignoring the injection of the SQL的错误的解决方案
  10. 神经网络参数个数计算,神经网络的参数设置