华东交通大学2016年ACM“双基”程序设计竞赛 1002

Problem Description

今天小学弟又训练完了,但是小学弟又不想看球赛,于是小学弟看马赛了。他发现马鞍是一个奇怪的东西。于是小学弟根据马鞍定义了一种马鞍数:在一个二位矩阵中,马鞍数在当前行是最小的,在当前列是最大的,并且这个数在当前行和列都是独一无二的。小学弟现在马上打开电脑写出了一个程序求出了矩阵里面马鞍数的数量。那么,你能求出来吗?这是一道非常简单的题目。

Input

输入包含多组,每一组输入一个n(1<=n<=1000),接下来n行,每一行有n个数a1,a2,a3……,an(0<=ai<=10000000000)。

Output

输出马鞍数的数量。

Sample Input

5
1 2 3 4 5
0 0 0 0 4
0 0 0 0 3
0 0 0 0 2
0 0 0 0 1

Sample Output

1

Author

zhengjinke2123
解法:首先找到行的符合要求的元素,用数组记录列,同理找到行的符合要求的元素,记录行,然后遍历每一行
#include<stdio.h>
//#include<bits/stdc++.h>
#include<string.h>
#include<iostream>
#include<math.h>
#include<sstream>
#include<set>
#include<queue>
#include<map>
#include<vector>
#include<algorithm>
#include<limits.h>
#define inf 0x3fffffff
#define INF 0x3f3f3f3f
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define ULL unsigned long long
using namespace std;
LL x[10010],y[10010];
LL a[10010][10010];
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        LL cnt=0;
        LL Max;
        LL Min;
        LL pos1,pos2;
        int cot1;
        int cot2;
        memset(x,0,sizeof(x));
        memset(y,0,sizeof(y));
        for(int i=1;i<=n;i++)
        {
            Min=2e11;
            pos1=0;
            cot1=0;
            for(int j=1;j<=n;j++)
            {
                cin>>a[i][j];
                if(a[i][j]<Min)
                {
                    Min=a[i][j];
                    pos1=j;
                }
            }
            for(int j=1; j<=n; j++)
            {
                if(a[i][j]==Min)
                {
                    cot1++;
                }
            }
            if(cot1==1)
            {
                x[i]=pos1;
            }
            else
            {
                x[i]=-1;
            }
        }
        for(int i=1;i<=n;i++)
        {
            Max=-1;
            pos2=0;
            cot2=0;
            for(int j=1;j<=n;j++)
            {
               // cin>>a[i][j];
                if(a[j][i]>Max)
                {
                    Max=a[j][i];
                    pos2=j;
                }
            }
            for(int j=1; j<=n; j++)
            {
                if(a[j][i]==Max)
                {
                    cot2++;
                }
            }
            if(cot2==1)
            {
                y[i]=pos2;
            }
            else
            {
                y[i]=-1;
            }
        }
        for(int i=1;i<=n;i++)
        {
            if(x[i]!=-1&&y[x[i]]==i)
            {
                cnt++;
            }
        }
        printf("%d
",cnt);
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/yinghualuowu/p/6084479.html