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 (1≤K≤101≤K≤10), Bessie ranks the?NN?cows according to their performance (1≤N≤201≤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.
The first line of the input file contains two positive integers?KK?and?NN. The next?KK?lines will each contain the integers?1…N1…N?in some order, indicating the rankings of the cows (cows are identified by the numbers?1…N1…N). If?AA?appears before?BB?in one of these lines, that means cow?AA?did better than cow?BB.
Output, on a single line, the number of consistent pairs.
3 4 4 1 2 3 4 1 3 2 4 2 1 3
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
[/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++;
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;
}
以上就是關于【USACO 2019 December Contest, Bronze Problem 1. Cow Gymnastics】的解答,如需了解學校/賽事/課程動態,可至翰林教育官網獲取更多信息。
往期文章閱讀推薦:
2026?2027上海青少年科學社高級會員選拔結果公布,新一屆科創學子開啟成長新旅程!
國家級頂尖科創培養項目!深度解析上海市“中學生英才計劃”,新高一學生該如何布局?

? 2026. All Rights Reserved. 滬ICP備2023009024號-1