Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- struct T {
- int p; //pocetok
- //int k; //kraj
- int r; //razlika
- };
- int main() {
- int n;
- cin >> n;
- vector<T> t(n); //towers
- for (int i = 0; i < n; ++i) {
- cin >> t[i].p;
- }
- for (int i = 0; i < n; ++i) {
- int k, r;
- cin >> k;
- //t[i].k = k;
- r = k - t[i].p; //kraj - pocetok
- t[i].r = r;
- }
- int moves = 0;
- { //doing this out of scope because why not?
- int rMax = t[0].r, rMaxSign = rMax > 0 ? 1 : rMax < 0 ? -1 : 0, r = rMax, sign = rMaxSign, incr;
- for (int j = 0; j < n; ++j) {
- r = t[j].r;
- sign = r > 0 ? 1 : r < 0 ? -1 : 0;
- if (r > 0 && rMax > 0 && r > rMax) {
- rMax = r;
- } else if (r < 0 && rMax < 0 && r < rMax) {
- rMax = r;
- }
- if (rMaxSign != sign) {
- incr = rMaxSign == 1 ? rMax : -rMax;
- moves += incr;
- //cout << "(incremented moves by " << incr << ")\n";
- rMax = r;
- rMaxSign = sign;
- }
- //cout << "j:" << j << " r:" << r << " rMax:" << rMax << endl;
- }
- incr = rMaxSign == 1 ? rMax : -rMax;
- moves += incr;
- //cout << "(incremented moves by " << incr << ")\n";
- }
- cout << moves;
- }
Advertisement
Add Comment
Please, Sign In to add comment