【刷题-PAT】A1114 Family Property (25 分)
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 child2 ... childk M 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; Child**i's are the ID's of his/her children; Mestate is the total number of sets of the real estate under his/her name; and Area is the total area of his/her estate.
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 AVG_{sets} AVG_{area}\)
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.
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
分析:要将每个家庭的数据分离出来,使用并查集即可实现,数据处理上有两种方法:
1.先将录入的数据按照输入的形式存在数组中,录入的同时完成集合的合并,然后再从录入的数据中挑出需要的数据,最后对挑出的数据仓晒礼输出;
2.在录入的同时进行集合的合并,并将数据按照输出的格式存储,然后进行处理
#include<iostream>
#include<cstdio>
#include<vector>
#include<string>
#include<unordered_map>
#include<set>
#include<queue>
#include<algorithm>
#include<cmath>
using namespace std;
const int nmax = 10100;
int fath[nmax];
void init(){
for(int i = 0; i < nmax; ++i)fath[i] = i;
}
int findF(int x){
int z = x;
while(x != fath[x])x = fath[x];
while(z != fath[z]){
int temp = fath[z];
fath[z] = x;
z = temp;
}
return x;
}
void Union(int a, int b){
int fa = findF(a), fb = findF(b);
if(fa < fb)fath[fb] = fa;
else if(fa > fb)fath[fa] = fb;
}
struct node{
int id, people;
double num, area;
bool flag = false;
bool operator < (node &a)const{
return area != a.area ? area > a.area : id < a.id;
}
}ans[nmax];
struct data{
int id, fid, mid, k;
int child[6];
int num, area;
}v[nmax];
bool vis[nmax] = {false};
int main(){
#ifdef ONLINE_JUDGE
#else
freopen("input.txt", "r", stdin);
#endif // ONLINE_JUDGE
init();
int N;
scanf("%d", &N);
//录入数据并合并,由于序号不连续,要标记哪些序号已经出现过
for(int i = 0; i < N; ++i){
int id, fid, mid, k;
scanf("%d%d%d%d", &id, &fid, &mid, &k);
v[id].id = id;
v[id].fid = fid;
v[id].mid = mid;
v[id].k = k;
vis[id] = true;
if(fid != -1){
Union(id, fid);
vis[fid] = true;
}
if(mid != -1){
Union(id, mid);
vis[mid] = true;
}
for(int j = 0; j < k; ++j){
scanf("%d", &v[id].child[j]);
Union(id, v[id].child[j]);
vis[v[id].child[j]] = true;
}
scanf("%d%d", &v[id].num, &v[id].area);
}
//统计并抽出需要的数据,用flag标记该根节点是否已经出现
int cnt = 0;
for(int i = 0; i < nmax; ++i){
if(vis[i] == true){
int index = findF(i);
ans[index].id = index;
ans[index].people++;
ans[index].num += v[i].num;
ans[index].area += v[i].area;
if(ans[index].flag == false)cnt++;
ans[index].flag = true;
}
}
//处理
for(int i = 0; i < nmax; ++i){
if(ans[i].flag == true){
ans[i].num /= ans[i].people;
ans[i].area /= ans[i].people;
}
}
//排序输出
sort(ans, ans + nmax);
printf("%d\n", cnt);
for(int i = 0; i < cnt; ++i){
printf("%04d %d %.3f %.3f\n", ans[i].id, ans[i].people, ans[i].num, ans[i].area);
}
return 0;
}
#include<cstdio>
#include<iostream>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;
const int Nmax = 10000;
int father[Nmax];
bool vis[Nmax] = {false};
struct node{
int id, peoNum;
double Sav, Aav;
};
void init(){
for(int i = 0; i < Nmax; ++i)father[i] = i;
}
int findF(int x){
int z = x;
while(x != father[x])x = father[x];
while(z != father[z]){
int temp = father[z];
father[z] = x;
z = temp;
}
return x;
}
void Union(int a, int b){
int fa = findF(a), fb = findF(b);
if(fa != fb)father[fb] = fa;
}
bool cmp(node a, node b){
bool flag = false;
if(a.Aav / a.peoNum > b.Aav / b.peoNum){
flag = true;
}else if(a.Aav / a.peoNum == b.Aav / b.peoNum && a.id < b.id){
flag = true;
}
return flag;
}
int main(){
init();
int N = 0;
vector<node>v, v2;
scanf("%d", &N);
for(int i = 0; i < N; ++i){
int f, m, id, k, idm = 10000;
int peonum = 0;
scanf("%d%d%d%d", &id, &f, &m, &k);
if(f == -1 && m == -1)idm = id;
if(f == -1 && m != -1)idm = min(id, m);
if(f != -1 && m == -1)idm = min(id, f);
if(f != -1 && m != -1)idm = min(min(m, f), id);
if(!vis[id]){
peonum = 1;
vis[id] = true;
}
if(f != -1){
Union(f, id);
if(!vis[f]){
peonum++;
vis[f] = true;
}
}
if(m != -1){
Union(m, id);
if(!vis[m]){
peonum++;
vis[m] = true;
}
}
for(int j = 0; j < k; ++j){
int child;
scanf("%d", &child);
idm = min(idm, child);
Union(id, child);
if(!vis[child]){
peonum++;
vis[child] = true;
}
}
int setNum, area;
scanf("%d%d", &setNum, &area);
int flag = false;
for(int j = 0; j < v.size(); ++j){
if(findF(v[j].id) == findF(idm)){
v[j].id = min(idm, v[j].id);
v[j].peoNum += peonum;
v[j].Sav += (double)setNum;
v[j].Aav += (double)area;
flag = true;
break;
}
}
if(!flag)v.push_back({idm, peonum , (double)setNum, (double)area});
}
//录入数据的时候只合并了一部分家庭,再合并一遍
v2.push_back(v[0]);
for(int i = 1; i < v.size(); ++i){
int flag = false;
for(int j = 0; j < v2.size(); ++j){
if(findF(v[i].id) == findF(v2[j].id)){
v2[j].id = min(v[i].id, v2[j].id);
v2[j].peoNum += v[i].peoNum;
v2[j].Sav += v[i].Sav;
v2[j].Aav += v[i].Aav;
flag = true;
break;
}
}
if(!flag)v2.push_back(v[i]);
}
cout<<v2.size()<<endl;
sort(v2.begin(), v2.end(), cmp);
for(int i = 0; i < v2.size(); ++i){
printf("%04d %d %.3f %.3f\n", v2[i].id, v2[i].peoNum, v2[i].Sav / v2[i].peoNum, v2[i].Aav / v2[i].peoNum);
}
return 0;
}
【刷题-PAT】A1114 Family Property (25 分)的更多相关文章
- PTA PAT排名汇总(25 分)
PAT排名汇总(25 分) 计算机程序设计能力考试(Programming Ability Test,简称PAT)旨在通过统一组织的在线考试及自动评测方法客观地评判考生的算法设计与程序设计实现能力,科 ...
- 【刷题-PAT】A1126 Eulerian Path (25 分)
1126 Eulerian Path (25 分) In graph theory, an Eulerian path is a path in a graph which visits every ...
- 【刷题-PAT】A1101 Quick Sort (25 分)
1101 Quick Sort (25 分) There is a classical process named partition in the famous quick sort algorit ...
- PAT 甲级 1029 Median (25 分)(思维题,找两个队列的中位数,没想到)*
1029 Median (25 分) Given an increasing sequence S of N integers, the median is the number at the m ...
- PAT 甲级 1070 Mooncake (25 分)(结构体排序,贪心,简单)
1070 Mooncake (25 分) Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autum ...
- PAT 甲级 1078 Hashing (25 分)(简单,平方二次探测)
1078 Hashing (25 分) The task of this problem is simple: insert a sequence of distinct positive int ...
- PAT 甲级 1032 Sharing (25 分)(结构体模拟链表,结构体的赋值是深拷贝)
1032 Sharing (25 分) To store English words, one method is to use linked lists and store a word let ...
- PAT 1051 Pop Sequence (25 分)
返回 1051 Pop Sequence (25 分) Given a stack which can keep M numbers at most. Push N numbers in the ...
- 【刷题-PAT】A1108 Finding Average (20 分)
1108 Finding Average (20 分) The basic task is simple: given N real numbers, you are supposed to calc ...
随机推荐
- 数据改变认知——不知怎么选,用RFM模型看舔狗质量!
假设我长得很漂亮,拥有众多追求者,但是初出闺房的我对这世界上的男人毫无认知,那么该如何选择呢?这真是一个问题! 妈妈说,愿意为我花钱的男人未必爱我,但不愿意为我花钱的男人必定不爱我,而后传授了一套RF ...
- C++ 获取函数耗时
C++ 记录耗时 #include <sys/timeb.h> #include <stdio.h> long long getSystemTime() { struct ti ...
- 【LeetCode】163. Missing Ranges 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...
- 【LeetCode】279. Perfect Squares 解题报告(C++ & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 四平方和定理 动态规划 日期 题目地址:https: ...
- 【九度OJ】题目1202:排序 解题报告
[九度OJ]题目1202:排序 解题报告 标签(空格分隔): 九度OJ [LeetCode] http://ac.jobdu.com/problem.php?pid=1202 题目描述: 对输入的n个 ...
- Web 网站进化
01-初始阶段 应用程序.数据库.文件等所有资源都在一台服务器上 02-应用服务与数据服务分离 应用服务器 要处理大量的业务逻辑,所以需要更好更快更强大的 CPU 数据库服务器 需要快速的进行磁盘 ...
- 编写Java程序,比较两个Dog对象是否为同一个对象
返回本章节 返回作业目录 需求说明: 重写Dog类的equals(Object obj)方法. 如果equals(Object obj)中obj为Dog类型,则判断当前 对象的dogName与obj对 ...
- Hadoop编译打包记录
Hadoop编译打包,基于2.7.2版本的源码. # 打包过程中需要使用到的工具 java -version mvn -version ant -version type protoc type cm ...
- Hbase集群安装Version1.1.5
Hbase集群安装,基于版本1.1.5, 使用hbase-1.1.5.tar.gz安装包. 1.安装说明 使用外部Zookeeper集群而非Hbase自带zookeeper, 使用Hadoop文件系统 ...
- SpringBoot 与 SpringCloud 的版本对应详细信息
"spring-cloud": { "Finchley.M2": "Spring Boot >=2.0.0.M3 and <2.0.0.M ...