Codeforces Round #153 (Div. 1), problem: (A) Points on Line:
http://codeforces.com/problemset/problem/251/A
Codeforces Round #153 (Div. 1), problem: (A) Points on Line editorial:
http://codeforces.com/blog/entry/6054
Codeforces Round #153 (Div. 1), problem: (A) Points on Line solution: http://ideone.com/N2cHDh
#include <iostream>
#include <cstdio>
using namespace std;
int main() {
int n, d, x[100005];
long long int ans=0;
scanf("%d%d", &n, &d);
for(int i=0; i<n; i++) scanf("%d", x+i);
for(int i=0, j=0; i<n; i++) {
while(x[i]-x[j]>d) j++;
ans+=(long long)(i-j)*(i-j-1)/2;
}
printf("%I64d", ans);
}
i tried the code below but it gave WA:
http://ideone.com/e8VzQB
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <map>
using namespace std;
int main() {
int n, d, x[100005], it;
long long int cnt1, cnt2, tmp=-1, ans=0, ma=0, mi=0, dif=0;
const int con=1e9+1;
map <int, int> m;
scanf("%d%d", &n, &d);
for(int i=0; i<n; i++) scanf("%d", &x[i]);
for(int i=1; i<n; i++) for(int j=x[i-1]; j<x[i]; j++) m[j]=i;
m[x[n-1]]=n;
for(int i=0; i<n; i++) {
if(x[i]+d>=x[n-1]) it=x[n-1];
else it=x[i]+d;
dif=m[it]-m[x[i]]+1;
if(dif>=3) {
if(m[it]==tmp) continue;
cnt1=1, cnt2=1;
for(int j=1; j<=dif; j++) cnt1*=j;
for(int j=1; j<=dif-3; j++) cnt2*=j;
ans+=cnt1/(cnt2*6);
tmp=m[it];
}
}
printf("%I64d", ans);
return 0;
}
note: if you give right AC approach similar to my WA approach, that would be appreciated