为什么re了啊啊!!
  • 板块P1102 A-B 数对
  • 楼主llqqhh
  • 当前回复0
  • 已保存回复0
  • 发布时间2022/11/26 14:31
  • 上次更新2023/10/27 01:25:39
查看原帖
为什么re了啊啊!!
525817
llqqhh楼主2022/11/26 14:31

longlong也开了,数组开的足够大了,应该也不会出现下表越界的情况,但是样例全部re,求指点

#include<iostream>
#include<algorithm>
using namespace std;

const int N = 1e6+10;

int n, m, a[N];

int main()
{
	
	cin >> n >> m;
	for(int i = 1; i <= n; i++)
	{
		scanf("%d", &a[i]);
	} 
	sort(a+1, a+n+1);
	
	long long res = 0;
	for(int i = 1, j = 2; i <= n && j <= n && i <= j;)
	{
		int cnti = 0, cntj = 0, c = 0, d = 0;
		if(a[j]-a[i] == m) 
		{
			c = i; d = j;
		}
		
		while(a[j]-a[i] == m)
		{
			j++;
			cntj++;
			res++;
		}
		j = j-cntj;
		while(a[j]-a[i] == m)
		{
			if(i == c && j == d) res--;
			i++;
			cnti++;
			res++;
		}
		while(a[j]-a[i] > m)
		{
			i++;
		} 
		while(a[j]-a[i] < m)
		{
			j++;
		}
	}
	cout << res << endl;
	return 0;
} 
2022/11/26 14:31
加载中...