Submission #313350

# Submission time Handle Problem Language Result Execution time Memory
313350 2020-10-15T20:39:03 Z hhh07 Ancient Books (IOI17_books) C++14
42 / 100
2000 ms 42656 KB
#include <iostream>
#include <vector>
#include <cassert>
#include "books.h"


using namespace std;

typedef vector<int> vi;

void extend(int &l, int &r, vi C, vi L, vi R){
    int ll = l, rr = r;
    ll = min(ll, min(L[C[l]], L[C[r]]));
    rr = max(rr, max(R[C[l]], R[C[r]]));
    
    while(ll < l || r < rr){
        if (ll < l){ // sve dok ll "bjezi" tj dok se nalazi min smanjivat ce se i l
            l--; // l == ll kad ne mogne dalje tj kad L[C[l]] dodje do najljevljeg moguceg
            ll = min(ll, L[C[l]]);
            rr = max(rr, R[C[l]]);
        }
        else{
            r++;
            ll = min(ll, L[C[r]]);
            rr = max(rr, R[C[r]]);
        }
    }
        
}

int connect(int l, int r, int target_l, int target_r, vi &C, vi &L, vi &R){
    int cost = 0;
    do{
        extend(l, r, C, L, R);
        bool next_l = false;
        int cost_l = 0;
        int l_l = l, r_l = r;
        while(true){
            if (l_l <= target_l)
                break;
            l_l--;
            cost_l += 2;
            extend(l_l, r_l, C, L, R);
            if (r_l > r){
                next_l = true;
                break;
            }
        }
        bool next_r = false;
        int cost_r = 0;
        int l_r = l, r_r = r;
        while(true){
            if (r_r >= target_r)
                break;
            r_r++;
            cost_r += 2;
            extend(l_r, r_r, C, L, R);
            if (l_r < l){
                next_r = true;
                break;
            }
        }
        assert(!(next_l xor next_r));
        if (next_l && next_r)
            cost += min(cost_l, cost_r);
        else
            cost += cost_l + cost_r;
        l = min(l_l, l_r);
        r = max(r_l, r_r);
    }while(l > target_l || r < target_r);
    
    return cost;
}

long long booksort(int n, int s, vector<int> order){
    long long res = 0;
    
    vector<int> C(n, -1), L(n), R(n);
    int l = s, r = s;
    int c = 0;
    
    for (int i = 0; i < n; i++){
        res += abs(i - order[i]);
        if (C[i] == -1){
            L[c] = i; R[c] = i;
            int j = i;
            do{
                C[j] = c;
                R[c] = max(R[c], j);
                j = order[j];
            }while(i != j);
            if (i != order[i]){
                l = min(l, L[c]);
                r = max(r, R[c]);
            }
            c++;
        }
    }
    
    return res + connect(s, s, l, r, C, L, R);
}

long long minimum_walk(vi order, int s){
    int n = order.size();
    return booksort(n, s, order);
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 0 ms 256 KB Output is correct
4 Correct 0 ms 256 KB Output is correct
5 Correct 1 ms 256 KB Output is correct
6 Correct 1 ms 276 KB Output is correct
7 Correct 0 ms 256 KB Output is correct
8 Correct 1 ms 256 KB Output is correct
9 Correct 0 ms 256 KB Output is correct
10 Correct 0 ms 256 KB Output is correct
11 Correct 1 ms 256 KB Output is correct
12 Correct 0 ms 368 KB Output is correct
13 Correct 1 ms 256 KB Output is correct
14 Correct 0 ms 256 KB Output is correct
15 Correct 1 ms 256 KB Output is correct
16 Correct 1 ms 256 KB Output is correct
17 Correct 0 ms 256 KB Output is correct
18 Correct 0 ms 256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 0 ms 256 KB Output is correct
4 Correct 0 ms 256 KB Output is correct
5 Correct 1 ms 256 KB Output is correct
6 Correct 1 ms 276 KB Output is correct
7 Correct 0 ms 256 KB Output is correct
8 Correct 1 ms 256 KB Output is correct
9 Correct 0 ms 256 KB Output is correct
10 Correct 0 ms 256 KB Output is correct
11 Correct 1 ms 256 KB Output is correct
12 Correct 0 ms 368 KB Output is correct
13 Correct 1 ms 256 KB Output is correct
14 Correct 0 ms 256 KB Output is correct
15 Correct 1 ms 256 KB Output is correct
16 Correct 1 ms 256 KB Output is correct
17 Correct 0 ms 256 KB Output is correct
18 Correct 0 ms 256 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 384 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 1 ms 384 KB Output is correct
25 Correct 1 ms 384 KB Output is correct
26 Correct 1 ms 384 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
29 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 0 ms 256 KB Output is correct
4 Correct 0 ms 256 KB Output is correct
5 Correct 1 ms 256 KB Output is correct
6 Correct 1 ms 276 KB Output is correct
7 Correct 0 ms 256 KB Output is correct
8 Correct 1 ms 256 KB Output is correct
9 Correct 0 ms 256 KB Output is correct
10 Correct 0 ms 256 KB Output is correct
11 Correct 1 ms 256 KB Output is correct
12 Correct 0 ms 368 KB Output is correct
13 Correct 1 ms 256 KB Output is correct
14 Correct 0 ms 256 KB Output is correct
15 Correct 1 ms 256 KB Output is correct
16 Correct 1 ms 256 KB Output is correct
17 Correct 0 ms 256 KB Output is correct
18 Correct 0 ms 256 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 384 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 1 ms 384 KB Output is correct
25 Correct 1 ms 384 KB Output is correct
26 Correct 1 ms 384 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
29 Correct 1 ms 384 KB Output is correct
30 Correct 262 ms 42360 KB Output is correct
31 Correct 273 ms 42232 KB Output is correct
32 Correct 184 ms 42232 KB Output is correct
33 Execution timed out 2091 ms 42656 KB Time limit exceeded
34 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 324 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 0 ms 256 KB Output is correct
4 Correct 0 ms 256 KB Output is correct
5 Correct 1 ms 256 KB Output is correct
6 Correct 1 ms 276 KB Output is correct
7 Correct 0 ms 256 KB Output is correct
8 Correct 1 ms 256 KB Output is correct
9 Correct 0 ms 256 KB Output is correct
10 Correct 0 ms 256 KB Output is correct
11 Correct 1 ms 256 KB Output is correct
12 Correct 0 ms 368 KB Output is correct
13 Correct 1 ms 256 KB Output is correct
14 Correct 0 ms 256 KB Output is correct
15 Correct 1 ms 256 KB Output is correct
16 Correct 1 ms 256 KB Output is correct
17 Correct 0 ms 256 KB Output is correct
18 Correct 0 ms 256 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 384 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 1 ms 384 KB Output is correct
25 Correct 1 ms 384 KB Output is correct
26 Correct 1 ms 384 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
29 Correct 1 ms 384 KB Output is correct
30 Correct 262 ms 42360 KB Output is correct
31 Correct 273 ms 42232 KB Output is correct
32 Correct 184 ms 42232 KB Output is correct
33 Execution timed out 2091 ms 42656 KB Time limit exceeded
34 Halted 0 ms 0 KB -