Submission #388176

# Submission time Handle Problem Language Result Execution time Memory
388176 2021-04-10T11:13:58 Z BartolM Group Photo (JOI21_ho_t3) C++17
100 / 100
860 ms 524 KB
#define DEBUG 1
#include <bits/stdc++.h>

using namespace std;

#define X first
#define Y second
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <int, pii> pip;
typedef pair <pii, int> ppi;
typedef pair <ll, ll> pll;

const int INF=0x3f3f3f3f;
const int N=5005;

struct Fenwick{
    int F[N];
    Fenwick() {
        memset(F, 0, sizeof F);
    }
    int query(int x) {
        int ret=0;
        for (x++; x>0; x-=x&-x) ret+=F[x];
        return ret;
    }
    void update(int x, int val) {
        for (x++; x<N; x+=x&-x) F[x]+=val;
    }
    void reset() {
        memset(F, 0, sizeof F);
    }

};

int n;
int p[N], pos[N];
int dp[N];
Fenwick F1, F2;

void solve() {
    dp[n+1]=0;
    for (int i=n; i>0; --i) {
        dp[i]=INF;
        F1.reset(); F2.reset();
        for (int j=i; j<=n; ++j) F1.update(pos[j], 1);
        int dod=0;
        for (int j=i; j<=n; ++j) {
            dod+=F1.query(pos[j]-1)-(F2.query(n)-F2.query(pos[j]));
            dp[i]=min(dp[i], dp[j+1]+dod);
            F2.update(pos[j], 1);
        }
    }
    printf("%d\n", dp[1]);
}

void load() {
    scanf("%d", &n);
    for (int i=1; i<=n; ++i) {
        scanf("%d", &p[i]);
        pos[p[i]]=i;
    }
}

int main() {
    load();
    solve();
    return 0;
}

Compilation message

Main.cpp: In function 'void load()':
Main.cpp:60:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   60 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~
Main.cpp:62:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   62 |         scanf("%d", &p[i]);
      |         ~~~~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 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 340 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 1 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 340 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 344 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 336 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 1 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 340 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 344 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 336 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 460 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 332 KB Output is correct
29 Correct 2 ms 332 KB Output is correct
30 Correct 2 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 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 340 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 344 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 336 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 460 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 332 KB Output is correct
29 Correct 2 ms 332 KB Output is correct
30 Correct 2 ms 344 KB Output is correct
31 Correct 26 ms 344 KB Output is correct
32 Correct 20 ms 356 KB Output is correct
33 Correct 19 ms 356 KB Output is correct
34 Correct 20 ms 356 KB Output is correct
35 Correct 20 ms 332 KB Output is correct
36 Correct 20 ms 352 KB Output is correct
37 Correct 20 ms 332 KB Output is correct
38 Correct 20 ms 356 KB Output is correct
39 Correct 21 ms 332 KB Output is correct
40 Correct 20 ms 360 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 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 340 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 344 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 336 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 460 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 332 KB Output is correct
29 Correct 2 ms 332 KB Output is correct
30 Correct 2 ms 344 KB Output is correct
31 Correct 26 ms 344 KB Output is correct
32 Correct 20 ms 356 KB Output is correct
33 Correct 19 ms 356 KB Output is correct
34 Correct 20 ms 356 KB Output is correct
35 Correct 20 ms 332 KB Output is correct
36 Correct 20 ms 352 KB Output is correct
37 Correct 20 ms 332 KB Output is correct
38 Correct 20 ms 356 KB Output is correct
39 Correct 21 ms 332 KB Output is correct
40 Correct 20 ms 360 KB Output is correct
41 Correct 796 ms 396 KB Output is correct
42 Correct 860 ms 452 KB Output is correct
43 Correct 777 ms 404 KB Output is correct
44 Correct 800 ms 392 KB Output is correct
45 Correct 787 ms 400 KB Output is correct
46 Correct 796 ms 388 KB Output is correct
47 Correct 798 ms 452 KB Output is correct
48 Correct 797 ms 396 KB Output is correct
49 Correct 804 ms 396 KB Output is correct
50 Correct 798 ms 524 KB Output is correct
51 Correct 796 ms 456 KB Output is correct
52 Correct 799 ms 456 KB Output is correct
53 Correct 800 ms 392 KB Output is correct
54 Correct 803 ms 396 KB Output is correct
55 Correct 798 ms 396 KB Output is correct