Description
有一个含有n个元素的队列q,每个元素的大小满足1<=xi<=9(0<i<n)。队列有一种操作,对于队首元素若是整个队列最大的则出队列,否则加入队尾。对于一个给定的m,你能告诉我xm是第几个出队列的吗?
Input
输入数据第一行是一个整数T(1<=T<=1000),表示输入数据的组数;每组数据的第一行是两正整数n表示队列的大小和第几个元素(1<n<=1000,0<=m<n),第二行有n个数xi ,分别代表每个元素的大小。
Output
对于每组测试数据,输出xm是第几个出队列。
Sample Input
3
1 0
5
4 2
1 2 3 4
6 0
1 1 9 1 1 1
Sample Output
1
2
5
用队列模拟即可
#include<bits/stdc++.h>
using namespace std;
#define pi acos(-1)
#define mod 998244353
#define INF 0x3f3f3f
#define fi first
#define se second
#define it iterator
#define ins insert
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define np next_permutation
#define pq priority_queue
#define ll long long
#define ull unsigned long long
#define mem(a) memset(a,0,sizeof(a))
#define cio ios::sync_with_stdio(false)
#define gcd __gcd
ll lgcd(ll a,ll b){return b == 0? a:lgcd(b, a % b);}
int lowbit(int x){return x&(-x);}
#define T int t;scanf("%d",&t);while(t--)
#define Ce cout << endl
#define CE(n) cout << n << endl
#define C(n) cout << n;
#define CY cout << "YES" <<
#define CN cout << "NO" << endl
#define Cy cout << "Yes" << endl
#define Cn cout << "No" << endl
struct node
{
int num, x;
}s[1010];
bool cmp(node a, node b)
{
return a.x < b.x;
}
int main()
{
T{
int n, m;
cin >> n >> m;
queue<node>q;
for(int i = 0; i < n; i++){
cin >> s[i].x;
s[i].num = i; // 用于出队列时判断是否是最大值
q.push(s[i]);
}
sort(s,s+n,cmp);
n--; // 当前最大值的下标
int cnt = 1;
while(1){
node a = q.front(); // 取队列头元素
if(a.num==m&&a.x==s[n].x){ // 是最大值,且是xm的序号直接输出
CE(cnt);
break;
}
q.pop(); // 取出头元素
if(a.x==s[n].x){ // 是最大值,不用加入队尾
n--; // 最大值更新
cnt++; // 出队次数
}else{
q.push(a); // 非最大值,加入队尾
}
}
}
return 0;
}