1114 Family Property (PAT甲级)

这篇具有很好参考价值的文章主要介绍了1114 Family Property (PAT甲级)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

#include <cstdio>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
const int MAXN = 10000;

struct node{
    int id;
    int memberNbr = 1;
    int setNbr;
    int area;
    node(int _id, int _setNbr, int _area): id(_id), setNbr(_setNbr), area(_area){}
};

int N, id, father, mother, k, t, M, area, cnt;
int fa[MAXN];
std::map<int, int> mpSetNbr, mpArea, mpCnt;
std::set<int> st;
std::vector<node> vec;

int findFather(int x){
    int k = x;
    while(fa[x] != x){
        x = fa[x];
    }
    int t;
    while(fa[k] != k){
        t = fa[k];
        fa[k] = x;
        k = t;
    }
    return x;
}

void Union(int a, int b){
    int faA = findFather(a);
    int faB = findFather(b);
    if(faA < faB){
        fa[faB] = faA;
    } else{
        fa[faA] = faB;
    }
}

bool cmp(const node &a, const node &b){
    double avgA = a.area * 1.0 / a.memberNbr;
    double avgB = b.area * 1.0 / b.memberNbr;
    return avgA != avgB ? avgA > avgB : a.id < b.id;
}

int main(){
    for(int i = 0; i < MAXN; ++i){
        fa[i] = i;
    }
    scanf("%d", &N);
    for(int i = 0; i < N; ++i){
        scanf("%d %d %d %d", &id, &father, &mother, &k);
        st.insert(id);
        if(father != -1){
            Union(id, father);
            st.insert(father);
        }
        if(mother != -1){
            Union(id, mother);
            st.insert(mother);
        }
        for(int j = 0; j < k; ++j){
            scanf("%d", &t);
            Union(id, t);
            st.insert(t);
        }
        scanf("%d %d", &M, &area);
        mpSetNbr[id] = M;
        mpArea[id] = area;
    }
    cnt = 0;
    for(auto it = st.begin(); it != st.end(); ++it){
        if(*it == findFather(*it)){
            vec.push_back(node(*it, mpSetNbr[*it], mpArea[*it]));
            mpCnt.insert({*it, cnt++});
        } else{
            int i = mpCnt[findFather(*it)];
            vec[i].memberNbr++;
            vec[i].setNbr += mpSetNbr[*it];
            vec[i].area += mpArea[*it];
        }
    }
    sort(vec.begin(), vec.end(), cmp);
    printf("%d\n", vec.size());
    for(int i = 0; i < vec.size(); ++i){
        printf("%04d %d %.3f %.3f\n", vec[i].id, vec[i].memberNbr,
        vec[i].setNbr * 1.0 / vec[i].memberNbr, vec[i].area * 1.0 / vec[i].memberNbr);
    }
    return 0;
}

题目如下:

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 (≤1000). 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≤k≤5) 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.文章来源地址https://www.toymoban.com/news/detail-459800.html

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

到了这里,关于1114 Family Property (PAT甲级)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 菜鸟记录PAT甲级1003--Emergency

    久违的PAT,由于考研408数据结构中有一定需要,同时也是对先前所遗留的竞赛遗憾进行一定弥补 ,再次继续PAT甲级1003.。 As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the l

    2023年04月13日
    浏览(42)
  • 1111 Online Map (PAT甲级)

    这道题我读题不仔细导致踩了个大坑,一个测试点过不了卡了好几个小时:第二个dijkstra算法中,题目要求是“In case the fastest path is not unique, output the one that passes through the fewest intersections”,我却想当然地认为在fastest path is not unique的时候,判断标准是最短距离…… Input our

    2024年02月07日
    浏览(42)
  • pat甲级 1022 Digital Library

    A Digital Library contains millions of books, stored according to their titles, authors, key words of their abstracts, publishers, and published years. Each book is assigned an unique 7-digit number as its ID. Given any query from a reader, you are supposed to output the resulting books, sorted in increasing order of their ID\\\'s. Input Specification: Each inp

    2024年04月15日
    浏览(35)
  • 1083 List Grades (PAT甲级)

    Given a list of N student records with name, ID and grade. You are supposed to sort the records with respect to the grade in non-increasing order, and output those student records of which the grades are in a given interval. Input Specification: Each input file contains one test case. Each case is given in the following format: where  name[i]  and  ID[i

    2024年02月08日
    浏览(48)
  • PAT甲级图论相关题目

    PAT甲级图论相关题目: 分数 25 As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the length of each road between any pair of cities are marked on the map. When there is an emergency call to you from some o

    2024年01月21日
    浏览(52)
  • 1021 Deepest Root (PAT甲级)

    1021. Deepest Root (25)-PAT甲级真题(图的遍历,dfs,连通分量的个数)_柳婼的博客-CSDN博客 柳婼的解法在这里,两次dfs,还是挺好玩的。 我的解法比较暴力,就是先用并查集算连通分量(这个其实还是dfs来算会更方便),如果只有一个连通分量,那deepest root一定在仅有一条arc的

    2024年02月15日
    浏览(32)
  • PAT 甲级【1007 Maximum Subsequence Sum】

    本题是考察动态规划与java的快速输入: max[i]表示第i个结尾的最大的连续子串和。b begin[i]表示第[begin[i],i]为最大和的开始位置 超时代码: 未超时: 能用动态规划解决的问题,需要满足三个条件:最优子结构,无后效性和子问题重叠。 具有最优子结构也可能是适合用贪心的

    2024年02月08日
    浏览(39)
  • 1074 Reversing Linked List (PAT甲级)

    Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements on L. For example, given L being 1→2→3→4→5→6, if K=3, then you must output 3→2→1→6→5→4; if K=4, you must output 4→3→2→1→5→6. Input Specification: Each input file contains one test case. For each case, the first line

    2024年02月09日
    浏览(41)
  • PAT 甲级1005【1005 Spell It Right】

    用JAVA可以用BigInteger解决。       太长不看版:结尾自取模板…… 高精度计算(Arbitrary-Precision Arithmetic),也被称作大整数(bignum)计算,运用了一些算法结构来支持更大整数间的运算(数字大小超过语言内建整型)。 高精度问题包含很多小的细节,实现上也有很多讲究。

    2024年02月08日
    浏览(85)
  • PAT (Advanced Level) 甲级 1004 Counting Leaves

    点此查看所有题目集 A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child.   Input Specification: Each input file contains one test case. Each case starts with a line containing 0N100, the number of nodes in a tree, and M (N), the number of non-leaf nodes. Then M lines follow, ea

    2024年02月12日
    浏览(40)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包