(SPOJ) Times - Solution
Link to the problem: http://br.spoj.com/problems/TIMES1/
We only need to sort the students according to their level of ability in the game. Then, we distribute each student in a team.
import java.io.*;
import java.util.*;
class Main {
public void process() throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
String line = br.readLine();
String[] s = line.split(" ");
int numStudents = Integer.parseInt(s[0]);
int numTeams = Integer.parseInt(s[1]);
Abilitie[] abilities = new Abilitie[numStudents];
for (int i = 0; i < numStudents; i++) {
line = br.readLine();
s = line.split(" ");
abilities[i] = new Abilitie(s[0], Integer.parseInt(s[1]));
}
br.close();
Arrays.sort(abilities);
ArrayList<ArrayList<String>> teams = new ArrayList<>();
for (int i = 0; i < numTeams; i++) {
ArrayList<String> list = new ArrayList<>();
for (int j = i; j < numStudents; j += numTeams) {
list.add(abilities[j].name);
}
Collections.sort(list);
teams.add(list);
}
for (int i = 0; i < numTeams; i++) {
ArrayList<String> list = teams.get(i);
bw.write("Time " + (i+1) + "\n");
for (int j = 0; j < list.size(); j++) {
bw.write(list.get(j) + "\n");
}
bw.write("\n");
}
bw.flush();
bw.close();
return;
}
public static void main(String[] args) throws NumberFormatException, IOException {
Main m = new Main();
m.process();
System.exit(0);
}
}
class Abilitie implements Comparable<Abilitie> {
String name;
int level;
public Abilitie(String n, int l) {
name = n;
level = l;
}
public int compareTo(Abilitie a) {
return a.level-this.level;
}
}
We only need to sort the students according to their level of ability in the game. Then, we distribute each student in a team.
import java.io.*;
import java.util.*;
class Main {
public void process() throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
String line = br.readLine();
String[] s = line.split(" ");
int numStudents = Integer.parseInt(s[0]);
int numTeams = Integer.parseInt(s[1]);
Abilitie[] abilities = new Abilitie[numStudents];
for (int i = 0; i < numStudents; i++) {
line = br.readLine();
s = line.split(" ");
abilities[i] = new Abilitie(s[0], Integer.parseInt(s[1]));
}
br.close();
Arrays.sort(abilities);
ArrayList<ArrayList<String>> teams = new ArrayList<>();
for (int i = 0; i < numTeams; i++) {
ArrayList<String> list = new ArrayList<>();
for (int j = i; j < numStudents; j += numTeams) {
list.add(abilities[j].name);
}
Collections.sort(list);
teams.add(list);
}
for (int i = 0; i < numTeams; i++) {
ArrayList<String> list = teams.get(i);
bw.write("Time " + (i+1) + "\n");
for (int j = 0; j < list.size(); j++) {
bw.write(list.get(j) + "\n");
}
bw.write("\n");
}
bw.flush();
bw.close();
return;
}
public static void main(String[] args) throws NumberFormatException, IOException {
Main m = new Main();
m.process();
System.exit(0);
}
}
class Abilitie implements Comparable<Abilitie> {
String name;
int level;
public Abilitie(String n, int l) {
name = n;
level = l;
}
public int compareTo(Abilitie a) {
return a.level-this.level;
}
}
Comments
Post a Comment