当前位置:网站首页>ZCMU--1396: 队列问题(2)
ZCMU--1396: 队列问题(2)
2022-07-07 05:05:00 【小小小Why】
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
解析:我们利用set来快速得出当前队列中最大元素值,因为有元素重复,所以我们得用multiset来存。我们可以用结构体来存元素的id和valu值,c表示第几个出列,如果a[ i ]==st.*rbegin()表示a[ i ]是当前队列中最大的元素,出列,如果是xm,那么就输出c即可,反之不是最大值,就到队尾,对应multiset中也要删除该元素,如此反复直到xm出列。
#include <stdio.h>
#include <set>
using namespace std;
struct su
{
int id;
int v;
}a[10005];
int main()
{
int t,i,n,k,c;
scanf("%d",&t);
while(t--){
multiset<int>st; //建立multiset
scanf("%d%d",&n,&k);
c=1; //表示第几个出列
for(i=0;i<n;i++){
scanf("%d",&a[i].v);
a[i].id=i;
st.insert(a[i].v); //存入set
}
for(i=0;i<n;i++){
if(a[i].v==*st.rbegin()){ //表示是当前队列最大值
if(a[i].id==k){ //刚好是xm
printf("%d\n",c);
break;
}else st.erase(--st.end()),c++; //删除最后一个元素,也就是最大值
}else{ //不是最大值,模拟到队尾
a[n].v=a[i].v;
a[n++].id=a[i].id;
}
}
st.clear();
}
return 0;
}
边栏推荐
- 2022焊工(初级)判断题及在线模拟考试
- 追风赶月莫停留,平芜尽处是春山
- Blob 對象介紹
- Network learning (I) -- basic model learning
- Implementation of replacement function of shell script
- 芯片资料 网站 易特创芯
- Introduction à l'objet blob
- Relevant data of current limiting
- These five fishing artifacts are too hot! Programmer: I know, delete it quickly!
- jeeSite 表单页面的Excel 导入功能
猜你喜欢
电池、电机技术受到很大关注,反而电控技术却很少被提及?
Open source ecosystem | create a vibrant open source community and jointly build a new open source ecosystem!
Bugku CTF daily one question chessboard with only black chess
[quick start of Digital IC Verification] 17. Basic grammar of SystemVerilog learning 4 (randomization)
Content of string
Thinkcmf6.0 installation tutorial
Force buckle 145 Binary Tree Postorder Traversal
【数字IC验证快速入门】15、SystemVerilog学习之基本语法2(操作符、类型转换、循环、Task/Function...内含实践练习)
Codeforce c.strange test and acwing
UnityHub破解&Unity破解
随机推荐
It took "7" years to build the robot framework into a micro service
[quickstart to Digital IC Validation] 15. Basic syntax for SystemVerilog Learning 2 (operator, type conversion, loop, Task / Function... Including practical exercises)
Complex network modeling (III)
【无标题】
Recursive method to verify whether a tree is a binary search tree (BST)
Blob 對象介紹
Quickly use Jacobo code coverage statistics
QT learning 26 integrated example of layout management
2022 tea master (intermediate) examination questions and mock examination
【数字IC验证快速入门】15、SystemVerilog学习之基本语法2(操作符、类型转换、循环、Task/Function...内含实践练习)
【数字IC验证快速入门】11、Verilog TestBench(VTB)入门
让Livelink初始Pose与动捕演员一致
Roulette chart 2 - writing of roulette chart code
Dedecms collects content without writing rules
Bugku CTF daily one question chessboard with only black chess
Linux server development, redis source code storage principle and data model
The legend about reading the configuration file under SRC
Who has docker to install MySQL locally?
2022 National latest fire-fighting facility operator (primary fire-fighting facility operator) simulation questions and answers
Zsh shell adds automatic completion and syntax highlighting