思路:使用鄰接表的結構來存儲數據,分數做下標,賬號存鏈表。
插入的時候在鏈表中使用插入排序的方式(其他排序方式也可以)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.LinkedList;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
Reader.init(System.in);
int N = Reader.nextInt();
int G = Reader.nextInt();
int K = Reader.nextInt();
// 使用鄰接表結構,分數做下標,賬號放在鏈表中(使用插入排序的方式)
node[] map = new node[101];
for (int i = 0; i < N; i++) {
String id = Reader.next();
int grade = Reader.nextInt();
// 標記分數是否出現過,只分配空間給出現過的分數
if (map[grade] == null) {
map[grade] = new node();
}
map[grade].insert(id);
}
// 計算髮出去的pat代金券
int price_pat = 0;
for (int i = map.length - 1; i >= 0; i--) {
if (map[i] == null)
continue;
if (i >= G) {
price_pat += 50 * map[i].list.size();
} else if( i >= 60) {
price_pat += 20 * map[i].list.size();
}
}
System.out.println(price_pat);
int rank = 1;
// 打印排名
for (int i = map.length - 1; i >= 0; i--) {
if (map[i] == null)
continue;
if (rank > K) {
break;
}
int tmp = rank;
for (String item : map[i].list) {
System.out.println(tmp + " " + item + " " + i);
rank++;
}
}
}
static class node {
LinkedList<String> list = new LinkedList<String>();
// 插入的時候排序
void insert(String id) {
int index = 0;
for (String string : list) {
char[] o1 = string.toCharArray();
char[] o2 = id.toCharArray();
int len = o1.length > o2.length ? o2.length : o1.length;
boolean isFind = false;
for (int i = 0; i < len; i++) {
// 如果字母一樣,則繼續查對比
if(o1[i] != o2[i]) {
// 如果字母不一樣,則判斷是否是在當前位置
if(o1[i] > o2[i]){
// 是在當前位置,位置已經找到
isFind = true;
}
break;
}
}
if (isFind) {
break;
}
index++;
}
list.add(index, id);
}
}
}
// 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("");
}
static void init(File file) {
try {
reader = new BufferedReader(new FileReader(file));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
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 String nextLine() throws IOException {
return reader.readLine();
}
static int nextInt() throws IOException {
return Integer.parseInt(next());
}
static char nextChar() throws IOException {
return next().toCharArray()[0];
}
static float nextFloat() throws IOException {
return Float.parseFloat(next());
}
static Double nextDouble() throws IOException {
return Double.parseDouble(next());
}
}