Advertisement
Bekzhan

CF 153 div 2 C

Dec 7th, 2012
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.22 KB | None | 0 0
  1. /****************************************
  2. **     Solution by Bekzhan Kassenov    **
  3. ****************************************/
  4.  
  5. #include <iostream>
  6. #include <cstdio>
  7. #include <fstream>
  8. #include <sstream>
  9. #include <string>
  10. #include <vector>
  11. #include <deque>
  12. #include <queue>
  13. #include <stack>
  14. #include <map>
  15. #include <set>
  16. #include <list>
  17. #include <algorithm>
  18. #include <cstdlib>
  19. #include <cmath>
  20. #include <ctime>
  21. #include <iomanip>
  22.  
  23. using namespace std;
  24.  
  25. #define PII pair <int, int>
  26. #define F first
  27. #define S second
  28. #define MP make_pair
  29. #define PB push_back
  30. #define VI vector <int>
  31. #define sqr(x) (x) * (x)
  32. #define INF (int) (2e9)
  33. #define MOD (1000 * 1000 * 1000 + 7)
  34. #define ull unsigned long long
  35. #define ll long long
  36.  
  37. int main()
  38. {
  39.     #ifndef ONLINE_JUDGE
  40.         freopen("in", "r", stdin);
  41.     #endif
  42.  
  43.     int n, d;
  44.  
  45.     cin >> n >> d;
  46.  
  47.     VI a(n);
  48.  
  49.     for (int i = 0; i < n; i++)
  50.         cin >> a[i];
  51.  
  52.     VI uk(n, -1);
  53.  
  54.     for (int i = 0; i < n; i++)
  55.         {
  56.             VI :: const_iterator it = upper_bound(a.begin(), a.end(), a[i] + d);
  57.  
  58.             uk[i] = n - (a.end() - it);
  59.         }
  60.  
  61.     ll ans = 0;
  62.  
  63.     for (int i = 0; i < n; i++)
  64.         {
  65.             ans += ((uk[i] - i - 1) * 1ll * (uk[i] - i - 2)) / 2;
  66.         }
  67.  
  68.     cout << ans;
  69.  
  70.  
  71.     return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement