题目请点我
题解:
这道题不难,简单的模拟吧。但是收获是定义优先队列的优先级。同学还有一种思路是只放入一个数组中,重载sort比较函数后输出,也很棒。
代码实现:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
using namespace std;
方法一:放入结构体,重载运算符
//struct point{
// int x;
// bool operator <( const point* a){
// return this->x > a->x;
// }
//};
//
方法二:定义优先队列时指定好
//priority_queue< int, vector<int>, greater<int> > Q1;
//priority_queue< int, vector<int>, greater<int> > Q2;
int T;
int Num;
int result;
bool flag1,flag2;
priority_queue< int, vector<int>, greater<int> > Q1;
priority_queue< int, vector<int>, greater<int> > Q2;
int main()
{
scanf("%d",&T);
while( T-- ){
while( !Q1.empty() ){
Q1.pop();
}
while( !Q2.empty() ){
Q2.pop();
}
scanf("%d",&Num);
int tmp;
int turn;
int fir1,fir2;
int result = 0;
int tag;
flag1 = flag2 = true;
for( int i = 0; i < Num; i++ ){
scanf("%d",&tmp);
if( tmp > 0 ){
Q1.push(tmp);
}
else{
Q2.push(-tmp);
}
}
fir1 = Q1.top();
//Q1.pop();Q2.pop();
fir2 = Q2.top();
if( fir1 > fir2 ){
turn = 2;
}
else{
turn = 1;
}
while( 1 ){
if( turn == 1 ){
if( Q1.empty() ){
break;
}
result++;
tag = Q1.top();
turn = 2;
Q1.pop();
}
else{
if( Q2.empty() ){
break;
}
result++;
turn = 1;
tag = Q2.top();
Q2.pop();
}
fir1 = Q1.top();
fir2 = Q2.top();
while( turn == 1 && fir1 < tag && !Q1.empty() ){
Q1.pop();
fir1 = Q1.top();
}
while( turn == 2 && tag > fir2 && !Q2.empty() ){
Q2.pop();
fir2 = Q2.top();
}
}
printf("%d\n",result);
}
return 0;
}