Codeforces 1166C A Tale of Two Lands

博客介绍了通过枚举x,计算每次符合条件y的个数来求和对数。利用upper_bound找到第一个大于2x的位置pos,通过pos - 1得到小于等于2x的位置,结合x的位置i,计算出符合条件的数量。

在这里插入图片描述
枚举x 每次符合条件y的个数 即为对数求和
x<y<=2x upper_bound第一个大于2x 位置pos pos-1为<=2x
i为x位置 i+1为>x
pos-1-(i+1)+1

#include<bits/stdc++.h>
using namespace std;
#define IO ios::sync_with_stdio(false)
#define ll long long
#define mp make_pair
#define fi first
#define se second
#define pb emplace_back
#define pii pair<int,int>
#define all(n) (n).begin(),(n).end()
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define QAQ sort(co+1,co+1+n)
const int maxn = 2e5 + 5;
const ll mod = 998244353;
const int inf = 0x3f3f3f3f;

int a[maxn];
int main()
{
	int n,t;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>t;
		a[i]=abs(t);
	}
	sort(a+1,a+n+1);
	ll ans=0;		//全都符合有多少对 2e5*(2e5-1)/2=1e5*2e5=2e10 
	for(int i=1;i<=n;i++)	//x<y<=2*x 枚举每一个x符合条件的y有多少个 
	{
		 int pos=upper_bound(a+1,a+n+1,2*a[i])-a;
	//	 cout<<pos-1<<endl;//<=2*x的位置 
		 //从i+1开始大于x的数 
		 ans+=(pos-1-(i+1)+1);
	}
	cout<<ans<<endl;
	return 0;
}
/*

    */
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值