The problem I want to solve is not to get a solution to the problem, but to make it more efficient , in particular, go from O (N²) to O (N) or O (N) log (N)) if possible.
Context: problem raised at the university about a dice game .
Basically the game consists of the following: each player rolls a die and the one that gets the highest score wins.
The "grace" of the game is that the dice are not normal , they have between 1 and 100000 numbered faces each with numbers between 1 and 10000, such data entered via keyboard.
The question to be answered is, if you give me the option to choose the die first, which dice do I choose to have the highest probability of winning?
My proposal (and solution, I think effective) not efficient:
int main() {
int caras;
int tope_wins = caras * caras * 0.5;
int wins = 0;
vi faces1(caras, 1), faces2(caras, 1);
for (int i = 0; i < caras; i++) {
cin >> faces1[i];
}
for (int i = 0; i < caras; i++) {
cin >> faces2[i];
}
sort(faces2.begin(), faces2.end());
sort(faces1.begin(), faces1.end(), greater<int>());
for (int i = 0; i < caras; i++) {
if (faces1[i] > faces2[0]) {
for (int j = 0; j < caras; j++) {
if (faces1[i] <= faces2[j]) {
continue;
}
if (faces1[i] > faces2[j]) {
wins++;
}
}
}
}
if (wins == tope_wins || wins == 0)
cout << "NO GANA NADIE\n";
else if (wins > tope_wins)
cout << "GANA EL PRIMERO\n";
else if (wins < tope_wins)
cout << "GANA EL SEGUNDO\n";
return 0;
}
You can see how I check only the number of times the first die entered on the screen would win, saving us some iterations for having ordered the values of the faces before.
According to the professor, there must be a solution to this problem more efficient than this one.
Execution example:
D1: 4 4 4 8 11 11
D2: 1 1 9 9 9 9
WIN THE FIRST
In summary: you are given to choose two uncommon dice (many faces with values in each of them without following any sequence). How to choose the one with the highest probability of winning after many throws (the one that obtains the highest value when shooting, after throwing many times), but we only want to know which one we have left to start playing.