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;
}