답안 #805179

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
805179 2023-08-03T13:44:51 Z Ellinor 고대 책들 (IOI17_books) C++14
50 / 100
117 ms 20948 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];

            //

            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) 
                {
                    if (low1 == 0 && high1 == cycle.size() - 1) 
                    {
                        add = steps1;
                        break;
                    }

                    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
                {
                    if (low1 == 0 && high1 == cycle.size() - 1) 
                    {
                        add = steps1;
                        break;
                    }

                    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 // 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;

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

            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:117:33: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  117 |             if (low > 0 && high < cycle.size() - 1)
      |                            ~~~~~^~~~~~~~~~~~~~~~~~
books.cpp:131:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  131 |    if (low > 0 && high < cycle.size() - 1)
      |                   ~~~~~^~~~~~~~~~~~~~~~~~
books.cpp:143:44: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  143 |                     if (low1 == 0 && high1 == cycle.size() - 1)
      |                                      ~~~~~~^~~~~~~~~~~~~~~~~~~
books.cpp:165:44: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  165 |                     if (low1 == 0 && high1 == cycle.size() - 1)
      |                                      ~~~~~~^~~~~~~~~~~~~~~~~~~
books.cpp:178:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  178 |    else if (high < cycle.size() - 1)
      |             ~~~~~^~~~~~~~~~~~~~~~~~
books.cpp:217:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  217 |             if (low == 0 && high == cycle.size() - 1)
      |                             ~~~~~^~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 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 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 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 0 ms 296 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 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 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 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 0 ms 296 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 300 KB Output is correct
21 Correct 1 ms 300 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 304 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 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 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 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 0 ms 296 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 300 KB Output is correct
21 Correct 1 ms 300 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 304 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 117 ms 18764 KB Output is correct
31 Correct 103 ms 18772 KB Output is correct
32 Correct 75 ms 18672 KB Output is correct
33 Correct 86 ms 20948 KB Output is correct
34 Correct 85 ms 20944 KB Output is correct
35 Correct 86 ms 20928 KB Output is correct
36 Correct 88 ms 20824 KB Output is correct
37 Correct 96 ms 19044 KB Output is correct
38 Correct 83 ms 18780 KB Output is correct
39 Correct 82 ms 18792 KB Output is correct
40 Correct 90 ms 18780 KB Output is correct
41 Correct 92 ms 18652 KB Output is correct
42 Correct 110 ms 18648 KB Output is correct
43 Correct 104 ms 20936 KB Output is correct
# 결과 실행 시간 메모리 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 -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 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 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 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 0 ms 296 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 300 KB Output is correct
21 Correct 1 ms 300 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 304 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 117 ms 18764 KB Output is correct
31 Correct 103 ms 18772 KB Output is correct
32 Correct 75 ms 18672 KB Output is correct
33 Correct 86 ms 20948 KB Output is correct
34 Correct 85 ms 20944 KB Output is correct
35 Correct 86 ms 20928 KB Output is correct
36 Correct 88 ms 20824 KB Output is correct
37 Correct 96 ms 19044 KB Output is correct
38 Correct 83 ms 18780 KB Output is correct
39 Correct 82 ms 18792 KB Output is correct
40 Correct 90 ms 18780 KB Output is correct
41 Correct 92 ms 18652 KB Output is correct
42 Correct 110 ms 18648 KB Output is correct
43 Correct 104 ms 20936 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 -