-
Notifications
You must be signed in to change notification settings - Fork 0
/
cf_round918_F.java
52 lines (38 loc) · 1.5 KB
/
cf_round918_F.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
import java.util.Arrays;
import java.util.Scanner;
public class cf_round918_F {
public static int countGreetings(int[][] people) {
int n = people.length;
int[] startingPositions = new int[n];
// Create an array to store the starting positions
for (int i = 0; i < n; i++) {
startingPositions[i] = people[i][0];
}
// Sort the array of starting positions
Arrays.sort(startingPositions);
int greetings = 0;
// Count the number of greetings based on the sorted starting positions
for (int i = 0; i < n; i++) {
int position_i = people[i][0];
int index = Arrays.binarySearch(startingPositions, position_i);
// Number of people with ending positions less than position_i
greetings += Math.abs(index) - 1;
}
return greetings;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt(); // Number of test cases
for (int testCase = 0; testCase < t; testCase++) {
int n = scanner.nextInt(); // Number of people
int[][] people = new int[n][2];
for (int i = 0; i < n; i++) {
people[i][0] = scanner.nextInt(); // Starting position
people[i][1] = scanner.nextInt(); // Ending position
}
int result = countGreetings(people);
System.out.println(result);
}
scanner.close();
}
}