답안 #917946

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
917946 2024-01-29T07:10:06 Z nightfal 고대 책들 (IOI17_books) C++14
100 / 100
108 ms 26984 KB
#include <cstdio>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <tuple>
#include <climits>

using namespace std;

bool isSubtask3(int s) {return s==0;}
bool isSubtask4(int n) {return n<=1000;}

long long subtask3(vector<int> p, int s = 0, int e = -1 , int dir = 1) {
    int n = p.size(), last = s;
    if (e==-1) e = n-1;
    long long total = 0;

    for(int i=s; dir==1 && i<=e || dir==-1 && i>=e; i += dir) {
        if (i==p[i]) continue;
        if (dir*(i-last) > 0) total += abs(i-last)*2;
        total += abs(p[i]-i);
        last = dir==1? max(last,p[i]):min(last,p[i]);
    }
	return total;
}

long long cal(vector<int> p, int l, int r, int lmost, int rmost, vector<vector<long long>>& dp) {
    if (dp[l][r]!=LLONG_MAX) return dp[l][r];
    int n = p.size(); 
    long long temp1=LLONG_MAX, temp2=LLONG_MAX;
    if (l>0) {
        temp1 = abs(p[l-1]-(l-1)); if(l-1 < lmost) temp1 += 2;
        int lmostN = min(lmost,min(l-1,p[l-1])), rmostN = max(rmost,p[l-1]);
        temp1 += cal(p,l-1,r,lmostN,rmostN,dp); 
    }
    if (r<n-1) {
        temp2 = abs(p[r+1]-(r+1)); if(r+1 > rmost) temp2 += 2;
        int lmostN = min(lmost,p[r+1]), rmostN = max(rmost,max(r+1,p[r+1]));
        temp2 += cal(p,l,r+1,lmostN,rmostN,dp); 
    }
    dp[l][r] = min(temp1,temp2);
    return dp[l][r];
}

long long subtask4(vector<int> p, int s) {
    int n = p.size();
    // long long move = 0;
    
    int start; for(start = 0; start < s; start++) if(start!=p[start]) break;
    int end; for(end = n-1; end > s; end--) if(end!=p[end]) break;
    for(int i = 0; i <= end-start; i++) p[i] = p[i+start]-start;
    s -= start; n = end-start+1; p.resize(n); 
    vector<vector<long long>> dp(n,vector<long long>(n,LLONG_MAX));
    dp[0][n-1]=0;

    int l=s, r=s, lmost = min(s,p[s]), rmost = max(s,p[s]);
    return cal(p,l,r,lmost,rmost,dp)+abs(p[s]-s);
}

pair<int,int> extend(int l, int r, vector<int>& lmost, vector<int>& rmost) {
    while(l > min(lmost[l],lmost[r]) || r < max(rmost[l],rmost[r])) {
        l=min(l,min(lmost[l],lmost[r])); 
        r=max(r,max(rmost[r],rmost[l]));
    }
    return make_pair(l,r);
} 


long long fulltask(vector<int> p, int s) {

    int n = p.size();

    // curtail ignorable head and tail.
    int start; for(start = 0; start < s; start++) if(start!=p[start]) break;
    int end; for(end = n-1; end > s; end--) if(end!=p[end]) break;
    for(int i = 0; i <= end-start; i++) p[i] = p[i+start]-start;
    s -= start; n = end-start+1; p.resize(n); 
    // printf("s=%d, n=%d\n",s,n);

    int l=s, lStart=s, r=s, rStart=s, moveL=0, moveR=0;
    long long totalMoves=0, rMoves=0, lMoves=0, jumps=0;
    for(int i=0; i<n; i++) jumps += abs(p[i]-i);

    vector<int> lmost(n), rmost(n); 
    l = lmost[s] = min(s,p[s]), r = rmost[s] = max(s,p[s]);
    for(int i=s-1; i>=0; i--) {lmost[i] = min(lmost[i+1],p[i]); rmost[i] = max(rmost[i+1],p[i]);}
    for(int i=s+1; i<n ; i++) {lmost[i] = min(lmost[i-1],p[i]); rmost[i] = max(rmost[i-1],p[i]);}
    tie(l,r) = extend(l,r,lmost,rmost);
    while(0<l || r<n-1) {
        // printf("l=%d, r=%d\n",l,r);
        if(r<n-1 && (l<=0 || rMoves < lMoves)) {
            r++; rMoves++;
            int l2,r2;
            tie(l2,r2) = extend(l,r,lmost,rmost);
            if (l2<l) {totalMoves += rMoves; rMoves = lMoves = 0;}
            l = l2; r = r2; 
        }
        else {
            l--; lMoves++;
            int l2,r2;
            tie(l2,r2) = extend(l,r,lmost,rmost);
            if (r<r2) {totalMoves += lMoves; rMoves = lMoves = 0;}
            l = l2; r = r2;             
        }
    }
    totalMoves += rMoves + lMoves;
    // printf("totalMoves=%d, jumps=%d\n",totalMoves,jumps);
    return totalMoves*2+jumps;
}

long long minimum_walk(std::vector<int> p, int s) {
    // if (isSubtask3(s)) return subtask3(p);
    // if (isSubtask4(s)) return subtask4(p,s);
    return fulltask(p,s);
}

Compilation message

books.cpp: In function 'long long int subtask3(std::vector<int>, int, int, int)':
books.cpp:18:25: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   18 |     for(int i=s; dir==1 && i<=e || dir==-1 && i>=e; i += dir) {
      |                  ~~~~~~~^~~~~~~
books.cpp: In function 'long long int fulltask(std::vector<int>, int)':
books.cpp:80:14: warning: unused variable 'lStart' [-Wunused-variable]
   80 |     int l=s, lStart=s, r=s, rStart=s, moveL=0, moveR=0;
      |              ^~~~~~
books.cpp:80:29: warning: unused variable 'rStart' [-Wunused-variable]
   80 |     int l=s, lStart=s, r=s, rStart=s, moveL=0, moveR=0;
      |                             ^~~~~~
books.cpp:80:39: warning: unused variable 'moveL' [-Wunused-variable]
   80 |     int l=s, lStart=s, r=s, rStart=s, moveL=0, moveR=0;
      |                                       ^~~~~
books.cpp:80:48: warning: unused variable 'moveR' [-Wunused-variable]
   80 |     int l=s, lStart=s, r=s, rStart=s, moveL=0, moveR=0;
      |                                                ^~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 600 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 600 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 344 KB Output is correct
19 Correct 0 ms 344 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 1 ms 600 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 344 KB Output is correct
27 Correct 0 ms 344 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 600 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 344 KB Output is correct
19 Correct 0 ms 344 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 1 ms 600 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 344 KB Output is correct
27 Correct 0 ms 344 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 95 ms 19944 KB Output is correct
31 Correct 81 ms 19836 KB Output is correct
32 Correct 74 ms 12112 KB Output is correct
33 Correct 87 ms 19796 KB Output is correct
34 Correct 87 ms 19936 KB Output is correct
35 Correct 88 ms 19792 KB Output is correct
36 Correct 96 ms 19936 KB Output is correct
37 Correct 85 ms 19952 KB Output is correct
38 Correct 82 ms 19792 KB Output is correct
39 Correct 87 ms 19792 KB Output is correct
40 Correct 82 ms 19960 KB Output is correct
41 Correct 96 ms 19932 KB Output is correct
42 Correct 82 ms 19940 KB Output is correct
43 Correct 82 ms 19792 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 600 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 344 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 600 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 344 KB Output is correct
19 Correct 0 ms 344 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 1 ms 600 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 344 KB Output is correct
27 Correct 0 ms 344 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 95 ms 19944 KB Output is correct
31 Correct 81 ms 19836 KB Output is correct
32 Correct 74 ms 12112 KB Output is correct
33 Correct 87 ms 19796 KB Output is correct
34 Correct 87 ms 19936 KB Output is correct
35 Correct 88 ms 19792 KB Output is correct
36 Correct 96 ms 19936 KB Output is correct
37 Correct 85 ms 19952 KB Output is correct
38 Correct 82 ms 19792 KB Output is correct
39 Correct 87 ms 19792 KB Output is correct
40 Correct 82 ms 19960 KB Output is correct
41 Correct 96 ms 19932 KB Output is correct
42 Correct 82 ms 19940 KB Output is correct
43 Correct 82 ms 19792 KB Output is correct
44 Correct 0 ms 344 KB Output is correct
45 Correct 0 ms 348 KB Output is correct
46 Correct 0 ms 348 KB Output is correct
47 Correct 1 ms 348 KB Output is correct
48 Correct 0 ms 348 KB Output is correct
49 Correct 0 ms 348 KB Output is correct
50 Correct 0 ms 348 KB Output is correct
51 Correct 0 ms 348 KB Output is correct
52 Correct 0 ms 348 KB Output is correct
53 Correct 0 ms 600 KB Output is correct
54 Correct 0 ms 348 KB Output is correct
55 Correct 1 ms 348 KB Output is correct
56 Correct 0 ms 348 KB Output is correct
57 Correct 0 ms 348 KB Output is correct
58 Correct 1 ms 348 KB Output is correct
59 Correct 0 ms 344 KB Output is correct
60 Correct 1 ms 348 KB Output is correct
61 Correct 0 ms 348 KB Output is correct
62 Correct 0 ms 348 KB Output is correct
63 Correct 0 ms 348 KB Output is correct
64 Correct 84 ms 26592 KB Output is correct
65 Correct 86 ms 26728 KB Output is correct
66 Correct 85 ms 26592 KB Output is correct
67 Correct 83 ms 26708 KB Output is correct
68 Correct 9 ms 2908 KB Output is correct
69 Correct 8 ms 2904 KB Output is correct
70 Correct 8 ms 2908 KB Output is correct
71 Correct 8 ms 2908 KB Output is correct
72 Correct 8 ms 3160 KB Output is correct
73 Correct 9 ms 2908 KB Output is correct
74 Correct 88 ms 26984 KB Output is correct
75 Correct 108 ms 26596 KB Output is correct
76 Correct 82 ms 26604 KB Output is correct
77 Correct 83 ms 26708 KB Output is correct
78 Correct 90 ms 26728 KB Output is correct
79 Correct 90 ms 26704 KB Output is correct
80 Correct 75 ms 18752 KB Output is correct
81 Correct 86 ms 26712 KB Output is correct
82 Correct 84 ms 26704 KB Output is correct
83 Correct 94 ms 26708 KB Output is correct
84 Correct 85 ms 26600 KB Output is correct
85 Correct 83 ms 26704 KB Output is correct
86 Correct 83 ms 26704 KB Output is correct
87 Correct 87 ms 26680 KB Output is correct
88 Correct 101 ms 26728 KB Output is correct
89 Correct 86 ms 26708 KB Output is correct
90 Correct 83 ms 26588 KB Output is correct
91 Correct 82 ms 26960 KB Output is correct
92 Correct 84 ms 26468 KB Output is correct