P5831 [USACO19DEC]Cow Gymnastics奶牛体操

在这里插入图片描述

  • 申请一个二维数组记录配对关系,值为k的就递增tot
#include<iostream>
using namespace std;
int a[11][21],tot,b[21][21];
int main()
{
    int k,n;cin >> k >> n;
    for(int i = 1;i <= k;i++)
        for(int j = 1;j <= n;j++)
            cin >> a[i][j];
    for(int i = 1;i <= k;i++)
        for(int j = 1;j < n;j++)
            for(int q = j+1;q <= n;q++)
            {
                b[a[i][j]][a[i][q]]++;
            }
    for(int i = 1;i <= n;i++)
        for(int j = 1;j <= n;j++)
            if(b[i][j] == k)
                tot++;
    cout << tot;
}

大佬

看似两层循环,实则再加上cmp函数有三层

#include<bits/stdc++.h>
using namespace std;

int n,k; 
int tmp;
int cnt;
int a[100][100];
bool cmp(int x,int y){
    for(int i=1;i<=k;i++){
        if(a[i][x]>a[i][y]){
            return false;
        }
    }
    return true;
}
int main()
{

    cin>>k>>n;
    for(int i=1;i<=k;i++){
        for(int j=1;j<=n;j++){
            cin>>tmp;
            a[i][tmp]=j;
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            if(i==j){
                continue;
            }
            if(cmp(i,j)){
                cnt++;
            }
        }
    }
    cout<<cnt;
    return 0;
}
发布了395 篇原创文章 · 获赞 52 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/dghcs18/article/details/104312488
今日推荐