-
http://ac.jobdu.com/problem.php?pid=1007
-
按要求,给国家进行排名。
题目1007:奥运排序问题
题目描述:
-
输入:
-
有多组数据。
第一行给出国家数N,要求排名的国家数M,国家号从0到N-1。
第二行开始的N行给定国家或地区的奥运金牌数,奖牌数,人口数(百万)。
接下来一行给出M个国家号。
-
输出:
-
排序有4种方式: 金牌总数 奖牌总数 金牌人口比例 奖牌人口比例
对每个国家给出最佳排名排名方式 和 最终排名
格式为: 排名:排名方式
如果有相同的最终排名,则输出排名方式最小的那种排名,对于排名方式,金牌总数 < 奖牌总数 < 金牌人口比例 < 奖牌人口比例
如果有并列排名的情况,即如果出现金牌总数为 100,90,90,80.则排名为1,2,2,4.
每组数据后加一个空行。 -
-
-
样例输入:
-
4 4 4 8 1 6 6 2 4 8 2 2 12 4 0 1 2 3 4 2 8 10 1 8 11 2 8 12 3 8 13 4 0 3
-
样例输出:
-
1:3 1:1 2:1 1:2 1:1 1:1
-
-
import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; 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.List; import java.util.Map; import java.util.Set; import java.util.StringTokenizer; public class Main { public static void main(String[] args) { new Task().solve(); } } class Task{ InputReader in = new InputReader(System.in) ; PrintWriter out = new PrintWriter(System.out) ; class Country{ int id ; double golden ; double prize; double people ; Country(int id ,double golden , double prize , double people){ this.id = id ; this.golden = golden ; this.prize = prize ; this.people = people ; } @Override public String toString() { return "Country [id=" + id + ", golden=" + golden + ", prize=" + prize + ", people=" + people + "]\n"; } } @SuppressWarnings("unchecked") Comparator<Country>[] cmp = new Comparator[]{ new Comparator<Task.Country>() { @Override public int compare(Country o1, Country o2) { return Double.compare(o2.golden*1.0 , o1.golden*1.0); } } , new Comparator<Task.Country>() { @Override public int compare(Country o1, Country o2){ return Double.compare(o2.prize*1.0 , o1.prize*1.0) ; } }, new Comparator<Task.Country>() { @Override public int compare(Country o1, Country o2){ return Double.compare(o2.golden*1.0/ o2.people , o1.golden*1.0/ o1.people ); } } , new Comparator<Task.Country>() { @Override public int compare(Country o1, Country o2){ return Double.compare(o2.prize*1.0/ o2.people , o1.prize*1.0/ o1.people ); } }}; int[] gao(List<Country> cs , Comparator<Country> cmp , int N){ Collections.sort(cs, cmp); int rank = 1 ; Map<Integer, Integer> ranks = new HashMap<Integer, Integer>() ; ranks.put(cs.get(0).id, rank) ; for(int i = 1 ; i < cs.size() ; i++){ rank++ ; if(cmp.compare(cs.get(i-1), cs.get(i)) == 0){ ranks.put(cs.get(i).id, ranks.get( cs.get(i-1).id )) ; } else{ ranks.put(cs.get(i).id, rank) ; } } int[] r = new int[N] ; for(Map.Entry<Integer, Integer> e : ranks.entrySet()){ r[e.getKey()] = e.getValue() ; } return r ; } void solve(){ int N , M; while(in.hasNext()){ N = in.nextInt(); M = in.nextInt() ; List<Country> list = new ArrayList<Task.Country>(); for(int i = 0 ; i < N ; i++){ list.add(new Country(i , in.nextLong() , in.nextLong() , in.nextLong() ) ); } int[] sb = new int[M] ; Set<Integer> vis = new HashSet<Integer>() ; for(int i = 0 ; i < M ; i++){ sb[i] = in.nextInt() ; vis.add(sb[i]) ; } List<Country> toSortCountry = new ArrayList<Task.Country>() ; for(Country c : list){ if(vis.contains(c.id)){ toSortCountry.add(c) ; } } int[][]rank = new int[N][N] ; for(int i = 0 ; i < 4 ; i++){ rank[i] = gao(toSortCountry, cmp[i] , N) ; } int[][] res = new int[N][2] ; for(int man = 0 ; man < N ; man++){ res[man][0] = 0 ; res[man][1] = rank[0][man] ; for(int c = 1 ; c < 4 ; c++){ if(rank[c][man] < res[man][1]){ res[man][1] = rank[c][man] ; res[man][0] = c ; } } } for(int i = 0 ; i < M ; i++){ out.println(res[sb[i]][1] + ":" + (res[sb[i]][0]+1)) ; } out.println() ; // out.flush() ; } out.flush() ; } } class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = new StringTokenizer(""); } private void eat(String s) { tokenizer = new StringTokenizer(s); } public String nextLine() { try { return reader.readLine(); } catch (Exception e) { return null; } } public boolean hasNext() { while (!tokenizer.hasMoreTokens()) { String s = nextLine(); if (s == null) return false; eat(s); } return true; } public String next() { hasNext(); return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public BigInteger nextBigInteger() { return new BigInteger(next()); } }
-