PAT乙级1032 挖掘机技术哪家强

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Map.Entry;
public class Main {
	
	public static void main(String[] args) throws IOException {
		
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        String line = null;
        String[] record = null;
        int no;
        int score;
        int topNo = 0 ;
        int topScore = 0;
        HashMap hs = new HashMap();
        int num = Integer.parseInt(bf.readLine());
        for (int i = 0; i < num; i++) {
			line = bf.readLine();
			record = line.split(" ");
			no = Integer.parseInt(record[0]);
			score = Integer.parseInt(record[1]);
			
			if (hs.containsKey(no)) {
				score+=hs.get(no);
                if (score>topScore) {
					topNo = no;
					topScore = score;
				}
				hs.put(no, score);
			}
			else {
				hs.put(no, score);
                if (score>topScore) {
					topNo = no;
					topScore = score;
				}
			}
		}
        //Iterator> iterator = hs.entrySet().iterator();
        //while (iterator.hasNext()) {
			//Map.Entry entry = (Map.Entry) iterator.next();
			//if (entry.getValue()>topScore) {
				//topNo = entry.getKey();
				//topScore = entry.getValue();
			//}
		//}
        System.out.println(topNo+ " " + topScore);
	}
}

Java真的慢,优化不用迭代器,在每次存的时候就记录最大值还是运行超时,不适合刷OJ,以后决定换C++。

你可能感兴趣的:(Java,oj)