【PAT】1114. Family Property (25)【并查集、结构体排序】

2024-04-12 06:08

本文主要是介绍【PAT】1114. Family Property (25)【并查集、结构体排序】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目描述

This time, you are supposed to help us collect the data for family-owned property. Given each person’s family members, and the estate(房产)info under his/her own name, we need to know the size of each family, and the average area and number of sets of their real estate.

翻译:这一次,你需要帮助我们收集一个家族拥有的财产数据。给定每个家庭成员和在他/她的名义下的房地产(房产)信息,我们需要知道每个家庭的人口数,房产个数以及平均面积。

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤1000). Then N lines follow, each gives the infomation of a person who owns estate in the format:
ID Father Mother k Child1⋯Childk Mestate​ Area

where ID is a unique 4-digit identification number for each person; Father and Mother are the ID’s of this person’s parents (if a parent has passed away, -1 will be given instead); k (0≤k≤5) is the number of children of this person; Childi’s are the ID’s of his/her children; M​estate
​​ is the total number of sets of the real estate under his/her name; and Area is the total area of his/her estate.

翻译:每个输入文件包含一组测试数据。对于每组输入数据,第一行给出一个正整数N(≤1000)。接下来N行,每行按照以下格式给出一个人的拥有房产情况的信息:
ID Father Mother k Child1⋯Childk Mestate​ Area

ID为一个特定的4位数字标识一个人员,Father 和 Mother代表这个人的父母(如果父母一方去世,则用-1来代替);k(0≤k≤5)代表这个人拥有的孩子总数;Childi代表孩子的ID;Mestate代表在他名下的实际房产总数,Area代表他的房产的总面积。

Output Specification:

For each case, first print in a line the number of families (all the people that are related directly or indirectly are considered in the same family). Then output the family info in the format:

ID M AVGsets AVGarea

where ID is the smallest ID in the family; M is the total number of family members; AVGsets is the average number of sets of their real estate; and AVGarea​​ is the average area. The average numbers must be accurate up to 3 decimal places. The families must be given in descending order of their average areas, and in ascending order of the ID’s if there is a tie.

翻译:对于每组输入数据,输出一行家族的个数(所有有直接或间接关系的人都被认为是同一家庭)。接着按照以下格式输出一个家族的情况:
ID M AVGsets AVGarea
ID代表家族成员的最小ID;M 代表家族成员的总人数;AVGsets代表他们平均拥有的实际房产数。 AVGarea​​ 代表他们平均拥有的房产面积。平均数需要保留3位小数。家族必须按照它们的平均拥有房产面积大小降序排序,如果相同则按照ID大小升序排序。


Sample Input:

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


Sample Output:

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


解题思路

定义一个成员类,然后编写并查集,通过并查集将相同家族的成员合并在一起。然后对成员进行扫描,如果该成员在一个家族内且该家族的最小ID不是他则把他的数据加给该家族的最小ID。最后对家族排序。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<vector>
#include<algorithm>
#define INF 99999999
#define bug puts("Hello\n")
using namespace std;
int N;
int num[10000];//用于记录该id对应的家族人数 
struct Peo{int id;double mestate;double area;Peo():id(0),mestate(0),area(0.0){}Peo(int ID,int Mestate,double Area):id(ID),mestate(Mestate),area(Area){}Peo operator+(const Peo &a)const{double tmpmestate=mestate+a.mestate;double tmparea=area+a.area;return Peo(id,tmpmestate,tmparea);}bool operator<(const Peo &a)const{double averArea=area/num[id];double a_averArea=a.area/num[a.id];return averArea==a_averArea?id<a.id:averArea>a_averArea;}
};
Peo peo[10000];
int people[10000];//并查集 
int ans[10000];//保存该家族的最小编号 
Peo ansPeo[10000]; //将财产累计过后的该家族成员加入数组 
int anscount=0;
void init(){for(int i=0;i<10000;i++){peo[i].id=i;people[i]=i;} 
}
int find(int a){if(people[a]==a)return a;else return people[a]=find(people[a]);
}
void Union(int a,int b){int fa=find(a);int fb=find(b);if(fa<fb)people[fb]=fa;else people[fa]=fb;
}int main(){scanf("%d",&N);init();int pid,fa,mo,k;for(int i=0;i<N;i++){scanf("%d%d%d%d",&pid,&fa,&mo,&k);num[pid]=1;if(fa>=0)Union(pid,fa),num[fa]=1;if(mo>=0)Union(pid,mo),num[mo]=1;int cid;for(int i=0;i<k;i++){scanf("%d",&cid);num[cid]=1;Union(cid,pid);}double Mstate;double Area;scanf("%lf%lf",&Mstate,&Area);peo[pid].mestate=Mstate;peo[pid].area=Area;}for(int i=0;i<10000;i++){int tmp=find(people[i]);if(tmp==i&&num[i]){ans[anscount++]=tmp;}else if(num[i]){peo[tmp]=peo[tmp]+peo[i];num[tmp]++;}}for(int i=0;i<anscount;i++){ansPeo[i]=peo[ans[i]];}sort(ansPeo,ansPeo+anscount);printf("%d\n",anscount);for(int i=0;i<anscount;i++){int tmpID=ansPeo[i].id;printf("%04d %d %.3lf %.3lf\n",tmpID,num[tmpID],ansPeo[i].mestate/num[tmpID],ansPeo[i].area/num[tmpID]);}return 0;
}

这篇关于【PAT】1114. Family Property (25)【并查集、结构体排序】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/896314

相关文章

Redis中Set结构使用过程与原理说明

《Redis中Set结构使用过程与原理说明》本文解析了RedisSet数据结构,涵盖其基本操作(如添加、查找)、集合运算(交并差)、底层实现(intset与hashtable自动切换机制)、典型应用场... 目录开篇:从购物车到Redis Set一、Redis Set的基本操作1.1 编程常用命令1.2 集

Vite 打包目录结构自定义配置小结

《Vite打包目录结构自定义配置小结》在Vite工程开发中,默认打包后的dist目录资源常集中在asset目录下,不利于资源管理,本文基于Rollup配置原理,本文就来介绍一下通过Vite配置自定义... 目录一、实现原理二、具体配置步骤1. 基础配置文件2. 配置说明(1)js 资源分离(2)非 JS 资

Java集合中的链表与结构详解

《Java集合中的链表与结构详解》链表是一种物理存储结构上非连续的存储结构,数据元素的逻辑顺序的通过链表中的引用链接次序实现,文章对比ArrayList与LinkedList的结构差异,详细讲解了链表... 目录一、链表概念与结构二、当向单链表的实现2.1 准备工作2.2 初始化链表2.3 打印数据、链表长

C++归并排序代码实现示例代码

《C++归并排序代码实现示例代码》归并排序将待排序数组分成两个子数组,分别对这两个子数组进行排序,然后将排序好的子数组合并,得到排序后的数组,:本文主要介绍C++归并排序代码实现的相关资料,需要的... 目录1 算法核心思想2 代码实现3 算法时间复杂度1 算法核心思想归并排序是一种高效的排序方式,需要用

创建springBoot模块没有目录结构的解决方案

《创建springBoot模块没有目录结构的解决方案》2023版IntelliJIDEA创建模块时可能出现目录结构识别错误,导致文件显示异常,解决方法为选择模块后点击确认,重新校准项目结构设置,确保源... 目录创建spChina编程ringBoot模块没有目录结构解决方案总结创建springBoot模块没有目录

SpringBoot利用树形结构优化查询速度

《SpringBoot利用树形结构优化查询速度》这篇文章主要为大家详细介绍了SpringBoot利用树形结构优化查询速度,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一个真实的性能灾难传统方案为什么这么慢N+1查询灾难性能测试数据对比核心解决方案:一次查询 + O(n)算法解决

Oracle查询表结构建表语句索引等方式

《Oracle查询表结构建表语句索引等方式》使用USER_TAB_COLUMNS查询表结构可避免系统隐藏字段(如LISTUSER的CLOB与VARCHAR2同名字段),这些字段可能为dbms_lob.... 目录oracle查询表结构建表语句索引1.用“USER_TAB_COLUMNS”查询表结构2.用“a

MySQL中的索引结构和分类实战案例详解

《MySQL中的索引结构和分类实战案例详解》本文详解MySQL索引结构与分类,涵盖B树、B+树、哈希及全文索引,分析其原理与优劣势,并结合实战案例探讨创建、管理及优化技巧,助力提升查询性能,感兴趣的朋... 目录一、索引概述1.1 索引的定义与作用1.2 索引的基本原理二、索引结构详解2.1 B树索引2.2

一文详解Java Stream的sorted自定义排序

《一文详解JavaStream的sorted自定义排序》Javastream中的sorted方法是用于对流中的元素进行排序的方法,它可以接受一个comparator参数,用于指定排序规则,sorte... 目录一、sorted 操作的基础原理二、自定义排序的实现方式1. Comparator 接口的 Lam

如何使用Maven创建web目录结构

《如何使用Maven创建web目录结构》:本文主要介绍如何使用Maven创建web目录结构的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录创建web工程第一步第二步第三步第四步第五步第六步第七步总结创建web工程第一步js通过Maven骨架创pytho