C. A Tale of Two Lands
程序员文章站
2024-03-06 20:20:08
...
链接:http://codeforces.com/contest/1166/problem/C
题意:问有多少组(俩俩一组)组合,能使|a|,|b|在|a+b|和|a-b|之间
题解:主要是要理解性质,同时思考如何节省时间。
代码:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<stack>
#include<cstring>
#include<map>
#include<queue>
#include<cmath>
#include<set>
#include<deque>
#define INF 0x3f3f3f3f
#define lowbit(a) ((a)&-(a))
#define speed std::ios::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
using namespace std;
typedef long long ll;
queue<ll> q;
stack<ll> s;
deque<ll> deq;
priority_queue<ll> pq;
const ll maxn = 200005;
ll n,m,l,r;
ll ans=0;
ll a[maxn];
int main()
{
cin>>n;
for(ll i=0;i<n;i++){
cin>>a[i];
a[i]=abs(a[i]); //只要绝对值 a[r]<=2*a[i] 即可
}
sort(a,a+n);
r=0;
for(ll i=0;i<n;i++){
while((r<n)&&(a[r]<=2*a[i])){ //由于递增的性质,所以r不必从头再开始
r++;
}
ans+=(r-i-1); //减头减尾
}
cout<<ans<<endl;
}