P1102 A-B 数对

https://www.luogu.com.cn/problem/P1102

方法一:计数(只能拿92,有一个测试点过不了)

#include
using namespace std;
int n, c, a[200005];
int b[1<<28];//注意数组最大下标
long long ans;
int main()
{
	cin>>n>>c;
	for(int i=0; i>a[i],b[a[i]]++;//注意a[i]的取值范围
	
	sort(a, a+n);
	
	for(int i=0; i 
 

方法二:MAP 

#include
#include 
#include

using namespace std;

int a[200005];//桶存储每个出现过的数的次数 
map tong;

int main()
{
	int n,c;
	scanf("%d%d",&n,&c);
	for(int i = 1;i <= n;++ i)
	{
		scanf("%d",&a[i]);
		tong[a[i]] ++;//计数 
	}
	long long js = 0;
	for(int i = 1;i <= n;++ i)
 	{//这里倒着想不去找两个数而是找一个然后再找另一个 
 		js += tong[a[i] + c];
	}
	printf("%lld\n",js);
	return 0;
 } 

方法三:STL之lower_bound/upper_bound

#include
using namespace std;
int n, c, a[200005];
long long ans;
int main()
{
	cin>>n>>c;
	for(int i=0; i>a[i];
	
	sort(a, a+n);
	
	for(int i=0; i 
 

方法四:双指针

https://www.cnblogs.com/acioi/p/11645889.html

你可能感兴趣的:(P1102 A-B 数对)