Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
哈哈,竟然过了。用JAVA 超时的兄弟可以参考一下。1、按 salepoint 排序, 再做统计。 2、做顺序统计的过程中, 不要使用 print , 而是使用 StringBuilder 把输出的内容装着。 3、在最后把 BtringBuilder 对象包含的内容打出来。 由于此题需要打屏输出大量数据, 每次调用 print 会消耗很多的时间。 注: Collection.sort 方法调用的是归并排序来的, 所以排序的效率算稳定。 import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashSet; import java.util.List; import java.util.StringTokenizer; public class Main { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { // TODO Auto-generated method stub BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); // new BufferedReader(new FileReader(new File("C:\\Users\\jackchongs\\workspace\\POJ\\src\\test.txt"))); int N = Integer.valueOf(br.readLine()); HashSet<Integer> items = new HashSet<Integer>(); HashSet<Integer> salePoints = new HashSet<Integer>(); List<Triplets> list = new ArrayList<Triplets>(); long begin = System.currentTimeMillis(); for (int i = 0; i < N; i++) { String str = br.readLine(); StringTokenizer stoken = new StringTokenizer(str, " "); int qi = Integer.valueOf(stoken.nextToken()); int si = Integer.valueOf(stoken.nextToken()); int vi = Integer.valueOf(stoken.nextToken()); // String tmp [] = str.split(" "); // // int qi = Integer.valueOf(tmp[0]); // int si = Integer.valueOf(tmp[1]); // int vi = Integer.valueOf(tmp[2]); if( ! items.contains(qi)) items.add(qi); if( ! salePoints.contains(si)) salePoints.add(si); list.add(new Triplets(qi, si, vi)); } Collections.sort(list, new MyComparator()); List<Integer> listItems = new ArrayList<Integer>(items); List<Integer> listSalePoints = new ArrayList<Integer>(salePoints); Collections.sort(listItems); Collections.sort(listSalePoints); // System.out.println(list); // System.out.println(listItems); // System.out.println(listSalePoints); System.out.print("-1"); for (Integer curItem : listItems ) { System.out.print(" " + curItem ); } System.out.println(); Triplets tmp = null; tmp = list.get(0); int k = 1; StringBuffer strBuff = new StringBuffer(); for (int i = 0; i < listSalePoints.size(); i++) { // System.out.print( listSalePoints.get(i)+ " "); strBuff.append(listSalePoints.get(i)); strBuff.append(" "); for (int j = 0; j < listItems.size(); j++) { if( tmp.qi != listItems.get(j) || tmp.si !=listSalePoints.get(i) ) { // System.out.print("0"); strBuff.append("0"); if( j != listItems.size() -1 ) { // System.out.print(" "); strBuff.append(" "); } } else { int sum = tmp.vi; while( k < list.size()&& tmp.qi == list.get(k).qi && tmp.si == list.get(k).si ) { sum += list.get(k).vi; k++; } if( k < list.size()) { tmp = list.get(k); k++; } // System.out.print(sum); strBuff.append(sum); if( j != listItems.size() -1 ) { // System.out.print(" "); strBuff.append(" "); } } } // System.out.println(); strBuff.append("\n"); } System.out.print(strBuff.toString()); long end = System.currentTimeMillis(); // System.out.println( end - begin ); br.close(); } } class Triplets { int qi = 0; int si = 0; int vi = 0; public Triplets( int in_qi , int in_si, int in_vi ) { qi = in_qi; si = in_si; vi = in_vi; } public String toString() { return "("+ qi +"," + si +"," + vi + ")"; } } class MyComparator implements Comparator<Triplets> { public int compare( Triplets a, Triplets b ) { if( a.si > b.si ) return 1; else if( a.si == b.si ) { if(a.qi > b.qi ) return 1; else return -1; } else { return -1; } } } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator