如果數字為負數則在字串中加入1 反之則加入空白
再用 stringstream 判斷有幾段 在減掉頭尾的數量就可以啦
#include<bits/stdc++.h>
using namespace std;
int main(){
int n, ans=0;
cin>>n;
vector<pair<int, int>> k(n);
string line="", s;
for(int i=0; i<n; i++){
cin>>k[i].first;
if(k[i].first<0) k[i].second=1;
else k[i].second=0;
k[i].first=abs(k[i].first);
}
sort(k.begin(), k.end());
for(int i=0; i<n; i++){
if(k[i].second) line+="1";
else line+=" ";
}
stringstream ss(line);
while(ss>>s) ans+=2;
if(k[0]=='1') ans--;
if(k[n-1]=='1') ans--;
cout<<ans<<endl;
return 0;
}