【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

相关文章

随想录 Day 69 并查集 107. 寻找存在的路径

随想录 Day 69 并查集 107. 寻找存在的路径 理论基础 int n = 1005; // n根据题目中节点数量而定,一般比节点数量大一点就好vector<int> father = vector<int> (n, 0); // C++里的一种数组结构// 并查集初始化void init() {for (int i = 0; i < n; ++i) {father[i] = i;}

百度OCR识别结构结构化处理视频

https://edu.csdn.net/course/detail/10506

微信小程序开发必知必会:文件结构和基本配置

一、微信小程序基本文件结构 1.  project.config.json:项目的基本配置文件,包括项目名称、appid、项目目录、页面文件夹等。     {"setting": {"urlCheck": false,"es6": true,"postcss": true,"nodeModulesPath": "D:\\\\node_modules"},"appid": "wxd678e

数据结构9——排序

一、冒泡排序 冒泡排序(Bubble Sort),顾名思义,就是指越小的元素会经由交换慢慢“浮”到数列的顶端。 算法原理 从左到右,依次比较相邻的元素大小,更大的元素交换到右边;从第一组相邻元素比较到最后一组相邻元素,这一步结束最后一个元素必然是参与比较的元素中最大的元素;按照大的居右原则,重新从左到后比较,前一轮中得到的最后一个元素不参4与比较,得出新一轮的最大元素;按照上述规则,每一轮结

PAT-1039 到底买不买(20)(字符串的使用)

题目描述 小红想买些珠子做一串自己喜欢的珠串。卖珠子的摊主有很多串五颜六色的珠串,但是不肯把任何一串拆散了卖。于是小红要你帮忙判断一下,某串珠子里是否包含了全部自己想要的珠子?如果是,那么告诉她有多少多余的珠子;如果不是,那么告诉她缺了多少珠子。为方便起见,我们用[0-9]、[a-z]、[A-Z]范围内的字符来表示颜色。例如,YrR8RrY是小红想做的珠串;那么ppRYYGrrYBR2258可以

PAT-1028

题目描述 某城镇进行人口普查,得到了全体居民的生日。现请你写个程序,找出镇上最年长和最年轻的人。这里确保每个输入的日期都是合法的,但不一定是合理的——假设已知镇上没有超过200岁的老人,而今天是2014年9月6日,所以超过200岁的生日和未出生的生日都是不合理的,应该被过滤掉。 输入描述: 输入在第一行给出正整数N,取值在(0, 105];随后N行,每行给出1个人的姓名(由不超过5个

七种排序方式总结

/*2018.01.23*A:YUAN*T:其中排序算法:冒泡排序,简单排序,直接插入排序,希尔排序,堆排序,归并排序,快速排序*/#include <stdio.h>#include <math.h>#include <malloc.h>#define MAXSIZE 10000#define FALSE 0#define TRUE 1typedef struct {i

利用结构体作为函数参数时结构体指针的定义

在利用结构体作为函数的参数进行传递时,容易犯的一个错误是将一个野指针传给函数导致错误。 #include <stdio.h>#include <math.h>#include <malloc.h>#define MAXSIZE 10typedef struct {int r[MAXSIZE]; //用于存储要排序的数组,r[0]作为哨兵或者临时变量int length;

PS系统教程25

介绍软件 BR(bridge) PS 配套软件,方便素材整理、管理素材 作用:起到桥梁作用 注意:PS和BR尽量保持版本一致 下载和安装可通过CSDN社区搜索,有免费安装指导。 安装之后,我们打开照片只需双击照片,就自动在Ps软件中打开。 前提:电脑上有PS软件 三种预览格式 全屏预览 评星级 直接按数字键就可以 方向键可以更换图片 esc退出 幻灯片放

拓扑排序——C语言

拓扑排序(Topological Sorting)是一种用于有向无环图(DAG)的排序算法,其输出是图中所有顶点的线性排序,使得对于每条有向边 (u, v),顶点 u 在 v 之前出现。拓扑排序确定了项目网络图中的起始事件和终止事件,也就是顶点的执行顺序。         因为是有向无环图,所以拓扑排序的作用其实就是把先发生的排序在前面,后发生的排序到后面。 例如现在我们有一个