In C Recently, a new game, Risk 1000000 , similar to Risk, has been released tha
ID: 3672801 • Letter: I
Question
In C
Recently, a new game, Risk1000000, similar to Risk, has been released that allows for megabattles of up to 1,000,000 armies versus 1,000,000 armies. Also, rather than simulating a battle with a die roll, an army's strength can be any number from 1 to a billion (instead of 1 through 6 for a die roll). One final difference in Risk1000000 is that it gives the defenders an extra advantage. Instead of lining up the highest attacking die with the highest defending die and so forth, the attacker must position his armies for each battle and show this information to the defender. The defender can then choose any ordering of her armies.
To see the difference, let's consider an example from regular Risk and a couple examples from Risk1000000. In the original game Risk, if an attacker with two armies rolls a 6 and 3, while a
defender rolls a 5 and a 2, we must match the two maximum rolls (6 versus 5) and the two minimum rolls (3 verses 2), which results in the defender losing two armies.
If we were to have the same situation in Risk1000000, the defender would see that the attacker has 6 for its first army and 3 for his second army. The defender can then strategically place the 2 for her first army and the 5 for her second army, resulting in the loss of one defending army and one attacking army.
Consider a slightly larger situation where the attacker had the following values for its armies showing:
18 9 21 5 3 27 15
Imagine that the defender's army values are:
15 8 6 2 25 19 20
If the defender kept this original ordering, she'd only win two battles (25 versus 3 and 20 versus 15).
An optimal rearrangement of armies for the defender is as follows:
19 15 25 6 8 2 20 versus
18 9 21 5 3 27 15
where the defender wins all but one battle, incurring the loss of just one army (in the 27 versues 2 battle) while the attacker would lose six armies.
The Problem
Write a program that, given a list of the values of both the attackers armies and defenders armies, determines the maximum number of attacking armies that can be defeated by the defender, if she plays optimally.
The Input (read from standard input)
The first line of input will contain a single positive integer, c (c 100), representing the number of input cases. The first line of each input case will contain a single integer, n (n 106), representing the number of armies battling from each side. The next n lines of the input case will contain a single integer each, representing the value of one of the attacking armies. The following n lines of the input case will contain a single integer each, representing the value of one of the defending armies. All of the values on these 2n lines will be positive integers less than or equal to one billion.
The Output (to standard out)
For each input case, output a single integer, on a line by itself, representing the maximum number of attacking armies that can be defeated by the defender.
Sample Input
2
2
3
6
5
2
3
2
3
12
9
3
4
Sample Output
1
2
Explanation / Answer
package com.he.capillary;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class Armies {
public static void main(String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
String line = br.readLine();
int noOfCases = Integer.parseInt(line.trim());
for (int i = 0; i < noOfCases; i++) {
int num = Integer.parseInt(br.readLine());
List<Integer> attacker = new ArrayList<Integer>();
for (int j = 0; j < num; j++) {
attacker.add(Integer.parseInt(br.readLine().trim()));
}
List<Integer> defender = new ArrayList<Integer>();
for (int j = 0; j < num; j++) {
defender.add(Integer.parseInt(br.readLine().trim()));
}
attacker.sort((i1, i2) -> i1.compareTo(i2));
defender.sort((i1, i2) -> i1.compareTo(i2));
int count = 0, k = defender.size() - 1, l = attacker.size() - 1;
while (k >= 0 && l >= 0) {
if (defender.get(k) > attacker.get(l)) {
count++;
k--;
l--;
} else if (defender.get(k) <= attacker.get(l)) {
l--;
}
}
System.out.println(count);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Let me know if it is not working fine. I have tested with some test cases.