【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

相关文章

Mybatis 传参与排序模糊查询功能实现

《Mybatis传参与排序模糊查询功能实现》:本文主要介绍Mybatis传参与排序模糊查询功能实现,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录一、#{ }和${ }传参的区别二、排序三、like查询四、数据库连接池五、mysql 开发企业规范一、#{ }和${ }传参的

使用Java实现通用树形结构构建工具类

《使用Java实现通用树形结构构建工具类》这篇文章主要为大家详细介绍了如何使用Java实现通用树形结构构建工具类,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录完整代码一、设计思想与核心功能二、核心实现原理1. 数据结构准备阶段2. 循环依赖检测算法3. 树形结构构建4. 搜索子

利用Python开发Markdown表格结构转换为Excel工具

《利用Python开发Markdown表格结构转换为Excel工具》在数据管理和文档编写过程中,我们经常使用Markdown来记录表格数据,但它没有Excel使用方便,所以本文将使用Python编写一... 目录1.完整代码2. 项目概述3. 代码解析3.1 依赖库3.2 GUI 设计3.3 解析 Mark

C++快速排序超详细讲解

《C++快速排序超详细讲解》快速排序是一种高效的排序算法,通过分治法将数组划分为两部分,递归排序,直到整个数组有序,通过代码解析和示例,详细解释了快速排序的工作原理和实现过程,需要的朋友可以参考下... 目录一、快速排序原理二、快速排序标准代码三、代码解析四、使用while循环的快速排序1.代码代码1.由快

mysql通过frm和ibd文件恢复表_mysql5.7根据.frm和.ibd文件恢复表结构和数据

《mysql通过frm和ibd文件恢复表_mysql5.7根据.frm和.ibd文件恢复表结构和数据》文章主要介绍了如何从.frm和.ibd文件恢复MySQLInnoDB表结构和数据,需要的朋友可以参... 目录一、恢复表结构二、恢复表数据补充方法一、恢复表结构(从 .frm 文件)方法 1:使用 mysq

Python中顺序结构和循环结构示例代码

《Python中顺序结构和循环结构示例代码》:本文主要介绍Python中的条件语句和循环语句,条件语句用于根据条件执行不同的代码块,循环语句用于重复执行一段代码,文章还详细说明了range函数的使... 目录一、条件语句(1)条件语句的定义(2)条件语句的语法(a)单分支 if(b)双分支 if-else(

Spring排序机制之接口与注解的使用方法

《Spring排序机制之接口与注解的使用方法》本文介绍了Spring中多种排序机制,包括Ordered接口、PriorityOrdered接口、@Order注解和@Priority注解,提供了详细示例... 目录一、Spring 排序的需求场景二、Spring 中的排序机制1、Ordered 接口2、Pri

使用Navicat工具比对两个数据库所有表结构的差异案例详解

《使用Navicat工具比对两个数据库所有表结构的差异案例详解》:本文主要介绍如何使用Navicat工具对比两个数据库test_old和test_new,并生成相应的DDLSQL语句,以便将te... 目录概要案例一、如图两个数据库test_old和test_new进行比较:二、开始比较总结概要公司存在多

大数据小内存排序问题如何巧妙解决

《大数据小内存排序问题如何巧妙解决》文章介绍了大数据小内存排序的三种方法:数据库排序、分治法和位图法,数据库排序简单但速度慢,对设备要求高;分治法高效但实现复杂;位图法可读性差,但存储空间受限... 目录三种方法:方法概要数据库排序(http://www.chinasem.cn对数据库设备要求较高)分治法(常

五大特性引领创新! 深度操作系统 deepin 25 Preview预览版发布

《五大特性引领创新!深度操作系统deepin25Preview预览版发布》今日,深度操作系统正式推出deepin25Preview版本,该版本集成了五大核心特性:磐石系统、全新DDE、Tr... 深度操作系统今日发布了 deepin 25 Preview,新版本囊括五大特性:磐石系统、全新 DDE、Tree