Submission #470056

# Submission time Handle Problem Language Result Execution time Memory
470056 2021-09-02T19:17:40 Z vincent97198 Group Photo (JOI21_ho_t3) C++14
100 / 100
918 ms 584 KB
#include <bits/stdc++.h>
#define int long long

using namespace std;

int calc[5005][5005];
int BIT[5005][2];

inline int lowbit(int x)
{
    return x&(-x);
}

inline void add(int pos,int val,int st)
{
    for(;pos<5005;pos+=lowbit(pos))
        BIT[pos][st]+=val;
}

inline int sum(int pos,int st)
{
    int ret=0;
    for(;pos>0;pos-=lowbit(pos))
        ret+=BIT[pos][st];
    return ret;
}

int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);   cout.tie(nullptr);

    int n;  cin >> n;
    vector<int> h(n+1),pos(n+1);

    for(int i=1;i<=n;++i){
        cin >> h[i];
        pos[h[i]]=i;
    }


    vector<int> dp(n+1,1e18);
    dp[0]=0;
    for(int i=1;i<=n;++i){
        int calc=0;
        for(int k=1;k<=i;++k) add(pos[k],1,0);
        for(int j=i-1;j>=0;--j){
            calc+=j+1-sum(pos[j+1],0);
            calc-=sum(pos[j+1],1);
            calc+=(i-j-1)-sum(pos[j+1],1);
            add(pos[j+1],-1,0);
            add(pos[j+1],1,1);
            dp[i]=min(dp[i],dp[j]+calc);
        }
        for(int k=0;k<5005;++k) BIT[k][1]=0;
    }
    cout << dp[n] << '\n';

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 2 ms 332 KB Output is correct
22 Correct 2 ms 332 KB Output is correct
23 Correct 2 ms 332 KB Output is correct
24 Correct 2 ms 332 KB Output is correct
25 Correct 2 ms 332 KB Output is correct
26 Correct 2 ms 332 KB Output is correct
27 Correct 2 ms 332 KB Output is correct
28 Correct 2 ms 404 KB Output is correct
29 Correct 2 ms 332 KB Output is correct
30 Correct 3 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 2 ms 332 KB Output is correct
22 Correct 2 ms 332 KB Output is correct
23 Correct 2 ms 332 KB Output is correct
24 Correct 2 ms 332 KB Output is correct
25 Correct 2 ms 332 KB Output is correct
26 Correct 2 ms 332 KB Output is correct
27 Correct 2 ms 332 KB Output is correct
28 Correct 2 ms 404 KB Output is correct
29 Correct 2 ms 332 KB Output is correct
30 Correct 3 ms 332 KB Output is correct
31 Correct 29 ms 424 KB Output is correct
32 Correct 30 ms 332 KB Output is correct
33 Correct 26 ms 332 KB Output is correct
34 Correct 25 ms 428 KB Output is correct
35 Correct 25 ms 424 KB Output is correct
36 Correct 25 ms 452 KB Output is correct
37 Correct 25 ms 332 KB Output is correct
38 Correct 25 ms 332 KB Output is correct
39 Correct 26 ms 332 KB Output is correct
40 Correct 25 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 2 ms 332 KB Output is correct
22 Correct 2 ms 332 KB Output is correct
23 Correct 2 ms 332 KB Output is correct
24 Correct 2 ms 332 KB Output is correct
25 Correct 2 ms 332 KB Output is correct
26 Correct 2 ms 332 KB Output is correct
27 Correct 2 ms 332 KB Output is correct
28 Correct 2 ms 404 KB Output is correct
29 Correct 2 ms 332 KB Output is correct
30 Correct 3 ms 332 KB Output is correct
31 Correct 29 ms 424 KB Output is correct
32 Correct 30 ms 332 KB Output is correct
33 Correct 26 ms 332 KB Output is correct
34 Correct 25 ms 428 KB Output is correct
35 Correct 25 ms 424 KB Output is correct
36 Correct 25 ms 452 KB Output is correct
37 Correct 25 ms 332 KB Output is correct
38 Correct 25 ms 332 KB Output is correct
39 Correct 26 ms 332 KB Output is correct
40 Correct 25 ms 332 KB Output is correct
41 Correct 899 ms 528 KB Output is correct
42 Correct 904 ms 580 KB Output is correct
43 Correct 905 ms 580 KB Output is correct
44 Correct 908 ms 580 KB Output is correct
45 Correct 898 ms 520 KB Output is correct
46 Correct 903 ms 452 KB Output is correct
47 Correct 904 ms 580 KB Output is correct
48 Correct 918 ms 460 KB Output is correct
49 Correct 905 ms 532 KB Output is correct
50 Correct 901 ms 516 KB Output is correct
51 Correct 903 ms 580 KB Output is correct
52 Correct 908 ms 520 KB Output is correct
53 Correct 904 ms 532 KB Output is correct
54 Correct 915 ms 520 KB Output is correct
55 Correct 904 ms 584 KB Output is correct