首页 > 其他分享 >1114 Family Property

1114 Family Property

时间:2024-02-06 15:44:50浏览次数:18  
标签:housholders Family family int average 1114 Property id areas

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 Child_1 ⋯ Child_k M_estate 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; Child_i's are the ID's of his/her children; M_estate 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 AVG_sets AVG_area

where ID is the smallest ID in the family; M is the total number of family members; AVG_sets is the average number of sets of their real estate; and AVG_area 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

Solution

Code

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int maxn = 10010;

int g_housholders[maxn] = {0};
int g_estates[maxn] = {0};
int g_areas[maxn] = {0};
int n;

struct Family
{
    int m_family_number = 0;
    int m_total_estates = 0;
    int m_total_areas = 0;
    int m_housholdersID = -1;

    double m_average_estates = 0.0;
    double m_average_areas = 0.0;
}family[maxn];

void init(int id)
{
    if(g_housholders[id] == -1) {
        g_housholders[id] = id;
    }
}

int findHousemaster(int x)
{
    int housemaster = x;
    while (housemaster != g_housholders[housemaster])
    {
        housemaster = g_housholders[housemaster];
    }

    while (x != g_housholders[x])
    {
        int f = x;
        x = g_housholders[x];
        g_housholders[f] = housemaster;
    }
    
    return housemaster;
}

void unionHouseholder(int a, int b)
{
    int faA = findHousemaster(a);
    int faB = findHousemaster(b);
    if(faA != faB) {
        int min = faA > faB ? faB : faA;
        int max = faA + faB - min;
        g_housholders[max] = min;
    }
}

bool cmp(Family a, Family b)
{
    if(a.m_average_areas != b.m_average_areas) {
        return a.m_average_areas > b.m_average_areas;
    }
    else {
        return a.m_housholdersID < b.m_housholdersID;
    }
}

int main()
{
    memset(g_housholders, -1, sizeof(g_housholders));
    scanf("%d", &n);

    int id, fatherId, motherId;
    int k, childId;
    for(int i = 0; i < n; ++i) {
        scanf("%d %d %d %d", &id, &fatherId, &motherId, &k);
        if(g_housholders[id] == -1) {
            g_housholders[id] = id;
        }

        if(fatherId != -1) {
            init(fatherId);
            unionHouseholder(id, fatherId);
        }
        if(motherId != -1) {
            init(motherId);
            unionHouseholder(id, motherId);
        }

        for(int j = 0; j < k; ++j) {
            scanf("%d", &childId);
            init(childId);
            unionHouseholder(id, childId);
        }

        scanf("%d %d", &g_estates[id], &g_areas[id]);
    }

    for(int i = 0; i < maxn; ++i) {
        if(g_housholders[i] != -1) {
            int housholdersId = findHousemaster(i);
            family[housholdersId].m_housholdersID = housholdersId;
            family[housholdersId].m_family_number++;
            family[housholdersId].m_total_estates += g_estates[i];
            family[housholdersId].m_total_areas += g_areas[i];
        }
    }

    int total_family = 0;
    for(int i = 0; i < maxn; ++i){
        if(family[i].m_housholdersID != -1) {
            total_family++;
            family[i].m_average_estates = family[i].m_total_estates*1.0 / family[i].m_family_number;
            family[i].m_average_areas = family[i].m_total_areas*1.0 / family[i].m_family_number;
        }
    }

    sort(family, family+maxn, cmp);
    printf("%d\n", total_family);

    for(int i = 0; i < total_family; ++i) {
        printf("%04d %d %.3f %.3f\n", family[i].m_housholdersID
                                , family[i].m_family_number
                                , family[i].m_average_estates
                                , family[i].m_average_areas);
    }

    return 0;
}

标签:housholders,Family,family,int,average,1114,Property,id,areas
From: https://www.cnblogs.com/wanghao-boke/p/18009830

相关文章

  • P1114 “非常男女”计划
    原题链接这道题是前缀和的简单应用。我们可以将男生看为1,女生看为-1。那么题目要求的最长子数组的判断条件为该数组和是否为0。首先我们对整个数组进行前缀和;接下来假定该最长子数组在right位置(right进行遍历)结束,那么就有两种情况讨论:1、该位置前缀和为0,那么与max进行比较。......
  • 关于easyExcel解析未添加@ExcelProperty报错问题分析
    在一次做辅料商品导出列表的需求,并且上线之后发现,怎么商品列表的导出没有反应,一看日志,发现报错了:这里新加了两个字段用于做转换使用。因为之前很少用easyExcel,所以以为只要不加@ExcelProperty,easyExcel就不会去解析字段,没想到easyExcel还是去做了解析。源码分析通过上面的......
  • Yield Keyword, classmethod and static method, and Property Method in Python
    ReferenceWhatisYieldKeywordinPythonPython'syieldkeywordislikeanotheroneweusetoreturnanexpressionorobject,typicallyinfunctions,calledreturn.Thereisasmallamountoffluctuation,though.Theyieldstatementofafunctionre......
  • wpf 数据绑定 INotifyPropertyChanged封装
    BindableBase.cspublicabstractclassBindableBase:INotifyPropertyChanged{publiceventPropertyChangedEventHandlerPropertyChanged;//调用方法:publicstringName{get=>name;set{SetProperty<string>(refname,value);}}......
  • PPO算法——PPOxFamily
    1.决策智能目的就是搜索最优解,方法主要有两种:从模仿中学习、从试错中学习从模仿中学习通过棋谱来学棋优势:简洁直观劣势:数据要求高,可迁移性差从试错中学习通过对弈来学习优势:可以不断提升和强化劣势:过程复杂,效率和稳定性有待提高深度强化学习——更强大、更通用、更稳定......
  • 序列化之@JsonComponent、@JsonInclude、@JsonSerialize、@JsonIgnore、JsonProperty
    前言:很多时候,例如前端需要字段user可能只是需要用到user中的userName属性,而后端传过去的却是一整个user对象,这样显然是不行的。那有没有一种技术,可以把后端传给前端的user类型的值改变为userName类型的值呢?@JsonComponent、@JsonInclude、@JsonSerialize可以在序列化的时候动手脚,可......
  • android开发编译出错:Unable to find method ''org.gradle.api.file.RegularFileProper
    Unabletofindmethod''org.gradle.api.file.RegularFilePropertyorg.gradle.api.file.ProjectLayout.fileProperty(org.gradle.api.provider.Provider)'''org.gradle.api.file.RegularFilePropertyorg.gradle.api.file.ProjectLayout.fileProp......
  • C# .net中PropertyDescriptor的使用和BindingList的ApplySort排序
    找了好多资料都是java中PropertyDescriptor的使用,至于C#中的都抄袭别人的,又讲不清楚怎么用。官方文档也没不会手把手教你怎么用,经过一下午的研究,结果如下1、找到PropertyDescriptor同一dll下的,使用TypeDescriptor反射出属性的PropertyDescriptorCollection,从这里拿出对应属性的P......
  • Python中的@property
      在Python中,@property是一种装饰器,用于将一个方法转换成只读属性。通过使用@property装饰器,你可以定义一个类的方法,使其在访问时可以像访问属性一样,而不是通过方法调用。  下面是一个简单的例子来说明@property的使用:class Circle:    def __init__(self, ra......
  • Attribute 和 Property 的区别
    Attribute和Property的区别在阅读源码文档时,经常会看到Attribute和Property这两个词。中文直译是相同的,这就导致了概念的混淆。因此有必要区分这两者。Property在英语里有财产的含义,一般指对象的组成部分,可以是简单数据也可以是对象或对象集合.Attribute多指一个对......