当前位置:网站首页>L2-007 家庭房产 (25 分)
L2-007 家庭房产 (25 分)
2022-07-06 09:14:00 【%xiao Q】
题目
给定每个人的家庭成员和其自己名下的房产,请你统计出每个家庭的人口数、人均房产面积及房产套数。
输入格式:
输入第一行给出一个正整数N(≤1000),随后N行,每行按下列格式给出一个人的房产:
编号 父 母 k 孩子1 … 孩子k 房产套数 总面积
其中编号是每个人独有的一个4位数的编号;父和母分别是该编号对应的这个人的父母的编号(如果已经过世,则显示-1);k(0≤k≤5)是该人的子女的个数;孩子i是其子女的编号。
输出格式:
首先在第一行输出家庭个数(所有有亲属关系的人都属于同一个家庭)。随后按下列格式输出每个家庭的信息:
家庭成员的最小编号 家庭人口数 人均房产套数 人均房产面积
其中人均值要求保留小数点后3位。家庭信息首先按人均面积降序输出,若有并列,则按成员编号的升序输出。
输入样例:
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
分析
一道并查集的题目,详情请看代码!!!
参考代码:
#include <iostream>
#include <cstdio>
#include <set>
#include <vector>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <algorithm>
#include <unordered_map>
#define LL long long
#define rep(i, a, b) for(int i = a; i <= b; i++)
#define reps(i, a, b) for(int i = a; i < b; i++)
#define pre(i, a, b) for(int i = b; i >= a; i--)
using namespace std;
const int N = 1010, M = 1e4;
int n;
bool st[M]; // 标记那些id出现过
int p[M];
struct data
{
int id, num, area;
}a[N];
struct stu
{
int id, people;
double num, area;
bool flag;
}ans[M];
// 寻找父节点
int find(int x)
{
while(x != p[x]) x = p[x];
return x;
}
// 合并2个子树
void Union(int x, int y)
{
int fx = find(x);
int fy = find(y);
if(fx > fy) p[fx] = fy;
else p[fy] = fx;
}
bool cmp(stu x, stu y)
{
if(x.area != y.area) return x.area > y.area;
return x.id < y.id;
}
int main()
{
cin >> n;
rep(i, 0, M) p[i] = i; //初始化所有的点的父节点为自己
rep(i, 1, n)
{
int id, d_id, m_id, k, son, num, area;
cin >> id >> d_id >> m_id;
st[id] = true;
if(d_id != -1)
{
st[d_id] = true;
Union(id, d_id);
}
if(m_id != -1)
{
st[m_id] = true;
Union(id, m_id);
}
cin >> k;
while(k--)
{
cin >> son;
st[son] = true;
Union(id, son);
}
cin >> num >> area;
a[i] = {
id, num, area};
}
rep(i, 1, n)
{
int id = find(a[i].id);
ans[id].id = id;
ans[id].num += a[i].num;
ans[id].area += a[i].area;
ans[id].flag = true; // 标记家族个数
}
int cnt = 0;
// 因为id数据范围不大,直接枚举所有可能出现的4为数id,并利用怕并查集来累加家族人群
rep(i, 0, 9999)
{
if(st[i]) ans[find(i)].people++;
if(ans[i].flag) cnt++;
}
rep(i, 0, 9999)
if(ans[i].flag)
{
ans[i].num = ans[i].num / ans[i].people;
ans[i].area = ans[i].area / ans[i].people;
}
sort(ans, ans + M, cmp);
cout << cnt << endl;
// 因为是按平均面积来降序输出的,所以可以直接排完序直接输出
rep(i, 0, cnt - 1)
printf("%04d %d %.3f %.3f\n", ans[i].id, ans[i].people, ans[i].num, ans[i].area);
return 0;
}
边栏推荐
- 基于apache-jena的知识问答
- Tcp/ip protocol (UDP)
- Are you monitored by the company for sending resumes and logging in to job search websites? Deeply convinced that the product of "behavior awareness system ba" has not been retrieved on the official w
- QT creator specifies dependencies
- 02-项目实战之后台员工信息管理
- QT creator uses Valgrind code analysis tool
- AI benchmark V5 ranking
- Copie maître - esclave MySQL, séparation lecture - écriture
- csdn-Markdown编辑器
- Ansible practical Series III_ Task common commands
猜你喜欢
AcWing 1298.曹冲养猪 题解
QT creator specify editor settings
[recommended by bloggers] C WinForm regularly sends email (with source code)
QT creator custom build process
CSDN question and answer module Title Recommendation task (II) -- effect optimization
Csdn-nlp: difficulty level classification of blog posts based on skill tree and weak supervised learning (I)
Redis的基础使用
La table d'exportation Navicat génère un fichier PDM
Install mysql5.5 and mysql8.0 under windows at the same time
[recommended by bloggers] background management system of SSM framework (with source code)
随机推荐
Machine learning -- census data analysis
Pytorch基础
Ansible实战系列一 _ 入门
[C language foundation] 04 judgment and circulation
记一次某公司面试题:合并有序数组
Use dapr to shorten software development cycle and improve production efficiency
Error connecting to MySQL database: 2059 - authentication plugin 'caching_ sha2_ The solution of 'password'
AcWing 1294.樱花 题解
自动机器学习框架介绍与使用(flaml、h2o)
Classes in C #
Deoldify项目问题——OMP:Error#15:Initializing libiomp5md.dll,but found libiomp5md.dll already initialized.
Installation and use of MySQL under MySQL 19 Linux
JDBC principle
Test objects involved in safety test
[free setup] asp Net online course selection system design and Implementation (source code +lunwen)
Asp access Shaoxing tourism graduation design website
frp内网穿透那些事
Why is MySQL still slow to query when indexing is used?
Generate PDM file from Navicat export table
PyCharm中无法调用numpy,报错ModuleNotFoundError: No module named ‘numpy‘