-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1636.h
64 lines (58 loc) · 1.15 KB
/
1636.h
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <climits>
#include <algorithm>
using namespace std;
int N;
int S[100000], E[100000];
int firstS, firstE;
vector<int> path = vector<int>();
vector<int> minPath = vector<int>();
int main_1636() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
scanf("%d", &N);
scanf("%d", &firstS);
scanf("%d", &firstE);
for (int i = 0; i < N - 1; i++) {
scanf("%d", &S[i]);
scanf("%d", &E[i]);
}
int minimum = INT_MAX;
int pos;
for (int j = firstS; j < firstE + 1; j++) {
int ret = 0;
pos = j;
path.push_back(pos);
for (int i = 0; i < N-1; i++) {
if (S[i] <= pos && pos <= E[i]) {
path.push_back(pos);
continue;
}
int a = abs(S[i] - pos);
int b = abs(E[i] - pos);
if (b < a) {
ret += b;
pos = E[i];
path.push_back(pos);
}
else {
ret += a;
pos = S[i];
path.push_back(pos);
}
}
if (ret < minimum) {
minimum = ret;
minPath.clear();
minPath.assign(path.begin(), path.end());
}
path.clear();
}
cout << minimum << endl;
for (int i = 0; i < minPath.size(); i++)
printf("%d\n", minPath[i]);
return 0;
}