L2-2 抢红包 (25分) java

没有人没抢过红包吧…… 这里给出NNN个人之间互相发红包、抢红包的记录,请你统计一下他们抢红包的收获。

输入格式:

输入第一行给出一个正整数NNN≤104\le 10^4104),即参与发红包和抢红包的总人数,则这些人从1到NNN编号。随后NNN行,第iii行给出编号为iii的人发红包的记录,格式如下:

KN1P1⋯NKPKK\quad N_1\quad P_1\quad \cdots\quad N_K\quad P_KKN1P1NKPK

其中KKK0≤K≤200 \le K \le 200K20)是发出去的红包个数,NiN_iNi是抢到红包的人的编号,PiP_iPi>0>0>0)是其抢到的红包金额(以分为单位)。注意:对于同一个人发出的红包,每人最多只能抢1次,不能重复抢。

输出格式:

按照收入金额从高到低的递减顺序输出每个人的编号和收入金额(以元为单位,输出小数点后2位)。每个人的信息占一行,两数字间有1个空格。如果收入金额有并列,则按抢到红包的个数递减输出;如果还有并列,则按个人编号递增输出。

输入样例:

10
3 2 22 10 58 8 125
5 1 345 3 211 5 233 7 13 8 101
1 7 8800
2 1 1000 2 1000
2 4 250 10 320
6 5 11 9 22 8 33 7 44 10 55 4 2
1 3 8800
2 1 23 2 123
1 8 250
4 2 121 4 516 7 112 9 10

  
    
    
    
    

输出样例:

1 11.63
2 3.63
8 3.63
3 2.11
7 1.69
6 -1.67
9 -2.18
10 -3.26
5 -3.26
4 -12.32

  
    
    
    
    

最后一个测试点超时

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.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 class Person{
     
		int number;
		int balance;
		int bags;
	}
	static Person[]person;
	public static void main(String[] args) throws IOException {
     
		Reader.init(System.in);
		n = Reader.nextInt();
		initialize();
		for (int i = 1; i < n+1; i++) {
     
			int k = Reader.nextInt();
			for (int j = 0; j < k; j++) {
     
				int number = Reader.nextInt();
				int money = Reader.nextInt();
				person[i].balance-=money;
				person[number].bags+=1;
				person[number].balance+=money;				
			}
		}
		Arrays.sort(person,1,n+1,new Comparator<Person>() {
     

			@Override
			public int compare(Person p1, Person p2) {
     
				// TODO Auto-generated method stub
				if (p1.balance<p2.balance) {
     		
					return 1;
				}else if (p1.balance==p2.balance) {
     
					if (p1.bags<p2.bags) {
     
						return 1;
					} else if(p1.bags == p2.bags) {
     
						if (p1.number>p2.number) {
     
							return 1;
						}
					}
				}
				return -1;
			}		
		});
		for (int i = 1; i < person.length; i++) {
     
			System.out.println(person[i].number+" "+String.format("%.2f", person[i].balance/100.0));
		}
	}
	public static void initialize() {
     
		person = new Person[n+1];
		for (int i = 1; i < person.length; i++) {
     
			person[i] = new Person();
			person[i].number = i;
		}
	}
	
}

你可能感兴趣的:(pta)