-
Notifications
You must be signed in to change notification settings - Fork 0
/
P10837.cpp
31 lines (25 loc) · 919 Bytes
/
P10837.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <algorithm>
#include <functional>
#include <utility>
int begins[100000] = {0};
int contribution(int thisBegin, int lastBegin, int nextBegin, int len) {
int begin = lastBegin == -1 ? thisBegin : std::min(lastBegin + m, thisBegin);
int end = nextBegin == -1 ? thisBegin + m : std::min(thisBegin + m, nextBegin);
return (end - begin + 1);
}
int main() {
int n, m; std::cin >> n >> m;
for (int i = 0; i < n; i++) std::cin >> begins[i];
sort(begins + 1, begins + n + 1);
long long ans = 0;
for (int i = 0; i < n; i++) {
ans += contribution(begins[i], (i == 0 ? -1 : begins[i-1]), (i == n-1 ? -1 : begins[i+1]), m);
}
for (int i = 0; i < n; i++) {
long long newAns = ans;
if (i == 0)
newAns -= contribution(begins[i], (i == 0 ? -1 : begins[i-1]), (i == n-1 ? -1 : begins[i+1]), m);
}
return 0;
}