Submission #936762

# Submission time Handle Problem Language Result Execution time Memory
936762 2024-03-02T16:56:14 Z anton Group Photo (JOI21_ho_t3) C++17
44 / 100
5000 ms 708 KB
#include<bits/stdc++.h>

using namespace std;
#define int long long
#define pii pair<int, int>
vector<int> v;
vector<int> link;

void swap_pos(int lh, int rh){
    int vlh = v[lh];
    int vrh = v[rh];
    link[vlh] = rh;
    link[vrh] = lh;
    swap(v[lh], v[rh]);
}

int move(int id, int pos){
    //cout<<"move "<<id<<" "<<pos<<endl;
    int cpos= link[id];
    int res = 0;
    if(cpos!=pos){
        for(; cpos<pos; cpos++){
            swap_pos(cpos,cpos+1);
            res++;
        }
        for(; pos<cpos; cpos--){
            swap_pos(cpos, cpos-1);
            res++;
        }
    }
    return res;
}

map<pii, int> store;
int dp(int val, int end){
    if(store.find({val, end})!= store.end()){
        return store[{val, end}];
    }
    else{
        int res = 0;
    
        if(val==0|| end == -1){
            res= 0;
        }
        else{
            res= 1e18;
            for(int i = 0; i<val; i++){   
                //cout<<"i is "<<i<<endl;   
                int c= 0;
                vector<int> begins;
                /*for(auto e: v){
                    cout<<e<<" ";
                }
                cout<<endl;*/
                for(int j = val-i; j<=val; j++){
                    begins.push_back(link[j]);
                    c+=move(j, end+val-i-j);
                }
                //cout<<val<<"cur c is "<<c<<endl;
                res=min(dp(val-i-1, end-i-1)+c, res);
                for(int j = val; j>=val-i; j--){
                    move(j, begins.back());
                    begins.pop_back();
                }
                /*for(auto e: v){
                    cout<<e<<" ";
                }
                cout<<endl;*/
            }
        }
        
        //cout<<val<<" "<<end<<" "<<res<<endl;
        store[{val, end}] = res;
        return res;
    }
    
}

signed main(){
    int n;
    cin>>n;
    v.resize(n);
    link.resize(n+1);
    int res= 0;
    for(int i = 0; i<n; i++){
        cin>>v[i];
        link[v[i]] = i;
    }

    cout<<dp(n,n-1)<<endl;
    
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:84:9: warning: unused variable 'res' [-Wunused-variable]
   84 |     int res= 0;
      |         ^~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 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 348 KB Output is correct
6 Correct 1 ms 420 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 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 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 348 KB Output is correct
6 Correct 1 ms 420 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 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 516 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 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 344 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 348 KB Output is correct
6 Correct 1 ms 420 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 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 516 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 560 ms 472 KB Output is correct
22 Correct 565 ms 452 KB Output is correct
23 Correct 564 ms 456 KB Output is correct
24 Correct 603 ms 456 KB Output is correct
25 Correct 570 ms 580 KB Output is correct
26 Correct 598 ms 452 KB Output is correct
27 Correct 583 ms 456 KB Output is correct
28 Correct 558 ms 708 KB Output is correct
29 Correct 605 ms 592 KB Output is correct
30 Correct 585 ms 548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 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 348 KB Output is correct
6 Correct 1 ms 420 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 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 516 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 560 ms 472 KB Output is correct
22 Correct 565 ms 452 KB Output is correct
23 Correct 564 ms 456 KB Output is correct
24 Correct 603 ms 456 KB Output is correct
25 Correct 570 ms 580 KB Output is correct
26 Correct 598 ms 452 KB Output is correct
27 Correct 583 ms 456 KB Output is correct
28 Correct 558 ms 708 KB Output is correct
29 Correct 605 ms 592 KB Output is correct
30 Correct 585 ms 548 KB Output is correct
31 Execution timed out 5002 ms 572 KB Time limit exceeded
32 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 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 348 KB Output is correct
6 Correct 1 ms 420 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 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 516 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 560 ms 472 KB Output is correct
22 Correct 565 ms 452 KB Output is correct
23 Correct 564 ms 456 KB Output is correct
24 Correct 603 ms 456 KB Output is correct
25 Correct 570 ms 580 KB Output is correct
26 Correct 598 ms 452 KB Output is correct
27 Correct 583 ms 456 KB Output is correct
28 Correct 558 ms 708 KB Output is correct
29 Correct 605 ms 592 KB Output is correct
30 Correct 585 ms 548 KB Output is correct
31 Execution timed out 5002 ms 572 KB Time limit exceeded
32 Halted 0 ms 0 KB -