Analysis of USACO past bronze group real test questions | December 2019 Cow Gymnastics

Learn C++ from a young age! Record the questions in the study process of preparing for the USACO (American Informatics Olympiad) bronze category competition and record every moment.

Attached is a summary post:Analysis of USACO past bronze group real questions | Summary-CSDN Blog


[Title description]

To improve their health, the cows begin gymnastics training! Farmer John selects his favorite cow, Bessie, to coach N other cows while evaluating their progress in learning different gymnastics techniques.

In each of the K training sessions, Bessie will give them feedback based on the performance of N cows. To rank. Later, she became curious about the consistency of these rankings. A pair of different cows is said to be congruent if one cow performs better than the other in every training session.

Please help Bessie calculate the number of pairs of cows that agree.

【enter】

The first line of input contains two positive integers K and N< a i=4>. The following K lines each contain some arrangement of the integers 1...N , indicating the ranking of the cows (the cows are distinguished by numbers 1...N ). If A appears before B in a certain line, it means cow < /span>  . B performs better than cows A

【Output】

Output a line containing the number of consistent pairs of cows.

【Input sample】

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

【Output sample】

4

[Detailed code explanation]

#include <bits/stdc++.h>
using namespace std;
int k, n, a[25][25]={0}, t[25]={0}, ans=0;
int main()
{
    cin >> k >> n;  // 输入k和n
    while (k--) {  // 依次读入k行数据
        for (int i=1; i<=n; i++) {  // 依次记录n个数据
            cin >> t[i];  
        }
        for (int i=1; i<n; i++) {  // 定义二维矩阵,分别记录每2个数的先后关系
            for (int j=i+1; j<=n; j++) {
                a[t[j]][t[i]] = 1;  // t[j]在t[i]后面,将表现不好的标记为1
            }
        }
    }
    for (int i=1; i<=n; i++) {  // 处理二维矩阵的斜线位置(左上到右下的斜线)
        for (int j=1; j<=n; j++) {
            if (i==j) a[i][j] = 1;  // 这些也标记为1
        }
    }
    for (int i=1; i<=n; i++) {  // 遍历二维矩阵
        for (int j=1; j<=n; j++) {
            if (a[i][j]==0) ans++;  // 对于值为0的单元格,就是一对A优于B的存在。统计ans就是一致的奶牛的对数
        }
    }
    cout << ans << endl;
    return 0;
}

【operation result】

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

Guess you like

Origin blog.csdn.net/guolianggsta/article/details/134786344