PAT L2-007 家庭房产
https://pintia.cn/problem-sets/994805046380707840/problems/994805068539215872
给定每个人的家庭成员和其自己名下的房产,请你统计出每个家庭的人口数、人均房产面积及房产套数。
输入格式:
输入第一行给出一个正整数N(≤),随后N行,每行按下列格式给出一个人的房产:
编号 父 母 k 孩子1 ... 孩子k 房产套数 总面积
其中编号是每个人独有的一个4位数的编号;父和母分别是该编号对应的这个人的父母的编号(如果已经过世,则显示-1);k(0k≤)是该人的子女的个数;孩子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 <bits/stdc++.h>
using namespace std; const int maxn = 1e5 + 10;
int N;
int f[maxn], vis[maxn];
vector<int> peo; struct Node{
double square = 0.0;
vector<int> v;
vector<int> vv;
double ave;
int fir;
double num = 0;
}node[maxn], line[maxn]; bool cmp(const Node &a, const Node &b) {
if((a.square / (1.0 * a.vv.size())) != (b.square / (1.0 * b.vv.size())))
return (a.square / (1.0 * a.vv.size())) > (b.square / (1.0 * b.vv.size()));
else return a.v[0] < b.v[0];
} void init() {
for(int i = 0; i < maxn; i ++)
f[i] = i;
} int Find(int x) {
if(f[x] != x) f[x] = Find(f[x]);
return f[x];
} void Merge(int x, int y) {
int fx = Find(x);
int fy = Find(y); if(fx != fy)
f[fx] = fy;
} int main() {
scanf("%d", &N);
init();
for(int i = 0; i < N; i ++) {
int man, fa, mot;
scanf("%d%d%d", &man, &fa, &mot);
if(fa != -1) Merge(man, fa);
if(mot != -1) Merge(man, mot); if(!vis[man]) peo.push_back(man), vis[man] = 1;
if(!vis[fa] && fa != -1) peo.push_back(fa), vis[fa] = 1;
if(!vis[mot] && mot != -1) peo.push_back(mot), vis[mot] = 1; int K;
scanf("%d", &K);
if(K) {
for(int k = 0; k < K; k ++) {
int x;
scanf("%d", &x);
if(!vis[x]) peo.push_back(x), vis[x] = 1;
Merge(man, x);
}
} double T, S;
cin >> T >> S;
node[man].num = T, node[man].square = S;
} int cnt = 0;
for(int i = 0; i < peo.size(); i ++) {
if(f[peo[i]] == peo[i])
cnt ++; line[Find(peo[i])].num += node[peo[i]].num;
line[Find(peo[i])].square += node[peo[i]].square;
line[Find(peo[i])].vv.push_back(peo[i]);
if(line[Find(peo[i])].v.size() == 0) line[Find(peo[i])].v.push_back(peo[i]);
else {
if(peo[i] < line[Find(peo[i])].v[0]) {
line[Find(peo[i])].v.pop_back();
line[Find(peo[i])].v.push_back(peo[i]);
}
} } printf("%d\n", cnt); sort(line, line + 10000, cmp); vector<Node> ans; for(int i = 0; i < 10000; i ++) {
if(line[i].v.size() == 0) continue;
ans.push_back(line[i]);
} sort(ans.begin(), ans.end(), cmp);
for(int i = 0; i < ans.size(); i ++)
printf("%04d %d %.3lf %.3lf\n", ans[i].v[0], ans[i].vv.size(), 1.0 * ans[i].num / ans[i].vv.size(), ans[i].square / (1.0 * ans[i].vv.size())); return 0;
}
debug 一个小时 真的是要死人了 希望比赛的时候不要这样啊啊啊啊 才 25 分的题啊
PAT L2-007 家庭房产的更多相关文章
- pat 团体天梯赛 L2-007. 家庭房产
L2-007. 家庭房产 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序给定每个人的家庭成员和其自己名下的房产,请你统计出每个家庭的人口数.人均房产面积及房产 ...
- 团体程序设计天梯赛-练习集L2-007. 家庭房产
L2-007. 家庭房产 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定每个人的家庭成员和其自己名下的房产,请你统计出每个 ...
- L2-007. 家庭房产
L2-007. 家庭房产 题目链接:https://www.patest.cn/contests/gplt/L2-007 并查集 初学,看这题的时候完全没有什么好的想法,参考了@yinzm的blog用 ...
- L2-007 家庭房产 (25 分)
L2-007 家庭房产 (25 分) 给定每个人的家庭成员和其自己名下的房产,请你统计出每个家庭的人口数.人均房产面积及房产套数. 输入格式: 输入第一行给出一个正整数N(≤),随后N行,每行按下 ...
- L2-007. 家庭房产(并查集)*
L2-007. 家庭房产 参考博客 #include <iostream> #include <cstdio> #include <cstring> #includ ...
- 天梯赛 L2-007. (并查集) 家庭房产
题目链接 题目描述 给定每个人的家庭成员和其自己名下的房产,请你统计出每个家庭的人口数.人均房产面积及房产套数. 输入格式: 输入第一行给出一个正整数N(<=1000),随后N行,每行按下列格式 ...
- PATL2-007. 家庭房产-并查集
L2-007. 家庭房产 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定每个人的家庭成员和其自己名下的房产,请你统计出每个 ...
- pat 团体赛练习题集 L2-007. 家庭房产
给定每个人的家庭成员和其自己名下的房产,请你统计出每个家庭的人口数.人均房产面积及房产套数. 输入格式: 输入第一行给出一个正整数N(<=1000),随后N行,每行按下列格式给出一个人的房产: ...
- PAT 天梯赛 L2-007. 家庭房产 【并查集】
题目链接 https://www.patest.cn/contests/gplt/L2-007 思路 将一个家庭里的所有人都并进去 然后最后查找的时候 找到所有同一个家庭的人,计算出人数,人均房产套数 ...
随机推荐
- Announcing the Updated NGINX and NGINX Plus Plug‑In for New Relic (Version 2)
In March, 2013 we released the first version of the “nginx web server” plug‑in for New Relic monitor ...
- Loj #6073.「2017 山东一轮集训 Day5」距离
Loj #6073.「2017 山东一轮集训 Day5」距离 Description 给定一棵 \(n\) 个点的边带权的树,以及一个排列$ p\(,有\)q $个询问,给定点 \(u, v, k\) ...
- centos7下安装docker(25docker swarm---replicated mode&global mode)
swarm可以在service创建或运行过程中灵活的通过--replicas调整容器的副本数量,内部调整调度器则会根据当前集群资源使用的情况在不同的node上启动或停止容器,这就是service默认的 ...
- UVA140-Bandwidth(搜索剪枝)
Problem UVA140-Bandwidth Time Limit: 3000 mSec Problem Description Given a graph (V, E) where V is ...
- Python之requests库
Request库 r = requests.get(url) 这个包括两个语句:Response.Request,我们重点来看一下Response Response包含了页面返回的所有信息,下面是它的 ...
- 利用filter替换字符串中的空格
s = "abc def ghi xy" print(','.join(filter(lambda x: x, s.split(' '))))
- python入门学习:7.函数
python入门学习:7.函数 关键点:函数 7.1 定义函数7.2 传递实参7.3 返回值7.4 传递列表7.5 传递任意数量的实参7.6 将函数存储在模块中 7.1 定义函数 使用关键字def ...
- Linux shell判断文件和文件夹是否存在(转发)
#!/bin/sh myPath="/var/log/httpd/" myFile="/var /log/httpd/access.log" #这里的-x 参数 ...
- 网站建设部署与发布--笔记4-部署mysql
部署MySQL 1.更新操作系统 $ yum update -y 2.安装mysql数据库,在CentOS 7.2 中,使用了mariadb替代了官方的mysql $ yum install mari ...
- ssh 免密码登录配置,及其原理
1.废话不多说,先上图 2. 典型的RSA非对称加密 RSA加密算法是一种典型的非对称加密算法,它基于大数的因式分解数学难题,它也是应用最广泛的非对称加密算法,于1978年由美国麻省理工学院(MI ...