当前位置:网站首页>L2-007 家庭房产(vector、set、map的使用)
L2-007 家庭房产(vector、set、map的使用)
2022-07-30 14:49:00 【真题OK撒】
给定每个人的家庭成员和其自己名下的房产,请你统计出每个家庭的人口数、人均房产面积及房产套数。
输入格式:
输入第一行给出一个正整数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 <set>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;
const int N = 1e4 + 10;
// 存储开始输入的基本信息
struct node
{
int id, house;
double area;
};
// 输出的家庭结构体
struct family
{
int id, number;
double house, area;
};
// 自定义比较函数
bool cmp(family a, family b)
{
if (a.area != b.area) return a.area > b.area;
return a.id < b.id;
}
int n, x, p[N];
set<int> s;// 存储每个人的编号、默认升序
map<int, node> nodes; // 记录人的房产信息
// 并查集模板、查找
int find(int x)
{
if (p[x] != x) p[x] = find(p[x]);
return p[x];
}
// 并查集模板、合并
void add(int x, int y)
{
if (find(x) != find(y)) p[find(y)] = find(x);
}
int main()
{
scanf("%d", &n);
for (int i = 0; i < N; i ++) p[i] = i;
for (int i = 0; i < n; i ++)
{
// 分别记录编号 编号 父 母 k 孩子1 ... 孩子k 房产套数 总面积
int id, p1, p2, k, house, id1;
double area; // 总面积
cin >> id >> p1 >> p2 >> k;
s.insert(id);
if (p1 != -1) s.insert(p1), add(id, p1);
if (p2 != -1) s.insert(p2), add(id, p2);
for (int j = 0; j < k; j ++)
cin >> id1, s.insert(id1), add(id, id1);
cin >> house >> area;
nodes[id] = {id, house, area}; // 记录该人的家庭信息
}
set<int> s1; // 记录家庭编号(可以理解为记录每个家庭的老大)
map<int, vector<int>> m; // 记录家庭编号和成员编号
for (auto it = s.begin(); it != s.end(); it ++)
{
int fa = find(*it);
m[fa].push_back(*it);
s1.insert(fa);
}
cout << s1.size() << endl;
vector<family> v1;
for (auto it = s1.begin(); it != s1.end(); it ++)
{
int fa = *it;
double house = 0, area = 0;
// 将每个家庭的成员及其房产计算汇总,放到 family 结构体中
for (auto tmp = 0; tmp < m[fa].size(); tmp ++)
house += nodes[m[fa][tmp]].house, area += nodes[m[fa][tmp]].area;
v1.push_back(family{ m[fa][0], int(m[fa].size()), house * 1.0 / (int)m[fa].size(), area * 1.0 / (int)m[fa].size()});
}
// 进行排序
sort(v1.begin(), v1.end(), cmp);
for (int i = 0; i < v1.size(); i ++)
printf("%04d %d %.3lf %.3lf\n", v1[i].id, v1[i].number, v1[i].house, v1[i].area);
return 0;
}边栏推荐
- B+树索引页大小是如何确定的?
- 《二舅》刷屏了!
- tiup install
- ECCV2022 | FPN错位对齐,实现高效半监督目标检测 (PseCo)
- Go to Tencent for an interview and let people turn left directly: I don't know idempotency!
- Flink本地UI运行
- Excel uses Visual Basic Editor to modify macros
- Sentinel
- How do luxury giants such as GUCCI and LV deploy the metaverse, should other brands keep up?
- How is the B+ tree index page size determined?
猜你喜欢

HTTP缓存小结

【重磅来袭】教你如何在RGBD三维重建中获取高质量模型纹理

canal scrape data

952. 按公因数计算最大组件大小 : 枚举质因数 + 并查集运用题

(Crypto必备干货)详细分析目前NFT的几大交易市场

Excel使用Visual Basic Editor对宏进行修改
4 senior experts share the insider architecture design and implementation principles of Flink technology with years of experience in large factories

JVM性能调优

Alluxio为Presto赋能跨云的自助服务能力

Distributed pre-course: MySQL implements distributed locks
随机推荐
【云原生】阿里云ARMS业务实时监控
数字量输入模块io
CMake库搜索函数居然不搜索LD_LIBRARY_PATH
微服务该如何拆分?
Use of InputStream and OutputStream
Go to Tencent for an interview and let people turn left directly: I don't know idempotency!
哨兵
Could not acquire management access for administration
How is the B+ tree index page size determined?
golang modules初始化项目
【云原生 • DevOps】influxDB、cAdvisor、Grafana 工具使用详解
基于5G的仓储信息化解决方案2022
视频加密的误解
How do luxury giants such as GUCCI and LV deploy the metaverse, should other brands keep up?
四大首搭加持,美学、安全、操控、效率优势明显,比亚迪海豹售价20.98万元起售!
Flink real-time data warehouse completed
How to split microservices?
1222. 可以攻击国王的皇后-力扣双百代码
定时任务 corn
软件包 - 笔记