Submission #915916

# Submission time Handle Problem Language Result Execution time Memory
915916 2024-01-24T21:54:13 Z nightfal Ancient Books (IOI17_books) C++14
70 / 100
887 ms 1048576 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;
}
tuple<int,int,long long> extend(vector<int> p, int l, int r, int lmost, int rmost, int move) {
    int n = p.size();
    while(l> lmost || r < rmost) {
        int idx;
        if (l > max(lmost,0)) idx == --l;
        else if (r < min(rmost,n-1)) idx == ++r;
        move += abs(p[idx]-idx);
        lmost = min(lmost,p[idx]); 
        rmost = max(rmost,p[idx]);
    }
    return make_tuple(l,r,move);
} 
long long cal2(vector<int> p, int l, int r, int lmost, int rmost, long long move, vector<vector<long long>>& dp) {
    if (dp[l][r]) return dp[l][r];
    int n = p.size();
    tie(l,r,move) = extend(p,l,r,lmost,rmost,move);
    dp[l][r] = move;
    if (l>0) dp[l][r] = cal2(p,l-1,r,lmost,rmost,move,dp) + 2;
    if (r<n-1) dp[l][r] = min(dp[l][r],cal2(p,l,r+1,lmost,rmost,move,dp)+2);
    return move;
}
long long cal(vector<int> p, int l, int r, int lmost, int rmost, vector<vector<long long>>& dp) {
    // printf("l=%d, r=%d, lmost=%d, rmost=%d\n",l,r,lmost,rmost);
    // for(auto & row: dp) {for(long long elem: row) printf("%lld ",elem); printf("\n");}
    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(l-1,p[l-1]);
        lmostN = min(lmost,lmostN);
        int 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]);
        int rmostN = max(r+1,p[r+1]);
        rmostN = max(rmost,rmostN);
        temp2 += cal(p,l,r+1,lmostN,rmostN,dp); 
    }
    dp[l][r] = min(temp1,temp2);
    // printf("dp[%d][%d]=%lld=min(%lld,%lld), \n",l,r,dp[l][r],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;
    // printf("s=%d\n",s);
    // printf("p: "); for(int elem: p) printf("%d ", elem); printf("\n");
    
    // for(auto &row: dp) {for(long long elem: row) printf("%lld ",elem); printf("\n");}
    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);
}

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

    int n = p.size(), left = s-1, right = s, leftmost = s-1, rightmost = s;
    long long total=0, sumL=0, sumR=0, moveL=1, moveR=0;

    while(0<=left || right<=n-1) {
        printf("total=%d, sumL=%d, sumR=%d, moveL=%d, moveR=%d\n",total,sumL,sumR,moveL,moveR);
        printf("left=%d, right=%d, leftmost=%d, rightmost=%d\n\n",left,right,leftmost,rightmost);

        if(right <= n-1 && (left < 0 || moveR < moveL || moveR == moveL && right <= rightmost)) {
            if(rightmost < right) moveR++;
            sumR += abs(p[right]-right);
            rightmost = max(rightmost,right);
            rightmost = max(rightmost,p[right]);    
            leftmost  = min(leftmost, p[right]);
            if (leftmost <= left) {moveL = 0; total+=sumR+moveR*2; moveR=0; sumR=0;}
            right++;
        } 
        else if(0 <= left && (right > n-1 || moveR > moveL || moveR == moveL && right > rightmost)) {
            if (left < leftmost) moveL++;
            sumL += abs(p[left]-left); 
            leftmost  = min(leftmost,left);
            leftmost  = min(leftmost, p[left]);
            rightmost = min(rightmost,p[left]);
            if (right <= rightmost) {moveR = 0; total+=sumL+moveL*2; moveL=0; sumL=0;}            
            left--;
        }
    }
    printf("total=%d, sumL=%d, sumR=%d, moveL=%d, moveR=%d\n",total,sumL,sumR,moveL,moveR);
    printf("left=%d, right=%d, leftmost=%d, rightmost=%d\n",left,right,leftmost,rightmost);
 
    if(sumL) total += sumL + moveL; // ??
    if(sumR) total += sumR + moveR; // ??
    return total;
}

long long minimum_walk(std::vector<int> p, int s) {
    if (isSubtask3(s)) return subtask3(p);
    // if (isSubtask4(p.size()))
    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 'std::tuple<int, int, long long int> extend(std::vector<int>, int, int, int, int, int)':
books.cpp:30:35: warning: value computed is not used [-Wunused-value]
   30 |         if (l > max(lmost,0)) idx == --l;
      |                               ~~~~^~~~~~
books.cpp:31:42: warning: value computed is not used [-Wunused-value]
   31 |         else if (r < min(rmost,n-1)) idx == ++r;
      |                                      ~~~~^~~~~~
books.cpp: In function 'long long int subtask4(std::vector<int>, int)':
books.cpp:76:15: warning: unused variable 'move' [-Wunused-variable]
   76 |     long long move = 0;
      |               ^~~~
books.cpp: In function 'long long int fulltask(std::vector<int>, int)':
books.cpp:99:24: warning: format '%d' expects argument of type 'int', but argument 2 has type 'long long int' [-Wformat=]
   99 |         printf("total=%d, sumL=%d, sumR=%d, moveL=%d, moveR=%d\n",total,sumL,sumR,moveL,moveR);
      |                       ~^                                          ~~~~~
      |                        |                                          |
      |                        int                                        long long int
      |                       %lld
books.cpp:99:33: warning: format '%d' expects argument of type 'int', but argument 3 has type 'long long int' [-Wformat=]
   99 |         printf("total=%d, sumL=%d, sumR=%d, moveL=%d, moveR=%d\n",total,sumL,sumR,moveL,moveR);
      |                                ~^                                       ~~~~
      |                                 |                                       |
      |                                 int                                     long long int
      |                                %lld
books.cpp:99:42: warning: format '%d' expects argument of type 'int', but argument 4 has type 'long long int' [-Wformat=]
   99 |         printf("total=%d, sumL=%d, sumR=%d, moveL=%d, moveR=%d\n",total,sumL,sumR,moveL,moveR);
      |                                         ~^                                   ~~~~
      |                                          |                                   |
      |                                          int                                 long long int
      |                                         %lld
books.cpp:99:52: warning: format '%d' expects argument of type 'int', but argument 5 has type 'long long int' [-Wformat=]
   99 |         printf("total=%d, sumL=%d, sumR=%d, moveL=%d, moveR=%d\n",total,sumL,sumR,moveL,moveR);
      |                                                   ~^                              ~~~~~
      |                                                    |                              |
      |                                                    int                            long long int
      |                                                   %lld
books.cpp:99:62: warning: format '%d' expects argument of type 'int', but argument 6 has type 'long long int' [-Wformat=]
   99 |         printf("total=%d, sumL=%d, sumR=%d, moveL=%d, moveR=%d\n",total,sumL,sumR,moveL,moveR);
      |                                                             ~^                          ~~~~~
      |                                                              |                          |
      |                                                              int                        long long int
      |                                                             %lld
books.cpp:102:73: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
  102 |         if(right <= n-1 && (left < 0 || moveR < moveL || moveR == moveL && right <= rightmost)) {
      |                                                          ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
books.cpp:111:78: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
  111 |         else if(0 <= left && (right > n-1 || moveR > moveL || moveR == moveL && right > rightmost)) {
      |                                                               ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~
books.cpp:121:20: warning: format '%d' expects argument of type 'int', but argument 2 has type 'long long int' [-Wformat=]
  121 |     printf("total=%d, sumL=%d, sumR=%d, moveL=%d, moveR=%d\n",total,sumL,sumR,moveL,moveR);
      |                   ~^                                          ~~~~~
      |                    |                                          |
      |                    int                                        long long int
      |                   %lld
books.cpp:121:29: warning: format '%d' expects argument of type 'int', but argument 3 has type 'long long int' [-Wformat=]
  121 |     printf("total=%d, sumL=%d, sumR=%d, moveL=%d, moveR=%d\n",total,sumL,sumR,moveL,moveR);
      |                            ~^                                       ~~~~
      |                             |                                       |
      |                             int                                     long long int
      |                            %lld
books.cpp:121:38: warning: format '%d' expects argument of type 'int', but argument 4 has type 'long long int' [-Wformat=]
  121 |     printf("total=%d, sumL=%d, sumR=%d, moveL=%d, moveR=%d\n",total,sumL,sumR,moveL,moveR);
      |                                     ~^                                   ~~~~
      |                                      |                                   |
      |                                      int                                 long long int
      |                                     %lld
books.cpp:121:48: warning: format '%d' expects argument of type 'int', but argument 5 has type 'long long int' [-Wformat=]
  121 |     printf("total=%d, sumL=%d, sumR=%d, moveL=%d, moveR=%d\n",total,sumL,sumR,moveL,moveR);
      |                                               ~^                              ~~~~~
      |                                                |                              |
      |                                                int                            long long int
      |                                               %lld
books.cpp:121:58: warning: format '%d' expects argument of type 'int', but argument 6 has type 'long long int' [-Wformat=]
  121 |     printf("total=%d, sumL=%d, sumR=%d, moveL=%d, moveR=%d\n",total,sumL,sumR,moveL,moveR);
      |                                                         ~^                          ~~~~~
      |                                                          |                          |
      |                                                          int                        long long int
      |                                                         %lld
books.cpp: In function 'std::tuple<int, int, long long int> extend(std::vector<int>, int, int, int, int, int)':
books.cpp:32:26: warning: 'idx' is used uninitialized in this function [-Wuninitialized]
   32 |         move += abs(p[idx]-idx);
      |                          ^
# Verdict Execution time Memory Grader output
1 Correct 1 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 344 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 1 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 348 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 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 344 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 344 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 1 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 348 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 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 344 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 432 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 344 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 1 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 348 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 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 344 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 432 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 84 ms 18640 KB Output is correct
31 Correct 78 ms 19024 KB Output is correct
32 Correct 73 ms 18756 KB Output is correct
33 Correct 77 ms 18792 KB Output is correct
34 Correct 77 ms 18772 KB Output is correct
35 Correct 77 ms 18772 KB Output is correct
36 Correct 75 ms 18788 KB Output is correct
37 Correct 74 ms 18780 KB Output is correct
38 Correct 75 ms 18652 KB Output is correct
39 Correct 73 ms 18752 KB Output is correct
40 Correct 74 ms 19028 KB Output is correct
41 Correct 76 ms 18768 KB Output is correct
42 Correct 74 ms 18664 KB Output is correct
43 Correct 76 ms 18780 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 849 ms 12416 KB Output is correct
2 Correct 132 ms 12520 KB Output is correct
3 Correct 867 ms 12384 KB Output is correct
4 Correct 791 ms 12172 KB Output is correct
5 Correct 879 ms 12392 KB Output is correct
6 Correct 821 ms 12404 KB Output is correct
7 Correct 863 ms 12416 KB Output is correct
8 Correct 853 ms 12412 KB Output is correct
9 Correct 698 ms 12416 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 840 ms 12400 KB Output is correct
12 Correct 838 ms 12400 KB Output is correct
13 Correct 777 ms 12380 KB Output is correct
14 Correct 887 ms 12400 KB Output is correct
15 Correct 838 ms 12416 KB Output is correct
16 Correct 844 ms 12388 KB Output is correct
17 Correct 834 ms 12412 KB Output is correct
18 Correct 843 ms 12392 KB Output is correct
19 Correct 870 ms 12404 KB Output is correct
20 Correct 539 ms 12416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 344 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 1 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 348 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 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 344 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 432 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 84 ms 18640 KB Output is correct
31 Correct 78 ms 19024 KB Output is correct
32 Correct 73 ms 18756 KB Output is correct
33 Correct 77 ms 18792 KB Output is correct
34 Correct 77 ms 18772 KB Output is correct
35 Correct 77 ms 18772 KB Output is correct
36 Correct 75 ms 18788 KB Output is correct
37 Correct 74 ms 18780 KB Output is correct
38 Correct 75 ms 18652 KB Output is correct
39 Correct 73 ms 18752 KB Output is correct
40 Correct 74 ms 19028 KB Output is correct
41 Correct 76 ms 18768 KB Output is correct
42 Correct 74 ms 18664 KB Output is correct
43 Correct 76 ms 18780 KB Output is correct
44 Correct 849 ms 12416 KB Output is correct
45 Correct 132 ms 12520 KB Output is correct
46 Correct 867 ms 12384 KB Output is correct
47 Correct 791 ms 12172 KB Output is correct
48 Correct 879 ms 12392 KB Output is correct
49 Correct 821 ms 12404 KB Output is correct
50 Correct 863 ms 12416 KB Output is correct
51 Correct 853 ms 12412 KB Output is correct
52 Correct 698 ms 12416 KB Output is correct
53 Correct 1 ms 348 KB Output is correct
54 Correct 840 ms 12400 KB Output is correct
55 Correct 838 ms 12400 KB Output is correct
56 Correct 777 ms 12380 KB Output is correct
57 Correct 887 ms 12400 KB Output is correct
58 Correct 838 ms 12416 KB Output is correct
59 Correct 844 ms 12388 KB Output is correct
60 Correct 834 ms 12412 KB Output is correct
61 Correct 843 ms 12392 KB Output is correct
62 Correct 870 ms 12404 KB Output is correct
63 Correct 539 ms 12416 KB Output is correct
64 Runtime error 407 ms 1048576 KB Execution killed with signal 9
65 Halted 0 ms 0 KB -