L2-3 家庭房產 (25分) java

給定每個人的家庭成員和其自己名下的房產,請你統計出每個家庭的人口數、人均房產面積及房產套數。

輸入格式:

輸入第一行給出一個正整數NNN≤1000\le 10001000),隨後NNN行,每行按下列格式給出一個人的房產:

編號 父 母 k 孩子1 ... 孩子k 房產套數 總面積

其中編號是每個人獨有的一個4位數的編號;分別是該編號對應的這個人的父母的編號(如果已經過世,則顯示-1);k0≤0\le0k≤5\le 55)是該人的子女的個數;孩子i是其子女的編號。

輸出格式:

首先在第一行輸出家庭個數(所有有親屬關係的人都屬於同一個家庭)。隨後按下列格式輸出每個家庭的信息:

家庭成員的最小編號 家庭人口數 人均房產套數 人均房產面積

其中人均值要求保留小數點後3位。家庭信息首先按人均面積降序輸出,若有並列,則按成員編號的升序輸出。

輸入樣例:

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

輸出樣例:

3
8888 1 1.000 1000.000
0001 15 0.600 100.000
5551 4 0.750 100.000

兩個測試點超時

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Queue;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.Vector;

//** Class for buffered reading int and double values *//*
class Reader {
	static BufferedReader reader;
	static StringTokenizer tokenizer;

	// ** call this method to initialize reader for InputStream *//*
	static void init(InputStream input) {
		reader = new BufferedReader(new InputStreamReader(input));
		tokenizer = new StringTokenizer("");
	}

	// ** get next word *//*
	static String next() throws IOException {
		while (!tokenizer.hasMoreTokens()) {
			// TODO add check for eof if necessary
			tokenizer = new StringTokenizer(reader.readLine());
		}
		return tokenizer.nextToken();
	}
	static boolean hasNext()throws IOException {
		return tokenizer.hasMoreTokens();
	}
	static String nextLine() throws IOException{
		return reader.readLine();
	}
	static char nextChar() throws IOException{
		return next().charAt(0);
	}
	static int nextInt() throws IOException {
		return Integer.parseInt(next());
	}
	static long nextLong() throws IOException {
		return Long.parseLong(next());
	}
	static float nextFloat() throws IOException {
		return Float.parseFloat(next());
	}
}
public class Main {
	static int n;
	static int []person = new int[10000];
	static int []house = new int[10000];
	static int []area = new int[10000];
	static Set<Integer>visited = new HashSet<Integer>();
	static class Family{
		int min_number;
		int people_num;
		double avg_house_num;
		double avg_house_area;
	}
	static List<Family>family = new ArrayList<Family>();
	
	public static void main(String[] args) throws IOException {
		Reader.init(System.in);
		n = Reader.nextInt();
		for (int i = 0; i < person.length; i++) {
			person[i] = i;
		}
		for (int i = 0; i < n; i++) {
			int id = Reader.nextInt();
			int fid = Reader.nextInt();
			int mid = Reader.nextInt();
			if (fid!=-1) {
				visited.add(fid);
				union(fid, id);
			}
			if (mid!=-1) {
				visited.add(mid);
				union(mid, id);
			}
			visited.add(id);
			int k = Reader.nextInt();
			for (int j = 0; j < k; j++) {
				int cid = Reader.nextInt();
				visited.add(cid);
				union(cid, id);
			}
			house[id] = Reader.nextInt();
			area[id] = Reader.nextInt();
		}
		
		for (Integer id : visited) {
			if (person[id]==id) {
				Family f = new Family();
				int min_number = id;
				int people_num = 1;
				double house_num = house[id];
				double house_area = area[id];
				
				for (Integer id1 : visited) {
					
					if (id1!=id&&find(id1)==id) {
						
						min_number = id1>min_number?min_number:id1;
						people_num++;
						house_num+=house[id1];
						house_area+=area[id1];
					}
				}
				
				f.min_number = min_number;
				f.people_num = people_num;
				f.avg_house_num = house_num/people_num;
				f.avg_house_area = house_area/people_num;
				family.add(f);
			}
		}
		Collections.sort(family, new Comparator<Family>() {

			@Override
			public int compare(Family f1, Family f2) {
				if (f1.avg_house_area<f2.avg_house_area) {
					return 1;
				} else if (f1.avg_house_area==f2.avg_house_area) {
					if (f1.min_number>f2.min_number) {
						return 1;
					}
				}
				return -1;
			}
		});
		System.out.println(family.size());
		for (Family f : family) {
			System.out.println(String.format("%04d", f.min_number)+" "+f.people_num+" "+String.format("%.3f", f.avg_house_num)+" "+String.format("%.3f", f.avg_house_area));
		}
	}
	static int find(int x) {
		int r = x;
		while (person[r]!=r) {
			r = person[r];
		}
		return r;
	}
	static void union(int a, int b) {
		int fa = find(a);
		int fb = find(b);
		if (fa!=fb) {
			person[fa] = fb;
		}
		
	}
	
}

=============================================================================
今天又碰到了,重新寫了一遍能過了

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Set;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.regex.Pattern;

import javax.management.monitor.Monitor;


//** Class for buffered reading int and double values *//*
class Reader {
	static BufferedReader reader;
	static StringTokenizer tokenizer;

	// ** call this method to initialize reader for InputStream *//*
	static void init(InputStream input) {
		reader = new BufferedReader(new InputStreamReader(input));
		tokenizer = new StringTokenizer("");
	}

	// ** get next word *//*
	static String next() throws IOException {
		while (!tokenizer.hasMoreTokens()) {
			// TODO add ceck for eof if necessary
			tokenizer = new StringTokenizer(reader.readLine());
		}
		return tokenizer.nextToken();
	}
	static boolean hasNext()throws IOException {
		return tokenizer.hasMoreTokens();
	}
	static String nextLine() throws IOException{
		return reader.readLine();
	}
	static char nextChar() throws IOException{
		return next().charAt(0);
	}
	static int nextInt() throws IOException {
		return Integer.parseInt(next());
	}
	static long nextLong() throws IOException {
		return Long.parseLong(next());
	}
	static float nextFloat() throws IOException {
		return Float.parseFloat(next());
	}
	static double nextDouble() throws IOException {
		return Double.parseDouble(next());
	}
	static void close() throws IOException {
		reader.close();
	}
}
class Writer{
	static BufferedWriter writer;
	static void init(OutputStream outputStream) {
		writer = new BufferedWriter(new OutputStreamWriter(outputStream));
	}
	static void print(Object object) throws IOException {
		writer.write(object.toString());
	}
	static void println(Object object) throws IOException {
		writer.write(object.toString());
		writer.write("\n");
	}
	static void close() throws IOException {
		// TODO Auto-generated method stub
		writer.close();
	}
}
public class Main {
	
	public static void main(String[] args) throws IOException {
		Reader.init(System.in);
		Writer.init(System.out);
		solve();
		Reader.close();
		Writer.close();
	}
	static int[]pre = new int[10001];
	static int[]personNum = new int[10001];
	static int[]houseNum = new int[10001];
	static int[]area = new int[10001];
	static class Family{
		int minNumber;
		int num;
		double houseNumAvg;
		double areaAvg;
		public Family(int i, int j, double d, double e) {
			this.minNumber = i;
			this.num = j;
			this.houseNumAvg = d;
			this.areaAvg = e;
		}
	}
	static ArrayList<Family>families = new ArrayList<Family>(); 
	private static void solve() throws IOException {
		int n = Reader.nextInt();
		for (int i = 0; i < pre.length; i++) {
			pre[i] = i;
		}
		for (int i = 0; i < n; i++) {
			int id = Reader.nextInt();
			int fid = Reader.nextInt();
			int mid = Reader.nextInt();
			if (fid!=-1) {
				union(id,fid);
			}
			if (mid!=-1) {
				union(id,mid);
			}
			int cnum = Reader.nextInt();
			for (int j = 0; j < cnum; j++) {
				int cid = Reader.nextInt();
				union(id, cid);
			}
			int pid = find(id);
			houseNum[pid] += Reader.nextInt();
			area[pid] += Reader.nextInt();
		}
		for (int i = 0; i < personNum.length; i++) {
			personNum[find(i)]++;
		}
		for (int i = 0; i < pre.length; i++) {
			if (pre[i] == i&&houseNum[i]!=0) {
				families.add(new Family(i,personNum[i],1.0*houseNum[i]/personNum[i],1.0*area[i]/personNum[i]));
				
			}
		}
		Collections.sort(families, new Comparator<Family>() {

			@Override
			public int compare(Family o1, Family o2) {
				// TODO Auto-generated method stub
				if (o1.areaAvg>o2.areaAvg) {
					return -1;
				}else if(o1.areaAvg==o2.areaAvg) {
					return o1.minNumber-o2.minNumber;
				}else 
					return 1;
				
			}
		});
		Writer.println(families.size());
		for (Family f : families) {
			Writer.println(String.format("%04d", f.minNumber)+" "+f.num+" "+String.format("%.3f", f.houseNumAvg)+" "+String.format("%.3f", f.areaAvg));
		}
	}
	static void union(int id1,int id2) {
		int pid1 = find(id1);
		int pid2 = find(id2);
		if (pid1<pid2) {
			pre[pid2] = pid1;
			houseNum[pid1]+=houseNum[pid2];
			area[pid1]+=area[pid2];
		}
		if (pid1>pid2) {
			pre[pid1] = pid2;
			houseNum[pid2]+=houseNum[pid1];
			area[pid2]+=area[pid1];
		}
	}
	private static int find(int id1) {
		while (id1!=pre[id1]) {
			id1 = pre[id1];
		}
		return id1;
	}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章