답안 #767066

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
767066 2023-06-26T10:53:57 Z danikoynov 고대 책들 (IOI17_books) C++14
70 / 100
151 ms 46284 KB
#include "books.h"
#include<bits/stdc++.h>

using namespace std;
typedef long long ll;


const int maxn = 1e6 + 10;
const ll inf = 1e18;
ll used[maxn], l[maxn], r[maxn], n, dp[1030][1030];
int bef[maxn], aft[maxn];
vector < int > st[maxn];
ll distance(int l, int r, int i, int j)
{
    if (i <= l && j >= r)
    {
        int mx = 1e9;
        for (int idx : st[used[i]])
        {
            if (idx >= l && idx <= r)
            {
                mx = 0;
                break;
            }
            mx = min(mx, abs(idx - l));
            mx = min(mx, abs(idx - r));
        }
        return mx * 2;
    }
    if (i > r)
        return (i - r) * 2;
    if (j < l)
        return (l - j) * 2;
    return 0;
}
long long minimum_walk(vector<int> p, int s)
{
    n = p.size();
    if (n > 1000)
    {
        int cnt = 0;
    ll mx = 0, ans = 0;
    int to = 0;
    for (int i = 0; i < n; i ++)
    {
        if (p[i] == i)
            continue;
            ans = ans + (ll)abs(i - p[i]);
            ///cout << ans << endl;
        if (!used[i])
        {
            if (to < i)
                mx = mx + i - to;
            int v = p[i];
            while(v != i)
            {

                to = max(to, v);
                used[v] = 1;
                v = p[v];
            }
            to = max(to, v);
            used[v] = 1;
        }
    }
	return ans + 2 * mx;
    }
    int cnt = 0;
    ll ans = 0;
    for (int i = 0; i < n; i ++)
    {
        ans = ans + abs(p[i] - i);
        if (p[i] == i)
            continue;
        if (used[i])
            continue;

        cnt ++;
        used[i] = cnt;
        int v = p[i];
        l[cnt] = i;
        r[cnt] = i;
        st[cnt].push_back(i);
        while(v != i)
        {
            st[cnt].push_back(v);
            used[v] = cnt;
            r[cnt] = max(r[cnt], (ll)v);
            v = p[v];
        }
    }

    bef[0] = used[0];
    for (int i = 1; i < n; i ++)
    {
        bef[i] = bef[i - 1];
        if (used[i] != 0)
            bef[i] = used[i];
    }

    aft[n - 1] = used[n - 1];
    for (int i = n - 2; i >= 0; i --)
    {
        aft[i] = aft[i + 1];
        if (used[i] != 0)
            aft[i] = used[i];
    }

    for (int i = 0; i < n; i ++)
        for (int j = 0; j < n; j ++)
            dp[i][j] = inf;

    dp[s][s] = 0;
    ///if (used[s] != 0)
       /// dp[l[used[s]]][r[used[s]]] = 0;

    /**cout << "-------------" << endl;
    for (int i = 1; i <= cnt; i ++)
        cout << l[i] << " " << r[i] << endl;
    cout << "-------------" << endl;*/
    for (int len = 1; len <= n; len ++)
    {
        for (int i = 0; i < n - len + 1; i ++)
        {
            int j = i + len - 1;
            if (dp[i][j] == inf)
                continue;
                ///cout << i << " " << j << " " << dp[i][j] << endl;
            vector < int > idx;
            /**if (i != 0 && bef[i - 1] != 0)
                idx.push_back(bef[i - 1]);
            if (j != n - 1 && aft[j + 1] != 0)
                idx.push_back(aft[j + 1]);*/
                for (int p = 1; p <= cnt; p ++)
                    idx.push_back(p);
            for (int cur : idx)
            {
                int newl = min((ll)i, l[cur]);
                int newr = max((ll)j, r[cur]);
                if (dp[newl][newr] > dp[i][j] + distance(i, j, l[cur], r[cur]))
                {
                    dp[newl][newr] = dp[i][j] + distance(i, j, l[cur], r[cur]);
                    ///cout << "to " << newl << " " << newr << endl;
                }
                ///dp[newl][newr] = min(dp[newl][newr], dp[i][j] + distance(i, j, l[cur], r[cur]));
            }

        }

    }

    int lf = 0;
    int rf = n - 1;
    while(lf < s && used[lf] == 0)
        lf ++;
    while(rf > s && used[rf] == 0)
        rf --;
    ///cout << dp[lf][rf] << endl;
    ///cout << lf << " " << rf << endl;
    return ans + dp[lf][rf];


}

Compilation message

books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:46:9: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
   46 |         if (p[i] == i)
      |         ^~
books.cpp:48:13: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
   48 |             ans = ans + (ll)abs(i - p[i]);
      |             ^~~
books.cpp:41:13: warning: unused variable 'cnt' [-Wunused-variable]
   41 |         int cnt = 0;
      |             ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 23764 KB Output is correct
2 Correct 12 ms 23764 KB Output is correct
3 Correct 12 ms 23784 KB Output is correct
4 Correct 14 ms 23892 KB Output is correct
5 Correct 12 ms 23832 KB Output is correct
6 Correct 12 ms 23824 KB Output is correct
7 Correct 12 ms 23764 KB Output is correct
8 Correct 12 ms 23764 KB Output is correct
9 Correct 13 ms 23820 KB Output is correct
10 Correct 12 ms 23844 KB Output is correct
11 Correct 12 ms 23704 KB Output is correct
12 Correct 12 ms 23764 KB Output is correct
13 Correct 11 ms 23796 KB Output is correct
14 Correct 12 ms 23764 KB Output is correct
15 Correct 12 ms 23744 KB Output is correct
16 Correct 12 ms 23764 KB Output is correct
17 Correct 12 ms 23764 KB Output is correct
18 Correct 13 ms 23764 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 23764 KB Output is correct
2 Correct 12 ms 23764 KB Output is correct
3 Correct 12 ms 23784 KB Output is correct
4 Correct 14 ms 23892 KB Output is correct
5 Correct 12 ms 23832 KB Output is correct
6 Correct 12 ms 23824 KB Output is correct
7 Correct 12 ms 23764 KB Output is correct
8 Correct 12 ms 23764 KB Output is correct
9 Correct 13 ms 23820 KB Output is correct
10 Correct 12 ms 23844 KB Output is correct
11 Correct 12 ms 23704 KB Output is correct
12 Correct 12 ms 23764 KB Output is correct
13 Correct 11 ms 23796 KB Output is correct
14 Correct 12 ms 23764 KB Output is correct
15 Correct 12 ms 23744 KB Output is correct
16 Correct 12 ms 23764 KB Output is correct
17 Correct 12 ms 23764 KB Output is correct
18 Correct 13 ms 23764 KB Output is correct
19 Correct 16 ms 31828 KB Output is correct
20 Correct 19 ms 31904 KB Output is correct
21 Correct 20 ms 31868 KB Output is correct
22 Correct 17 ms 31828 KB Output is correct
23 Correct 17 ms 31844 KB Output is correct
24 Correct 19 ms 31916 KB Output is correct
25 Correct 17 ms 31828 KB Output is correct
26 Correct 16 ms 31900 KB Output is correct
27 Correct 16 ms 31872 KB Output is correct
28 Correct 16 ms 31828 KB Output is correct
29 Correct 17 ms 31912 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 23764 KB Output is correct
2 Correct 12 ms 23764 KB Output is correct
3 Correct 12 ms 23784 KB Output is correct
4 Correct 14 ms 23892 KB Output is correct
5 Correct 12 ms 23832 KB Output is correct
6 Correct 12 ms 23824 KB Output is correct
7 Correct 12 ms 23764 KB Output is correct
8 Correct 12 ms 23764 KB Output is correct
9 Correct 13 ms 23820 KB Output is correct
10 Correct 12 ms 23844 KB Output is correct
11 Correct 12 ms 23704 KB Output is correct
12 Correct 12 ms 23764 KB Output is correct
13 Correct 11 ms 23796 KB Output is correct
14 Correct 12 ms 23764 KB Output is correct
15 Correct 12 ms 23744 KB Output is correct
16 Correct 12 ms 23764 KB Output is correct
17 Correct 12 ms 23764 KB Output is correct
18 Correct 13 ms 23764 KB Output is correct
19 Correct 16 ms 31828 KB Output is correct
20 Correct 19 ms 31904 KB Output is correct
21 Correct 20 ms 31868 KB Output is correct
22 Correct 17 ms 31828 KB Output is correct
23 Correct 17 ms 31844 KB Output is correct
24 Correct 19 ms 31916 KB Output is correct
25 Correct 17 ms 31828 KB Output is correct
26 Correct 16 ms 31900 KB Output is correct
27 Correct 16 ms 31872 KB Output is correct
28 Correct 16 ms 31828 KB Output is correct
29 Correct 17 ms 31912 KB Output is correct
30 Correct 151 ms 39452 KB Output is correct
31 Correct 112 ms 46072 KB Output is correct
32 Correct 86 ms 38280 KB Output is correct
33 Correct 93 ms 46124 KB Output is correct
34 Correct 99 ms 46180 KB Output is correct
35 Correct 96 ms 46116 KB Output is correct
36 Correct 106 ms 46108 KB Output is correct
37 Correct 91 ms 46108 KB Output is correct
38 Correct 91 ms 46104 KB Output is correct
39 Correct 93 ms 46112 KB Output is correct
40 Correct 102 ms 46112 KB Output is correct
41 Correct 101 ms 46112 KB Output is correct
42 Correct 99 ms 46116 KB Output is correct
43 Correct 97 ms 46284 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 34 ms 31920 KB Output is correct
2 Correct 16 ms 31856 KB Output is correct
3 Correct 27 ms 31884 KB Output is correct
4 Correct 30 ms 31828 KB Output is correct
5 Correct 30 ms 31912 KB Output is correct
6 Correct 37 ms 31796 KB Output is correct
7 Correct 41 ms 31920 KB Output is correct
8 Correct 109 ms 31904 KB Output is correct
9 Correct 96 ms 31912 KB Output is correct
10 Correct 16 ms 31828 KB Output is correct
11 Correct 50 ms 31828 KB Output is correct
12 Correct 55 ms 31920 KB Output is correct
13 Correct 39 ms 31828 KB Output is correct
14 Correct 31 ms 31828 KB Output is correct
15 Correct 19 ms 31924 KB Output is correct
16 Correct 17 ms 31828 KB Output is correct
17 Correct 16 ms 31828 KB Output is correct
18 Correct 63 ms 31896 KB Output is correct
19 Correct 55 ms 31904 KB Output is correct
20 Correct 16 ms 31828 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 23764 KB Output is correct
2 Correct 12 ms 23764 KB Output is correct
3 Correct 12 ms 23784 KB Output is correct
4 Correct 14 ms 23892 KB Output is correct
5 Correct 12 ms 23832 KB Output is correct
6 Correct 12 ms 23824 KB Output is correct
7 Correct 12 ms 23764 KB Output is correct
8 Correct 12 ms 23764 KB Output is correct
9 Correct 13 ms 23820 KB Output is correct
10 Correct 12 ms 23844 KB Output is correct
11 Correct 12 ms 23704 KB Output is correct
12 Correct 12 ms 23764 KB Output is correct
13 Correct 11 ms 23796 KB Output is correct
14 Correct 12 ms 23764 KB Output is correct
15 Correct 12 ms 23744 KB Output is correct
16 Correct 12 ms 23764 KB Output is correct
17 Correct 12 ms 23764 KB Output is correct
18 Correct 13 ms 23764 KB Output is correct
19 Correct 16 ms 31828 KB Output is correct
20 Correct 19 ms 31904 KB Output is correct
21 Correct 20 ms 31868 KB Output is correct
22 Correct 17 ms 31828 KB Output is correct
23 Correct 17 ms 31844 KB Output is correct
24 Correct 19 ms 31916 KB Output is correct
25 Correct 17 ms 31828 KB Output is correct
26 Correct 16 ms 31900 KB Output is correct
27 Correct 16 ms 31872 KB Output is correct
28 Correct 16 ms 31828 KB Output is correct
29 Correct 17 ms 31912 KB Output is correct
30 Correct 151 ms 39452 KB Output is correct
31 Correct 112 ms 46072 KB Output is correct
32 Correct 86 ms 38280 KB Output is correct
33 Correct 93 ms 46124 KB Output is correct
34 Correct 99 ms 46180 KB Output is correct
35 Correct 96 ms 46116 KB Output is correct
36 Correct 106 ms 46108 KB Output is correct
37 Correct 91 ms 46108 KB Output is correct
38 Correct 91 ms 46104 KB Output is correct
39 Correct 93 ms 46112 KB Output is correct
40 Correct 102 ms 46112 KB Output is correct
41 Correct 101 ms 46112 KB Output is correct
42 Correct 99 ms 46116 KB Output is correct
43 Correct 97 ms 46284 KB Output is correct
44 Correct 34 ms 31920 KB Output is correct
45 Correct 16 ms 31856 KB Output is correct
46 Correct 27 ms 31884 KB Output is correct
47 Correct 30 ms 31828 KB Output is correct
48 Correct 30 ms 31912 KB Output is correct
49 Correct 37 ms 31796 KB Output is correct
50 Correct 41 ms 31920 KB Output is correct
51 Correct 109 ms 31904 KB Output is correct
52 Correct 96 ms 31912 KB Output is correct
53 Correct 16 ms 31828 KB Output is correct
54 Correct 50 ms 31828 KB Output is correct
55 Correct 55 ms 31920 KB Output is correct
56 Correct 39 ms 31828 KB Output is correct
57 Correct 31 ms 31828 KB Output is correct
58 Correct 19 ms 31924 KB Output is correct
59 Correct 17 ms 31828 KB Output is correct
60 Correct 16 ms 31828 KB Output is correct
61 Correct 63 ms 31896 KB Output is correct
62 Correct 55 ms 31904 KB Output is correct
63 Correct 16 ms 31828 KB Output is correct
64 Incorrect 91 ms 46120 KB 3rd lines differ - on the 1st token, expected: '9339550', found: '9338938'
65 Halted 0 ms 0 KB -