Submission #805197

# Submission time Handle Problem Language Result Execution time Memory
805197 2023-08-03T13:55:45 Z Ellinor Ancient Books (IOI17_books) C++14
50 / 100
102 ms 14188 KB
#include <bits/stdc++.h>
#include <unordered_map>
using namespace std;

#define rep(i,a,b) for (int i = (a); i < (b); i++)
#define pb push_back
typedef long long ll;
typedef pair<int, int> pii;


#include "books.h"

long long minimum_walk(std::vector<int> p, int s) 
{
	int cycles = 0;
	vector<int> cycle(p.size(), -1);
	vector<pii> lohi;

	ll base = 0;

	rep(i,0,p.size())
	{
		if (p[i] == i) cycle[i] = -1;
		else if (cycle[i] == -1)
		{
			cycle[i] = cycles;

			base += abs(i - p[i]);

			int at = p[i], low = i, high = i;
			while (at != i)
			{
				if (at < low) low = at;
				if (at > high) high = at;

				base += abs(at - p[at]);

				cycle[at] = cycles;
				at = p[at];
			}

			lohi.pb(make_pair(low, high));

			cycles++;
		}
	}

	ll add = 0;

	if (s == 0)
	{
		int low = 0, high = 0; // inc
		//int ind = 0;
		rep(i,0,cycle.size())
		{
			if (cycle[i] == -1) continue;

			if (i > high) add += (i - high) * 2;
			high = max(high, lohi[cycle[i]].second);
		}
	}

	else
	{
		vector<int> closest(cycle.size(), -1);

		if (cycle[0] == -1) closest[0] = -1;
		else closest[0] = 0;

		rep(i,1,s)
		{
			if (cycle[i] == -1) closest[i] = closest[i - 1];
			else closest[i] = i;
		}

		if (cycle[cycle.size() - 1] == -1) closest[cycle.size() - 1] = -1;
		else closest[cycle.size() - 1] = cycle.size() - 1;

		for (int i = cycle.size() - 2; i > s; i--)
		{
			if (cycle[i] == -1) closest[i] = closest[i + 1];
			else closest[i] = i;
		}

		// edges

		int low = s, high = s;
		if (cycle[s] != -1)
		{
			low = lohi[cycle[s]].first;
			high = lohi[cycle[s]].second;
		}

        //

        map<pii, ll> dp; // ??

        priority_queue<array<ll, 3>> pq;
        pq.push({-0, low, high});

        dp[make_pair(low, high)] = -1;

        while (!pq.empty())
        {
            auto x = pq.top();
            pq.pop();

            ll steps = x[0];
            steps = -steps;
            low = x[1];
            high = x[2];

            if (low == 0 && high == cycle.size() - 1) 
            {
                add = steps;
                break;
            }

            //

            int go = -1;

            if (low > 0 && high < cycle.size() - 1)
            {
                if (closest[low - 1] == -1) 
				{
					low = 0;
					// continue;
				}
				if (closest[high + 1] == -1)
				{
					high = cycle.size() - 1;
					// continue;
				}
            }

			if (low > 0 && high < cycle.size() - 1)
			{
                int low1 = low, high1 = high, steps1 = steps;

                go = closest[low - 1];
                steps1 += (low - closest[low - 1]) * 2;

                low1 = min(low, lohi[cycle[go]].first);
			    high1 = max(high, lohi[cycle[go]].second);

                if (dp[make_pair(low1, high1)] == 0) 
                {
                    dp[make_pair(low1, high1)] = steps1;
                    pq.push({-steps1, low1, high1});
                }
                
                //

                low1 = low, high1 = high, steps1 = steps;

                go = closest[high + 1];
                steps1 += (closest[high + 1] - high) * 2;

                low1 = min(low, lohi[cycle[go]].first);
			    high1 = max(high, lohi[cycle[go]].second);

                if (dp[make_pair(low1, high1)] == 0 || steps1 < dp[make_pair(low1, high1)]) // dp[make_pair(low1, high1)] == 0
                {
                    dp[make_pair(low1, high1)] = steps1;
                    pq.push({-steps1, low1, high1});
                }
                
                continue;
			}

			else if (high < cycle.size() - 1)
			{
				if (closest[high + 1] == -1)
				{
                    add = steps;
                    break;
				}

                go = closest[high + 1];
                steps += (closest[high + 1] - high) * 2;
			}

			else if (low > 0)
			{
				if (closest[low - 1] == -1) 
				{
					add = steps;
                    break;
				}

				go = closest[low - 1];
				steps += (low - closest[low - 1]) * 2;
			}

            if (go == -1)
            {
                add = steps;
                break;
            }


			low = min(low, lohi[cycle[go]].first);
			high = max(high, lohi[cycle[go]].second);

            //

            if (dp[make_pair(low, high)] != 0) continue; // ??
            dp[make_pair(low, high)] = steps;

            pq.push({-steps, low, high});
        }

        //
	}

	ll ans = base + add;

	return ans;
}

Compilation message

books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:5:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    5 | #define rep(i,a,b) for (int i = (a); i < (b); i++)
      |                                        ^
books.cpp:21:2: note: in expansion of macro 'rep'
   21 |  rep(i,0,p.size())
      |  ^~~
books.cpp:5:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    5 | #define rep(i,a,b) for (int i = (a); i < (b); i++)
      |                                        ^
books.cpp:54:3: note: in expansion of macro 'rep'
   54 |   rep(i,0,cycle.size())
      |   ^~~
books.cpp:52:7: warning: unused variable 'low' [-Wunused-variable]
   52 |   int low = 0, high = 0; // inc
      |       ^~~
books.cpp:113:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  113 |             if (low == 0 && high == cycle.size() - 1)
      |                             ~~~~~^~~~~~~~~~~~~~~~~~~
books.cpp:123:33: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  123 |             if (low > 0 && high < cycle.size() - 1)
      |                            ~~~~~^~~~~~~~~~~~~~~~~~
books.cpp:137:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  137 |    if (low > 0 && high < cycle.size() - 1)
      |                   ~~~~~^~~~~~~~~~~~~~~~~~
books.cpp:172:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  172 |    else if (high < cycle.size() - 1)
      |             ~~~~~^~~~~~~~~~~~~~~~~~
# 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 1 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 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 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 1 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 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 0 ms 212 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 0 ms 212 KB Output is correct
28 Correct 0 ms 212 KB Output is correct
29 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 1 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 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 0 ms 212 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 0 ms 212 KB Output is correct
28 Correct 0 ms 212 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 102 ms 12016 KB Output is correct
31 Correct 101 ms 12024 KB Output is correct
32 Correct 74 ms 12024 KB Output is correct
33 Correct 82 ms 14172 KB Output is correct
34 Correct 85 ms 14188 KB Output is correct
35 Correct 84 ms 14176 KB Output is correct
36 Correct 81 ms 14188 KB Output is correct
37 Correct 79 ms 12396 KB Output is correct
38 Correct 83 ms 12108 KB Output is correct
39 Correct 79 ms 12024 KB Output is correct
40 Correct 82 ms 12016 KB Output is correct
41 Correct 90 ms 12008 KB Output is correct
42 Correct 93 ms 12016 KB Output is correct
43 Correct 84 ms 14188 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 468 KB 3rd lines differ - on the 1st token, expected: '3304', found: '3314'
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 1 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 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 0 ms 212 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 0 ms 212 KB Output is correct
28 Correct 0 ms 212 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 102 ms 12016 KB Output is correct
31 Correct 101 ms 12024 KB Output is correct
32 Correct 74 ms 12024 KB Output is correct
33 Correct 82 ms 14172 KB Output is correct
34 Correct 85 ms 14188 KB Output is correct
35 Correct 84 ms 14176 KB Output is correct
36 Correct 81 ms 14188 KB Output is correct
37 Correct 79 ms 12396 KB Output is correct
38 Correct 83 ms 12108 KB Output is correct
39 Correct 79 ms 12024 KB Output is correct
40 Correct 82 ms 12016 KB Output is correct
41 Correct 90 ms 12008 KB Output is correct
42 Correct 93 ms 12016 KB Output is correct
43 Correct 84 ms 14188 KB Output is correct
44 Incorrect 1 ms 468 KB 3rd lines differ - on the 1st token, expected: '3304', found: '3314'
45 Halted 0 ms 0 KB -