PAT 甲級(jí) 刷題日記|A 1114 Family Property (25 分)

單詞積累

decimal 小數(shù)的,十進(jìn)制的

descending 下降的

ascending 上升的,增長(zhǎng)的

the total number of sets of the real estate under his/her name 其名下房產(chǎn)的總套數(shù)

題目

This time, you are supposed to help us collect the data for family-owned property. Given each person's family members, and the estate(房產(chǎn))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
結(jié)尾無(wú)空行

Sample Output:

3
8888 1 1.000 1000.000
0001 15 0.600 100.000
5551 4 0.750 100.000
結(jié)尾無(wú)空行

思路

很典型的并查集題目,就是元素過(guò)多,處理起來(lái)較麻煩。比起在union過(guò)程中處理元素值,更簡(jiǎn)單的做法是合并完全部遍歷計(jì)算。

此外還涉及到了排序,格式化輸出等知識(shí)點(diǎn)。

最坑的點(diǎn)是默認(rèn)id編號(hào)是從1開(kāi)始的,居然導(dǎo)致了三個(gè)樣例出錯(cuò),檢查了半天,牢記不要想當(dāng)然,題目不指名,就是從0開(kāi)始編。

代碼

#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);
    }
} 
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容