PAT甲级——A1114 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 (≤). 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) is the number of children of this person; Childi'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 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.
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
两个代码,仅供参考:
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <cmath>
using namespace std;
struct Node
{
int ID, nums = ;
double ss = 0.0, aa = 0.0;
};
int n;
double num[] = { 0.0 }, area[] = { 0.0 };//房子数量//房子面积
int mark[];//标记属于哪个家族
vector<set<int>>sets();//记录每个家庭有多少人
vector<Node*>res;
void combin(int my, int other)//合并家族
{
for (auto ptr = sets[other].begin(); ptr != sets[other].end(); ++ptr)//将其他成员进行同化
{
mark[*ptr] = my;
sets[my].insert(*ptr);
}
sets[other].clear();//删除其他成员的标记
}
int main()
{
cin >> n;
fill(mark, mark + , -);
for (int i = ; i < n; ++i)
{
int my, parent, k, child, nn, aa;
cin >> my;
if (mark[my] == -)//还未标记是哪个家族
{
mark[my] = my;//就以自己为标记
sets[mark[my]].insert(my);
}
for (int i = ; i < ; ++i)//添加父母
{
cin >> parent;
if (parent == -)
continue;
if (mark[parent] == -)//家人未标记
{
mark[parent] = mark[my];
sets[mark[my]].insert(parent);
}
else if (mark[parent] != - && mark[parent] != mark[my])//家人与自己标记不同
combin(mark[my], mark[parent]);//将家人同化为自己标记
}
cin >> k;
while (k--)
{
cin >> child;
if (mark[child] == -)//孩子未标记
{
mark[child] = mark[my];
sets[mark[my]].insert(child);
}
else if (mark[child] != - && mark[child] != mark[my])//孩子与自己标记不同
combin(mark[my], mark[child]);//将孩子同化为自己标记
}
cin >> nn >> aa;
num[my] = nn;
area[my] = aa;
}
for (int i = ; i < sets.size(); ++i)
{
if (sets[i].empty())
continue;
Node* temp = new Node;
temp->ID = *sets[i].begin();
temp->nums = sets[i].size();
for (auto ptr = sets[i].begin(); ptr != sets[i].end(); ++ptr)
{
temp->aa += area[*ptr];
temp->ss += num[*ptr];
}
temp->ss /= temp->nums;
temp->aa /= temp->nums;
res.push_back(temp);
}
sort(res.begin(), res.end(), [](Node*a, Node*b) {
if (abs(a->aa - b->aa) < 0.0001)
return a->ID < b->ID;
else
return a->aa > b->aa; });
cout << res.size() << endl;
for (auto v : res)
printf("%04d %d %0.3f %0.3f\n", v->ID, v->nums, v->ss, v->aa);
return ;
}
#include<bits/stdc++.h>
using namespace std;
struct Estate{//存储集合最小id、集合结点个数num、集合总sets、集合总area
int id,num=;
double sets=0.0,area=0.0;
};
bool cmp(const Estate&e1,const Estate&e2){//比较函数
return e1.area!=e2.area?e1.area>e2.area:e1.id<e2.id;
}
int father[];//并查集底层数组
int findFather(int x){//查找根节点
if(x==father[x])
return x;
int temp=findFather(father[x]);
father[x]=temp;
return temp;
}
void unionSet(int a,int b){//合并两个集合
int ua=findFather(a),ub=findFather(b);
if(ua!=ub)
father[max(ua,ub)]=min(ua,ub);//以小的id作为根节点
}
unordered_map<int,pair<double,double>>idEstate;
unordered_map<int,Estate>familyEstate;
int main(){
int N;
scanf("%d",&N);
iota(father,father+,);
while(N--){//读取数据并合并相关集合
int id,f,m,k,a;
scanf("%d%d%d%d",&id,&f,&m,&k);
if(f!=-)
unionSet(id,f);
if(m!=-)
unionSet(id,m);
while(k--){
scanf("%d",&a);
unionSet(id,a);
}
scanf("%lf%lf",&idEstate[id].first,&idEstate[id].second);//记录id和对应的sets、area
}
for(int i=;i<;++i){//遍历并查集数组
int temp=findFather(i);
if(temp!=i)//根节点不等于本身
++familyEstate[temp].num;//递增根节点下集合的结点个数
if(idEstate.find(i)!=idEstate.cend()){//累加集合的总sets、总area
familyEstate[temp].sets+=idEstate[i].first;
familyEstate[temp].area+=idEstate[i].second;
}
}
vector<Estate>v;
for(auto i=familyEstate.begin();i!=familyEstate.end();++i){//将familyEstate中的值搬迁到vector中,并更新相关信息
(i->second).id=i->first;
++(i->second).num;//集合下结点个数没有统计根节点,所以要+1
(i->second).sets/=(i->second).num;
(i->second).area/=(i->second).num;
v.push_back(i->second);
}
sort(v.begin(),v.end(),cmp);//排序
printf("%d\n",v.size());
for(int i=;i<v.size();++i)
printf("%04d %d %.3f %.3f\n",v[i].id,v[i].num,v[i].sets,v[i].area);
return ;
}
PAT甲级——A1114 Family Property【25】的更多相关文章
- PAT甲级1114. Family Property
PAT甲级1114. Family Property 题意: 这一次,你应该帮我们收集家族财产的数据.鉴于每个人的家庭成员和他/她自己的名字的房地产(房产)信息,我们需要知道每个家庭的规模,以及他们的 ...
- PAT甲级——1114 Family Property (并查集)
此文章同步发布在我的CSDN上https://blog.csdn.net/weixin_44385565/article/details/89930332 1114 Family Property ( ...
- 【PAT甲级】1070 Mooncake (25 分)(贪心水中水)
题意: 输入两个正整数N和M(存疑M是否为整数,N<=1000,M<=500)表示月饼的种数和市场对于月饼的最大需求,接着输入N个正整数表示某种月饼的库存,再输入N个正数表示某种月饼库存全 ...
- 【刷题-PAT】A1114 Family Property (25 分)
1114 Family Property (25 分) This time, you are supposed to help us collect the data for family-owned ...
- PAT甲级 1122. Hamiltonian Cycle (25)
1122. Hamiltonian Cycle (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The ...
- PAT甲级 1121. Damn Single (25)
1121. Damn Single (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue "Dam ...
- PAT甲级 1126. Eulerian Path (25)
1126. Eulerian Path (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue In grap ...
- PAT甲级 1130. Infix Expression (25)
1130. Infix Expression (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Give ...
- PAT甲级 1129. Recommendation System (25)
1129. Recommendation System (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
随机推荐
- Linux下安装SkyWalking 6.1版本 以及.NETCore项目集成
SkyWalking和APM介绍 今天给大家给大家介绍一下SkyWalking,那什么是SkyWalking Skywalking 是 Apache 基金会下面的一个开源 APM 项目 ,那什么又是A ...
- 41. wait notify 方法
wait() 等待,如果一个线程执行了wait方法,那么该线程就会进去一个以锁对象为标识符的线程池中等待 notity() 唤醒,如果一个线程执行了notity方法,那么就会唤醒以锁对象为标识符的线 ...
- Vue Router基础
路由 安装 vue-router 起步 <router-link to="/foo">Go to Foo</router-link> <router- ...
- EF批量添加数据之修改SQL Server执行上限
asp.net core 项目 打开Startup.cs services.AddDbContext<MyContext>( options => { options.UseSqlS ...
- TTreeView、TTreeNodes和TTreeNode
TreeView是Delphi中使用频率比较高的一个控件,虽然使用次数很多,但总结不够.借着这次做GDW原型的机会总结一下,写的过程中也会参考网上的博文. TTreeView.TTreeNodes和T ...
- delphi Sqlite
Delphi中SQLite如何读写二进制字段(Blob类型) 在Delphi中,有大量的组件可以操作SQLite数据库,如UniDAC就是其中一个比较优秀的,当然还有ASQLite3Component ...
- NSDateFormatter 今年日期格式化成字符串是明年日期问题?
在项目里我要是把NSDate格式化成字符串 我的format是@"YYYY年MM月dd日 HH:mm" 传入日期2013-12-30 15:00:00后,返回给我的字符串是 201 ...
- sizeof,真正终结版GCC与VC
在VC6.0中sizeof结果是16.我电脑上装了个linux虚拟机,在虚拟机上GCC中结果是12, 恩不同编译器默认对齐数值不一样. VC 默认为 8 gcc 默认为 4 有个编译参数控制对齐. # ...
- 第37讲 谈谈Spring Bean的生命周期和作用域
在企业应用软件开发中,Java 是毫无争议的主流语言,开放的 Java EE 规范和强大的开源框架功不可没,其中 Spring 毫无疑问已经成为企业软件开发的事实标准之一.今天这一讲,我将补充 Spr ...
- PAT_A1086#Tree Traversals Again
Source: PAT A1086 Tree Traversals Again (25 分) Description: An inorder binary tree traversal can be ...