Submission #109401

# Submission time Handle Problem Language Result Execution time Memory
109401 2019-05-06T11:33:32 Z JustasZ Ancient Books (IOI17_books) C++14
0 / 100
62 ms 35764 KB
#include "books.h"
#include <bits/stdc++.h>
#define pb push_back
#define x first
#define y second
#define all(a) a.begin(), a.end()
#define sz(a) (int)a.size()
#define rd() abs((int)rng())
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
const int maxn = 1e6 + 100;
const int mod = 1e9 + 7;
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
int n, s, val[maxn], nxt, dist[maxn];
vector<vector<int> > edge;
vector<int>p;
vector<pii>adj[maxn];
ll ans;
void clr()
{
    for(int i = 1; i <= nxt; i++)
        adj[i].clear();
    ans = 0;
    nxt = 0;
    memset(val, 0, sizeof val);
}
ll minimum_walk(vector<int>perm, int S)
{
    clr();
    s = S;
    p = perm;
    n = sz(p);
    set<int>one;
    for(int i = 0; i < n; i++)
    {
        if(!val[i])
        {
            int col = ++nxt, cur = i, cnt = 0, chk = 0;
            while(!val[cur])
            {
                if(cur == s)
                    chk = 1;
                cnt++;
                val[cur] = col;
                ans += abs(p[cur] - cur);
                cur = p[cur];
            }
            if(cnt == 1 && !chk)
                one.insert(col);
        }
    }
    vector<int>belong[nxt + 1];
    for(int i = 0; i < n; i++)
        belong[val[i]].pb(i);
    edge = vector<vector<int> > (nxt + 1, vector<int>(nxt + 1, mod));
    for(int i = 0; i < n; i++)
    {
        for(int j = i + 1; j < n; j++)
        {
            if(val[i] != val[j])
                edge[val[i]][val[j]] = min(edge[val[i]][val[j]], j - i);
        }
    }
    for(int i = 1; i <= nxt; i++)
    {
        for(int j = 1; j <= nxt; j++)
        {
            if(i == j)
                continue;
            for(int a : belong[i])
            {
                for(int b : belong[j])
                {
                    if((a < b && p[a] > b) || (a > b && p[a] < b))
                        edge[i][j] = 0;
                }
            }
        }
    }
    for(int i = 1; i <= nxt; i++)
        for(int j = i + 1; j <= nxt; j++)
            {
                adj[i].pb({j, edge[i][j]});
                adj[j].pb({i, edge[j][i]});
            }

    for(int i = 1; i <= nxt; i++)
    {
        for(int j = 1; j <= nxt; j++)
        {
            if(i == j)
                continue;
            /*for(pii to : adj[i])
            {
                cout << i << " " << to.x << " " << to.y << endl;
            }*/
        }
    }
    set<pii>se;
    for(int i = 1; i <= nxt; i++)
        dist[i] = mod;
    se.insert({dist[val[s]] = 0, val[s]});
    while(sz(se))
    {
        int v = se.begin()->second;
        if(dist[v] != mod)
            ans += dist[v] * 2;
        //cout << v << " " << dist[v] << endl;
        se.erase(se.begin());
        for(pii to : adj[v])
        {
            if(dist[to.x] > to.y)
            {
                //cout << v << " " << to.x << " " << to.y << endl;
                if(dist[to.x] != mod)
                    se.erase({dist[to.x], to.x});
                dist[to.x] = to.y;
                se.insert({dist[to.x], to.x});
            }
        }
    }
    //int add = mod;
    //for(int i = 1; i <= nxt; i++)
    //    if(i != startcol) add = min(add, edge[i][startcol]);
    //ans += add;
    //cout << "Startcol: " << val[s] << endl;
    return ans;
}
/*int main()
{
    ios_base::sync_with_stdio(false), cin.tie(0);
    vector<int>perm;
    for(int i = 0; i < 4; i++)
        perm.pb(i);
    do
    {
        for(int i = 0; i < 4; i++)
        {
            clr();
            cout << "permutaiton ";
            for(int a : perm)
                cout << a << " ";
            cout << endl;
            cout << "start: " << i << " " << minimum_walk(perm, i) << endl;
        }
    } while(next_permutation(all(perm)));
    ifstream fin("in.txt");
    int n, s;
    fin >> n >> s;
    perm.resize(n);
    for(int i = 0; i < n; i++)
        fin >> perm[i];
    cout << minimum_walk(perm, s) << endl;
    //cout << minimum_walk({3, 2, 1, 0}, 0);
    return 0;
}*/
# Verdict Execution time Memory Grader output
1 Correct 32 ms 27776 KB Output is correct
2 Correct 32 ms 27776 KB Output is correct
3 Correct 32 ms 27768 KB Output is correct
4 Correct 29 ms 27776 KB Output is correct
5 Incorrect 31 ms 27748 KB 3rd lines differ - on the 1st token, expected: '4', found: '6'
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 32 ms 27776 KB Output is correct
2 Correct 32 ms 27776 KB Output is correct
3 Correct 32 ms 27768 KB Output is correct
4 Correct 29 ms 27776 KB Output is correct
5 Incorrect 31 ms 27748 KB 3rd lines differ - on the 1st token, expected: '4', found: '6'
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 32 ms 27776 KB Output is correct
2 Correct 32 ms 27776 KB Output is correct
3 Correct 32 ms 27768 KB Output is correct
4 Correct 29 ms 27776 KB Output is correct
5 Incorrect 31 ms 27748 KB 3rd lines differ - on the 1st token, expected: '4', found: '6'
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 62 ms 35764 KB 3rd lines differ - on the 1st token, expected: '3304', found: '3506'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 32 ms 27776 KB Output is correct
2 Correct 32 ms 27776 KB Output is correct
3 Correct 32 ms 27768 KB Output is correct
4 Correct 29 ms 27776 KB Output is correct
5 Incorrect 31 ms 27748 KB 3rd lines differ - on the 1st token, expected: '4', found: '6'
6 Halted 0 ms 0 KB -