跳到正文

题解

AT_abc388_e 题解

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

文章信息

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

版本

  1. v1当前

题目传送门

思路

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

首先考虑答案最大的情况,那就是每一个麻薯都能与另一个配对,答案为 ⌊N2⌋\lfloor\frac{N}{2}\rfloor。

序列已经按照升序排序。为了使答案最大,我们用两个指针,ii 从 11 开始,jj 从一半(⌊N2⌋+1\lfloor\frac{N}{2}\rfloor+1)开始遍历。这样做一定能使答案最大,比如若 jj 过于靠前会导致前面数不足,反之若靠后会导致后面的数不足,无法使答案最大化。遍历时,若 Ai≤Aj2A_i\le\frac{A_j}{2},说明满足条件,继续向后遍历,将 ii 和 jj 都增加 11,同时答案增加 11;否则说明 AjA_j 不够大,只将 jj 增加 11。

由于 ii 和 jj 只会向后移动,时间复杂度为 O(N)\mathcal{O}(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 hn=n/2,ans=0;
	for(int i=1,j=hn+1;i<=hn&&j<=n;){
		if(a[i]*2<=a[j])
			++i,++j,++ans;
		else ++j;
	}
	printf("%lld\n",ans);
	return 0;
}
原文
存档评论8 条抓取于