#include<cstdio>
#include<vector>
using namespace std;
int main() {
int length, limit;
scanf("%d %d\n", &length, &limit);
vector<int> segments(length);
for (int i = 0; i < length; i++) {
int segment;
scanf("%d", &segment);
segments[i] = segment;
}
vector<bool> visited(length, false);
int trucks = 0;
for (int i = 0; i < length; i++) {
int max = 0;
int maxIx = -1;
for (int j = 0; j < length; j++) {
if (segments[j] > max && !visited[j]) {
max = segments[j];
maxIx = j;
}
}
if (maxIx > 0 && segments[maxIx] - segments[maxIx - 1] > limit) {
trucks += (segments[maxIx] - limit) - segments[maxIx - 1];
segments[maxIx - 1] = segments[maxIx] - limit;
}
if (maxIx < length - 1 && segments[maxIx] - segments[maxIx + 1] > limit) {
trucks += (segments[maxIx] - limit) - segments[maxIx + 1];
segments[maxIx + 1] = segments[maxIx] - limit;
}
visited[maxIx] = true;
}
printf("%d\n", trucks);
return 0;
}
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | #include<cstdio> #include<vector> using namespace std; int main() { int length, limit; scanf("%d %d\n", &length, &limit); vector<int> segments(length); for (int i = 0; i < length; i++) { int segment; scanf("%d", &segment); segments[i] = segment; } vector<bool> visited(length, false); int trucks = 0; for (int i = 0; i < length; i++) { int max = 0; int maxIx = -1; for (int j = 0; j < length; j++) { if (segments[j] > max && !visited[j]) { max = segments[j]; maxIx = j; } } if (maxIx > 0 && segments[maxIx] - segments[maxIx - 1] > limit) { trucks += (segments[maxIx] - limit) - segments[maxIx - 1]; segments[maxIx - 1] = segments[maxIx] - limit; } if (maxIx < length - 1 && segments[maxIx] - segments[maxIx + 1] > limit) { trucks += (segments[maxIx] - limit) - segments[maxIx + 1]; segments[maxIx + 1] = segments[maxIx] - limit; } visited[maxIx] = true; } printf("%d\n", trucks); return 0; } |
English