Cow Gymnastics//队列//排位1

Cow Gymnastics//队列//排位1


题目

In order to improve their physical fitness, the cows have taken up gymnastics! Farmer John designates his favorite cow Bessie to coach the N other cows and to assess their progress as they learn various gymnastic skills. In each of K practice sessions (1≤K≤10), Bessie ranks the N cows according to their performance (1≤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
The first line of the input file contains two positive integers K and N. The next K lines will each contain the integers 1…N in some order, indicating the rankings of the cows (cows are identified by the numbers 1…N). If A appears before B in one of these lines, that means cow A did better than cow B.

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

Example
inputCopy
3 4
4 1 2 3
4 1 3 2
4 2 1 3
outputCopy
4
Note
The consistent pairs of cows are (1,4), (2,4), (3,4), and (1,3).
题意
有多少对数字,出现在已给出的序列中的顺序相同

思路

用队列来储存前面有的数列,这一次没有出现就pop,最后留下的就是每次序列先后都一样的,中间暴力寻找位置即可

代码

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <deque>
#include <queue>
#include <cmath>
#include <stack>
#define pi 3.1415926
using namespace std;
typedef long long ll;
const ll mod = 1e9+7;

int g[100][100];
typedef pair<int,int> p;
queue<p> q;
int main()
{
    int k,n;
    scanf("%d%d",&k,&n);
    for(int i=0;i<k;i++){
        for(int j=0;j<n;j++){
            scanf("%d",&g[i][j]);
        }
    }
    for(int i=0;i<k;i++){
        if(i==0){
            for(int j=0;j<n;j++){
                for(int l=j+1;l<n;l++){
                    p temp;
                    temp.first=g[i][j];
                    temp.second=g[i][l];
                    q.push(temp);
                }
            }
        }
        else{
            int cnt=q.size();
            while(!q.empty()&&cnt){
                p temp=q.front();
                int grade1,grade2;
                for(int j=0;j<n;j++){
                    if(g[i][j]==temp.first) grade1=j;
                    if(g[i][j]==temp.second) grade2=j;
                }
                q.pop();
                if(grade1<grade2) {q.push(temp);}
                cnt--;
            }
        }
    }
    int ans=q.size();
    cout<<ans<<endl;
    return 0;
}

注意

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值