Advertisement
Guest User

Untitled

a guest
Jan 26th, 2020
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.09 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cmath>
  4. #include <vector>
  5. #include <string>
  6. #include <ctime>
  7.  
  8. using namespace std;
  9.  
  10. template <typename T>
  11. T gcd(T a, T b) {
  12.     while (a && b) {
  13.         a %= b;
  14.         swap(a, b);
  15.     }
  16.     return a + b;
  17. }
  18.  
  19. int main() {
  20.     ios_base::sync_with_stdio(false);
  21.     cin.tie(NULL);
  22.    
  23.     int n;
  24.     long long r;
  25.     cin >> n >> r;
  26.     vector<long long> a(n);
  27.     for (int i = 0; i < n; ++i) {
  28.         cin >> a[i];
  29.     }
  30.  
  31.     int first = 0;
  32.     int second = 0;
  33.  
  34.     while (a[second] - a[first] <= r) {
  35.         second++;
  36.         if (second == n) {
  37.             break;
  38.         }
  39.     }
  40.  
  41.     long long ans = 0;
  42.     while (second < n) {
  43.         ans += n - second;
  44.         first++;
  45.         while (a[second] - a[first] <= r) {
  46.             second++;
  47.             if (second == n) {
  48.                 break;
  49.             }
  50.         }
  51.     }
  52.  
  53.     while (a[second] - a[first] < r) {
  54.         ans += n - second;
  55.         first++;
  56.         if (first == n) {
  57.             break;
  58.         }
  59.     }
  60.  
  61.     cout << ans;
  62.  
  63.     return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement