# USACO 2019 December Contest, Bronze Problem 1. Cow Gymnastics

In order to improve their physical fitness, the cows have taken up gymnastics! Farmer John designates his favorite cow Bessie to coach the NN other cows and to assess their progress as they learn various gymnastic skills.

In each of KK practice sessions (1K101≤K≤10), Bessie ranks the NN cows according to their performance (1N201≤N≤20). Afterward, she is curious about the consistency in these rankings. A pair of two distinct cows is consistent if one cow did better than the other one in every practice session.

Help Bessie compute the total number of consistent pairs.

#### INPUT FORMAT (file gymnastics.in):

The first line of the input file contains two positive integers KK and NN. The next KK lines will each contain the integers 1N1…N in some order, indicating the rankings of the cows (cows are identified by the numbers 1N1…N). If AA appears before BB in one of these lines, that means cow AA did better than cow BB.

#### OUTPUT FORMAT (file gymnastics.out):

Output, on a single line, the number of consistent pairs.

#### SAMPLE INPUT:

3 4
4 1 2 3
4 1 3 2
4 2 1 3


#### SAMPLE OUTPUT:

4


The consistent pairs of cows are (1,4)(1,4)(2,4)(2,4)(3,4)(3,4), and (1,3)(1,3).

Problem credits: Nick Wu

### USACO 2019 December Contest, Bronze Problem 1. Cow Gymnastics题解(翰林国际教育提供，仅供参考)

[/hide]

(Analysis by Nick Wu)

As the problem statement says, for each pair of cows (A,B)(A,B), we will count how many practice sessions cow AA did better than cow BB in. If cow AA did better than cow BB in all KK 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++;
}

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];
}