PAT 甲级 刷题日记|A 1114 Family Proper

2021-08-08  本文已影响0人  九除以三还是三哦

单词积累

decimal 小数的,十进制的

descending 下降的

ascending 上升的,增长的

the total number of sets of the real estate under his/her name 其名下房产的总套数

题目

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; child'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
结尾无空行

思路

很典型的并查集题目,就是元素过多,处理起来较麻烦。比起在union过程中处理元素值,更简单的做法是合并完全部遍历计算。

此外还涉及到了排序,格式化输出等知识点。

最坑的点是默认id编号是从1开始的,居然导致了三个样例出错,检查了半天,牢记不要想当然,题目不指名,就是从0开始编。

代码

#include <bits/stdc++.h>
using namespace std;

const int maxn = 10000;
int father[maxn];
int numbers[maxn];
int visit[maxn];
double sets[maxn];
double areas[maxn];

struct fam{
    int id;
    int number;
    double sets;
    double areas;
}Family[maxn];

bool cmp(fam a, fam b) {
    if (a.areas == b.areas) return a.id < b.id;
    else return a.areas > b.areas;
}

void inital() {
    for (int i = 0; i < maxn; i++) {
        father[i] = i;
        numbers[i] = 1;
        visit[i] = 0;
        sets[i] = 0;
        areas[i] = 0;
    }
    return;
}

int findfather(int x) {
    while (x != father[x]) {
        x = father[x];
    }
    return x;
}

void Union(int a, int b) {
    visit[a] = 1;
    visit[b] = 1;
    int x = findfather(a);
    int y = findfather(b);
    if (x == y) return ;
    else if (x < y) {
        father[y] = x;
    } else if (x > y) {
        father[x] = y;
    }
    return ;
}

int main() {
    inital(); 
    int N;
    cin>>N;
    for (int i = 0; i < N; i++) {
        int id, idf, idm, idnc;
        double estate, area;
        cin>>id>>idf>>idm>>idnc;
        if (idf != -1) Union(id, idf);
        if (idm != -1) Union(id, idm);
        int idc[6];
        for (int j = 0; j < idnc; j++) {
            cin>>idc[j];
            Union(id, idc[j]);
        }
        cin>>estate>>area;
        visit[id] = 1;
        sets[id] += estate;
        areas[id] += area;
    }
    for (int i = 1; i < maxn; i++) {
        if(sets[i] != 0 || areas[i] != 0) {
            int fa = findfather(i);
            if (i != fa) {
                sets[fa] += sets[i];
                areas[fa] += areas[i];
            }
        }
    }
    int clus = 0;
    for (int i = 0; i < maxn; i++) {
        if(i != father[i]) {
            int fa = findfather(i);
            numbers[fa]++;
        }
    }
    for (int i = 0; i < maxn; i++) {
        if(i == father[i] && visit[i] != 0) {
            Family[clus].id = i;
            Family[clus].number = numbers[i];
            Family[clus].sets = (double)(sets[i] * 1.0)/numbers[i];
            Family[clus].areas= (double)(areas[i] * 1.0)/numbers[i];
            clus++;
        }
    }
    sort(Family, Family+clus, cmp);
    cout<<clus<<endl;
    for (int i = 0; i < clus; i++) {
        printf("%04d %d %.3f %.3f\n", Family[i].id, Family[i].number, Family[i].sets, Family[i].areas);
    }
} 
上一篇下一篇

猜你喜欢

热点阅读