有没有大佬看一眼,我感觉我的思路没问题呀
查看原帖
有没有大佬看一眼,我感觉我的思路没问题呀
1180146
kaito_936楼主2024/4/11 21:32

举个例子:排序后数组为1 1 2 2 3 4,l就是指向相同元素列的开头位置,j在a[i]-a[j]>c的条件后跳转到相同元素列的尾后元素;temp就是用来记录c+a[j]的那个元素列的个数。我觉得我的思路没有任何问题,但是就是过不了2、4、8、9,是我忽略了哪些情况吗,

#include<iostream>
#include<algorithm>
#include<map>
using namespace std;
long long int n, c, temp, ans,a[200001],j,l,i;

int main(){
    cin >> n >> c;
    

    for (i = 0; i < n; i++) cin >> a[i];
    sort(a, a + n);
    a[n] = a[n - 1] + 1;
    for (i = 0; i <= n; i++) {
	while (a[i] - a[j] > c&&a[j]==a[l]) j++;
	if (j != l) {
	    ans += temp * (j - l);
	    temp = 0;
	    l = j;
	}
	if (a[i] - a[j] == c) temp++;
    }
    cout << ans;

    return 0;
 }
2024/4/11 21:32
加载中...