跳到正文

题解

AT_abc388_c 题解

ikunTLE约 2 分钟
原文
文章信息与版本

文章信息

发布
2025年1月12日
收录
2025年12月12日
更新
2026年11月13日
篇幅
约 2 分钟
获赞
10

版本

  1. v1当前

题目传送门

思路

可以使用双指针解决此题。

首先将序列按升序排序。用两个循环变量,ii 在左,jj 在右,则 AiA_i 表示上面的麻薯,AjA_j 表示下面的麻薯。每一次寻找最小的 AjA_j 满足 Ai≤Aj2A_i\le\frac{A_j}{2},那么 ii 到 jj 的区间全部满足要求,故将答案增加 j−i+1j-i+1 即可。

由于 ii 和 jj 只会向后移动,时间复杂度为 NN 级别,最大时间复杂度来自排序,为 O(Nlog⁡N)\mathcal{O}(N\log N),可以通过此题。

AC CODE

C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
int read(){int x=0;char f=1,ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();return x*f;}
const int N=5e5+10;
int a[N];
signed main(){
	int n=read();
	for(int i=1;i<=n;++i)
		a[i]=read();
	sort(a+1,a+n+1);
	int ans=0;
	for(int i=1,j=1;i<=n;++i){
		while(j<=n&&a[i]*2>a[j])
			++j;
		ans+=n-j+1;
	}
	printf("%lld\n",ans);
	return 0;
}
原文
存档评论3 条抓取于