水题:根据绝对值大小排序,遍历即可
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
struct num{
int val,symbol;
friend bool operator <(num a,num b){
return a.val<b.val;
}
};
int main(){
int ncase;
cin >> ncase;
vector<num> g;
while(ncase--){
g.clear();
int n;
scanf("%d",&n);
for(int i = 0; i < n; i++){
int tmp;
num t;
scanf("%d",&tmp);
if(tmp > 0){
t.val = tmp;
t.symbol = 1;
}else{
t.val = -tmp;
t.symbol = 0;
}
g.push_back(t);
}
sort(g.begin(),g.end());
int ans = 1;
bool flag = g[0].symbol;
for(int i = 1; i < g.size(); i++){
while(flag == g[i].symbol && i < g.size()){
i++;
}
if(flag != g[i].symbol && i < g.size()){
ans++;
flag = g[i].symbol;
}
}
cout<<ans<<endl;
}
return 0;
}