Submission #578455

# Submission time Handle Problem Language Result Execution time Memory
578455 2022-06-17T01:47:30 Z definitelynotmee Ancient Books (IOI17_books) C++
50 / 100
314 ms 97612 KB
#include<bits/stdc++.h>
#include "books.h"
using namespace std;
#define ff first
#define ss second
#define all(x) x.begin(), x.end()
using ll = long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
template<typename T>
using matrix = vector<vector<T>>;
const int INF = 1e9;
long long minimum_walk(std::vector<int> p, int s) {
    int n = p.size();
    ll resp =0;

    for(int i = 0; i < n; i++){
        resp+=abs(i-p[i]);
    }
    vector<pii> crange(n);
    vector<int> cycle(n), check(n);
    auto dfs = [&](int id, int cid, auto dfs)->int{
        cycle[id] = cid;
        check[id] = 1;
        if(check[p[id]])
            return id;
        return max(id,dfs(p[id],cid,dfs));
    };
    vector<int> isgood(n);
    vector<pii> cycles;
    
    for(int i = 0; i < n; i++){
        if(!check[i]){
            int r = dfs(i,i,dfs);
            crange[i] = {i,r};
            isgood[i] = cycles.empty() || cycles.back().ss < r;
            if(r-i && isgood[i]){
                if(cycles.size() && cycles.back().ss > i){
                    int l = cycles.back().ff;
                    cycles.pop_back();
                    cycles.push_back({l,r});
                } else {
                    cycles.push_back({i,r});
                }
            }
        }
    }
    
    for(int i = 0; i < (int)cycles.size()-1; i++){
        resp+= 2*(cycles[i+1].ff-cycles[i].ss);
    }
    if(cycles.empty())
        return resp;
    if(s < cycles[0].ff || s > cycles.back().ss){
        resp+=max(cycles[0].ff-s, s-cycles.back().ss)*2;
        return resp;
    }
    
    matrix<pii> g(n);
    for(int i = 0; i < n; i++){
        int cur = cycle[i];
        if(i!=n-1)
            g[cur].push_back({cycle[i+1], crange[cur].ss >= i+1 ? 0 : 2});
        if(i)
            g[cur].push_back({cycle[i-1], crange[cur].ff <= i-1 ? 0 : 2});
    }
    
    priority_queue<pii,vector<pii>,greater<pii>> pq;
    vector<int> dist(n,INF);
    pq.push({0,cycle[s]});
    dist[cycle[s]] = 0;

    while(!pq.empty()){
        int cur = pq.top().ff, curp = pq.top().ss;
        pq.pop();
        if(curp> dist[cur])
            continue;
        for(auto [viz,vizp] : g[cur]){
            if(dist[viz] > vizp+dist[cur]){
                dist[viz]= vizp+dist[cur];
                pq.push({viz,dist[viz]});
            }
        }
    }
    
    int add = INF;
    for(int i = 0; i < n; i++){
        if(isgood[i])
            add = min(add,dist[i]);
    }
    resp+=add;
    return resp;
}

Compilation message

books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:78:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   78 |         for(auto [viz,vizp] : g[cur]){
      |                  ^
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 300 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 1 ms 296 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 304 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 300 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 1 ms 296 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 304 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 258 ms 97612 KB Output is correct
31 Correct 276 ms 84916 KB Output is correct
32 Correct 110 ms 29744 KB Output is correct
33 Correct 108 ms 31784 KB Output is correct
34 Correct 127 ms 31764 KB Output is correct
35 Correct 245 ms 84656 KB Output is correct
36 Correct 236 ms 81768 KB Output is correct
37 Correct 169 ms 79708 KB Output is correct
38 Correct 152 ms 77632 KB Output is correct
39 Correct 177 ms 77172 KB Output is correct
40 Correct 155 ms 76148 KB Output is correct
41 Correct 191 ms 84612 KB Output is correct
42 Correct 192 ms 79684 KB Output is correct
43 Correct 314 ms 84468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 340 KB 3rd lines differ - on the 1st token, expected: '3304', found: '1000002744'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 300 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 1 ms 296 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 304 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 258 ms 97612 KB Output is correct
31 Correct 276 ms 84916 KB Output is correct
32 Correct 110 ms 29744 KB Output is correct
33 Correct 108 ms 31784 KB Output is correct
34 Correct 127 ms 31764 KB Output is correct
35 Correct 245 ms 84656 KB Output is correct
36 Correct 236 ms 81768 KB Output is correct
37 Correct 169 ms 79708 KB Output is correct
38 Correct 152 ms 77632 KB Output is correct
39 Correct 177 ms 77172 KB Output is correct
40 Correct 155 ms 76148 KB Output is correct
41 Correct 191 ms 84612 KB Output is correct
42 Correct 192 ms 79684 KB Output is correct
43 Correct 314 ms 84468 KB Output is correct
44 Incorrect 1 ms 340 KB 3rd lines differ - on the 1st token, expected: '3304', found: '1000002744'
45 Halted 0 ms 0 KB -