As the problem statement says, for each pair of cows $(A, B)$, we will count how many practice sessions cow $A$ did better than cow $B$ in. If cow $A$ did better than cow $B$ in all $K$ practice sessions, we increment a counter, and we'll print out the value of the counter once we've looped over all pairs of cows.
In terms of implementation details, we can use a 2D array to store all of the rankings. Below is Brian Dean's code following this approach.
#include <iostream> #include <fstream> using namespace std; int N, K; int data[10][20]; bool better(int a, int b, int session) { int apos, bpos; for (int i=0; i<N; i++) { if (data[session][i] == a) apos = i; if (data[session][i] == b) bpos = i; } return apos < bpos; } int Nbetter(int a, int b) { int total = 0; for (int session=0; session<K; session++) if (better(a,b,session)) total++; return total; } int main(void) { ifstream fin ("gymnastics.in"); ofstream fout ("gymnastics.out"); fin >> K >> N; for (int k=0; k<K; k++) for (int n=0; n<N; n++) fin >> data[k][n]; int answer = 0; for (int a=1; a<=N; a++) for (int b=1; b<=N; b++) if (Nbetter(a,b) == K) answer++; fout << answer << "\n"; return 0; }