-
Notifications
You must be signed in to change notification settings - Fork 7
/
Main.java
77 lines (66 loc) · 1.47 KB
/
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package basicLevel1055;
// 测试点3,4,5超时
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int k = in.nextInt();
People[] peoples = new People[n];
for (int i = 0; i < n; i++) {
peoples[i] = new People(in.next(), in.nextInt());
}
in.close();
Arrays.sort(peoples);
int per = n / k;
for (int i = k; i >= 1; i--) {
int len = 0;
int index = 0;
if (i == k) {
len = n - k * per + per;
index = n-1;
} else {
len = per;
index = i * per - 1;
}
People[] temp = new People[len];
int left = len / 2;
int right = len / 2;
temp[left] = peoples[index--];
left--;
right++;
while (left >= 0 && right < len) {
temp[left--] = peoples[index--];
temp[right++] = peoples[index--];
}
if (left >= 0) {
temp[left] = peoples[index];
} else if (right < len) {
temp[right] = peoples[index];
}
for (int j = 0; j < len - 1; j++) {
System.out.print(temp[j].name + " ");
}
System.out.println(temp[len - 1].name);
}
}
}
class People implements Comparable<People> {
String name;
int high;
public People(String name, int high) {
this.name = name;
this.high = high;
}
@Override
public int compareTo(People o) {
if (this.high < o.high) {
return -1;
} else if (this.high > o.high) {
return 1;
} else {
return -this.name.compareTo(o.name);
}
}
}