博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PAT甲级——A1114 Family Property【25】
阅读量:4541 次
发布时间:2019-06-08

本文共 5770 字,大约阅读时间需要 19 分钟。

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 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:

106666 5551 5552 1 7777 1 1001234 5678 9012 1 0002 2 3008888 -1 -1 0 1 10002468 0001 0004 1 2222 1 5007777 6666 -1 0 2 3003721 -1 -1 1 2333 2 1509012 -1 -1 3 1236 1235 1234 1 1001235 5678 9012 0 1 502222 1236 2468 2 6661 6662 1 3002333 -1 3721 3 6661 6662 6663 1 100

Sample Output:

38888 1 1.000 1000.0000001 15 0.600 100.0005551 4 0.750 100.000

两个代码,仅供参考:

1 #include 
2 #include
3 #include
4 #include
5 #include
6 using namespace std; 7 struct Node 8 { 9 int ID, nums = 0;10 double ss = 0.0, aa = 0.0;11 };12 int n;13 double num[10000] = { 0.0 }, area[10000] = { 0.0 };//房子数量//房子面积14 int mark[10000];//标记属于哪个家族15 vector
>sets(10000);//记录每个家庭有多少人16 vector
res;17 void combin(int my, int other)//合并家族18 {19 for (auto ptr = sets[other].begin(); ptr != sets[other].end(); ++ptr)//将其他成员进行同化20 {21 mark[*ptr] = my;22 sets[my].insert(*ptr);23 }24 sets[other].clear();//删除其他成员的标记25 }26 int main()27 { 28 cin >> n; 29 fill(mark, mark + 10000, -1);30 for (int i = 0; i < n; ++i)31 {32 int my, parent, k, child, nn, aa;33 cin >> my;34 if (mark[my] == -1)//还未标记是哪个家族35 {36 mark[my] = my;//就以自己为标记37 sets[mark[my]].insert(my);38 }39 for (int i = 0; i < 2; ++i)//添加父母40 {41 cin >> parent;42 if (parent == -1)43 continue;44 if (mark[parent] == -1)//家人未标记45 {46 mark[parent] = mark[my];47 sets[mark[my]].insert(parent);48 }49 else if (mark[parent] != -1 && mark[parent] != mark[my])//家人与自己标记不同50 combin(mark[my], mark[parent]);//将家人同化为自己标记51 }52 cin >> k;53 while (k--)54 {55 cin >> child;56 if (mark[child] == -1)//孩子未标记57 {58 mark[child] = mark[my];59 sets[mark[my]].insert(child);60 }61 else if (mark[child] != -1 && mark[child] != mark[my])//孩子与自己标记不同62 combin(mark[my], mark[child]);//将孩子同化为自己标记63 }64 cin >> nn >> aa;65 num[my] = nn;66 area[my] = aa;67 }68 for (int i = 0; i < sets.size(); ++i)69 {70 if (sets[i].empty())71 continue;72 Node* temp = new Node;73 temp->ID = *sets[i].begin();74 temp->nums = sets[i].size();75 for (auto ptr = sets[i].begin(); ptr != sets[i].end(); ++ptr)76 {77 temp->aa += area[*ptr];78 temp->ss += num[*ptr];79 }80 temp->ss /= temp->nums;81 temp->aa /= temp->nums;82 res.push_back(temp);83 }84 sort(res.begin(), res.end(), [](Node*a, Node*b) {85 if (abs(a->aa - b->aa) < 0.0001)86 return a->ID < b->ID;87 else88 return a->aa > b->aa; });89 cout << res.size() << endl;90 for (auto v : res)91 printf("%04d %d %0.3f %0.3f\n", v->ID, v->nums, v->ss, v->aa);92 return 0;93 }
1 #include
2 using namespace std; 3 struct Estate{
//存储集合最小id、集合结点个数num、集合总sets、集合总area 4 int id,num=0; 5 double sets=0.0,area=0.0; 6 }; 7 bool cmp(const Estate&e1,const Estate&e2){
//比较函数 8 return e1.area!=e2.area?e1.area>e2.area:e1.id
>idEstate;24 unordered_map
familyEstate;25 int main(){26 int N;27 scanf("%d",&N);28 iota(father,father+10005,0);29 while(N--){
//读取数据并合并相关集合30 int id,f,m,k,a;31 scanf("%d%d%d%d",&id,&f,&m,&k);32 if(f!=-1)33 unionSet(id,f);34 if(m!=-1)35 unionSet(id,m);36 while(k--){37 scanf("%d",&a);38 unionSet(id,a);39 }40 scanf("%lf%lf",&idEstate[id].first,&idEstate[id].second);//记录id和对应的sets、area41 }42 for(int i=0;i<10005;++i){
//遍历并查集数组43 int temp=findFather(i);44 if(temp!=i)//根节点不等于本身45 ++familyEstate[temp].num;//递增根节点下集合的结点个数46 if(idEstate.find(i)!=idEstate.cend()){
//累加集合的总sets、总area47 familyEstate[temp].sets+=idEstate[i].first;48 familyEstate[temp].area+=idEstate[i].second;49 }50 }51 vector
v;52 for(auto i=familyEstate.begin();i!=familyEstate.end();++i){
//将familyEstate中的值搬迁到vector中,并更新相关信息53 (i->second).id=i->first;54 ++(i->second).num;//集合下结点个数没有统计根节点,所以要+155 (i->second).sets/=(i->second).num;56 (i->second).area/=(i->second).num;57 v.push_back(i->second);58 }59 sort(v.begin(),v.end(),cmp);//排序60 printf("%d\n",v.size());61 for(int i=0;i

 

转载于:https://www.cnblogs.com/zzw1024/p/11456327.html

你可能感兴趣的文章
[AHOI 2009]chess 中国象棋
查看>>
UVA 11990 ”Dynamic“ Inversion(线段树+树状数组)
查看>>
Hibernate学习四----------Blob
查看>>
CTF-练习平台-Misc之 中国菜刀,不再web里?
查看>>
Mac系统配置JDK环境变量
查看>>
多项式累加
查看>>
剑指offer(18)二叉搜索树的后续遍历
查看>>
微信小程序一笔记账开发进度四
查看>>
bzoj 1070 费用流
查看>>
201671010139 徐楠 第四周总结
查看>>
JAVA链表简单实现
查看>>
[转载]T-SQL(MSSQL)语句查询执行顺序
查看>>
SignalR 行实时通信最大连接数
查看>>
开发进度6
查看>>
php方法重载
查看>>
三次握手和四次挥手(二)
查看>>
MySQL中的索引
查看>>
Android开发之手势滑动(滑动手势监听)详解
查看>>
switch
查看>>
HTTP错误code大全
查看>>