Submission #400749

# Submission time Handle Problem Language Result Execution time Memory
400749 2021-05-08T15:45:12 Z idk321 Ancient Books (IOI17_books) C++11
0 / 100
1 ms 204 KB
#include "books.h"
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

const int N = 1000000;

bool vis[N];
bool tree[4 * N];

void push(int node)
{
    if (tree[node])
    {
        tree[node * 2] = true;
        tree[node * 2 +1] = true;
    }
}

void ins(int from, int to, int a, int b, int node)
{
    if (from <= a && b <= to)
    {
        tree[node] = true;
        return;
    }

    push(node);
    int mid = (a + b) / 2;
    if (from <= mid) ins(from, to, a, mid, node * 2);
    if (to > mid) ins(from, to, mid + 1, b, node * 2 + 1);
}

bool getIn(int i, int a, int b, int node)
{
    if (a == b) return tree[node];

    push(node);
    int mid = (a + b) / 2;
    if (i <= mid) return getIn(i, a, mid, node * 2);
    return getIn(i, mid + 1, b, node * 2 + 1);
}

long long minimum_walk(std::vector<int> p, int s) {

    ll res = 0;

    int n = p.size();
    for (int i = 0; i < n; i++)
    {
        if (p[i] == i || vis[i]) continue;
        int cur = i;
        do
        {
            vis[cur] = true;
            res += abs(p[cur] - cur);
            ins(min(cur, p[cur]), max(cur, p[cur]), 0, n - 1, 1);
            cur = p[cur];
        } while (cur != i);
    }

    if (res == 0) return 0;

    int first = -1;
    int last = -1;
    for (int i = 0; i < n; i++)
    {
        if (getIn(i, 0, n- 1, 1))
        {
            first = i;
            break;
        }
    }
    for (int i = n - 1; i >= 0; i--)
    {
        if (getIn(i, 0, n - 1, 1))
        {
            last = i;
            break;
        }
    }


    for (int i = n - 1; i >= 0; i--)
    {
        if (!getIn(i, 0, n - 1, 1))
        {
            if (i <= last)
            {
                if (i < first) res++;
                else res += 2;
            }
        }
    }

    return res;
}

/*
4 0
3 1 2 0
*/
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 204 KB 3rd lines differ - on the 1st token, expected: '6', found: '5'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 204 KB 3rd lines differ - on the 1st token, expected: '6', found: '5'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 204 KB 3rd lines differ - on the 1st token, expected: '6', found: '5'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 204 KB 3rd lines differ - on the 1st token, expected: '3304', found: '2744'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 204 KB 3rd lines differ - on the 1st token, expected: '6', found: '5'
2 Halted 0 ms 0 KB -