Submission #503511

# Submission time Handle Problem Language Result Execution time Memory
503511 2022-01-08T08:55:07 Z InternetPerson10 Group Photo (JOI21_ho_t3) C++17
100 / 100
768 ms 313296 KB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

const ll BIG = 1234567891234567890;

ll ans[5001];
ll dp[5001][5001];
ll permute_people[5001][5001]; // Permute people [i, j] once they are already on the left
ll push_people[5001][5001]; // Push people [i, j] to the left

struct FTree {
    vector<int> nums;
    int size = 1;
    void init(int n) {
        vector<int>().swap(nums);
        size = 1;
        while(size <= n) size *= 2;
        nums.resize(size+1);
    }
    void add(int n, int k) {
        while(n <= size) {
            nums[n] += k;
            n += (n & (-n));
        }
    }
    int sum(int n) {
        int ans = 0;
        while(n) {
            ans += nums[n];
            n -= (n & (-n));
        }
        return ans;
    }
};

ll swap_photo(vector<int> nums) {
    int n = nums.size();
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < n; j++) {
            dp[i][j] = 0;
        }
    }
    vector<int> ind(n);
    for(int i = 0; i < n; i++) ind[nums[i]] = i;
    FTree ft, ft2;
    for(int i = 0; i < n; i++) {
        ft.init(n+1);
        ft2.init(n+1);
        for(int j = i; j < n; j++) ft2.add(ind[j]+1, 1);
        for(int j = i; j < n; j++) {
            if(i == j) push_people[i][j] = ft2.sum(ind[j]+1) - 1;
            else push_people[i][j] = push_people[i][j-1] + ft2.sum(ind[j]+1) - 1 - (j - i) + ft.sum(ind[j]+1);
            ft.add(ind[j]+1, 1);
        }
    }
    for(int i = n-1; i >= 0; i--) {
        ans[i] = BIG;
        for(int j = i; j < n; j++) {
            dp[i][j] = push_people[i][j] + ans[j+1];
            ans[i] = min(ans[i], dp[i][j]);
        }
    }
    return ans[0];
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    vector<int> nums(n);
    for(int i = 0; i < n; i++) cin >> nums[i];
    for(int i = 0; i < n; i++) nums[i]--;
    cout << swap_photo(nums);
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 324 KB Output is correct
2 Correct 0 ms 320 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 0 ms 320 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 0 ms 332 KB Output is correct
8 Correct 0 ms 332 KB Output is correct
9 Correct 0 ms 316 KB Output is correct
10 Correct 0 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 324 KB Output is correct
2 Correct 0 ms 320 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 0 ms 320 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 0 ms 332 KB Output is correct
8 Correct 0 ms 332 KB Output is correct
9 Correct 0 ms 316 KB Output is correct
10 Correct 0 ms 332 KB Output is correct
11 Correct 0 ms 448 KB Output is correct
12 Correct 0 ms 460 KB Output is correct
13 Correct 1 ms 460 KB Output is correct
14 Correct 1 ms 452 KB Output is correct
15 Correct 0 ms 460 KB Output is correct
16 Correct 0 ms 452 KB Output is correct
17 Correct 1 ms 460 KB Output is correct
18 Correct 0 ms 460 KB Output is correct
19 Correct 1 ms 460 KB Output is correct
20 Correct 0 ms 460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 324 KB Output is correct
2 Correct 0 ms 320 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 0 ms 320 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 0 ms 332 KB Output is correct
8 Correct 0 ms 332 KB Output is correct
9 Correct 0 ms 316 KB Output is correct
10 Correct 0 ms 332 KB Output is correct
11 Correct 0 ms 448 KB Output is correct
12 Correct 0 ms 460 KB Output is correct
13 Correct 1 ms 460 KB Output is correct
14 Correct 1 ms 452 KB Output is correct
15 Correct 0 ms 460 KB Output is correct
16 Correct 0 ms 452 KB Output is correct
17 Correct 1 ms 460 KB Output is correct
18 Correct 0 ms 460 KB Output is correct
19 Correct 1 ms 460 KB Output is correct
20 Correct 0 ms 460 KB Output is correct
21 Correct 2 ms 2380 KB Output is correct
22 Correct 2 ms 2380 KB Output is correct
23 Correct 2 ms 2380 KB Output is correct
24 Correct 3 ms 2400 KB Output is correct
25 Correct 2 ms 2368 KB Output is correct
26 Correct 2 ms 2380 KB Output is correct
27 Correct 2 ms 2380 KB Output is correct
28 Correct 2 ms 2380 KB Output is correct
29 Correct 2 ms 2380 KB Output is correct
30 Correct 2 ms 2380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 324 KB Output is correct
2 Correct 0 ms 320 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 0 ms 320 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 0 ms 332 KB Output is correct
8 Correct 0 ms 332 KB Output is correct
9 Correct 0 ms 316 KB Output is correct
10 Correct 0 ms 332 KB Output is correct
11 Correct 0 ms 448 KB Output is correct
12 Correct 0 ms 460 KB Output is correct
13 Correct 1 ms 460 KB Output is correct
14 Correct 1 ms 452 KB Output is correct
15 Correct 0 ms 460 KB Output is correct
16 Correct 0 ms 452 KB Output is correct
17 Correct 1 ms 460 KB Output is correct
18 Correct 0 ms 460 KB Output is correct
19 Correct 1 ms 460 KB Output is correct
20 Correct 0 ms 460 KB Output is correct
21 Correct 2 ms 2380 KB Output is correct
22 Correct 2 ms 2380 KB Output is correct
23 Correct 2 ms 2380 KB Output is correct
24 Correct 3 ms 2400 KB Output is correct
25 Correct 2 ms 2368 KB Output is correct
26 Correct 2 ms 2380 KB Output is correct
27 Correct 2 ms 2380 KB Output is correct
28 Correct 2 ms 2380 KB Output is correct
29 Correct 2 ms 2380 KB Output is correct
30 Correct 2 ms 2380 KB Output is correct
31 Correct 18 ms 14268 KB Output is correct
32 Correct 18 ms 14284 KB Output is correct
33 Correct 18 ms 14284 KB Output is correct
34 Correct 19 ms 14324 KB Output is correct
35 Correct 19 ms 14328 KB Output is correct
36 Correct 19 ms 14352 KB Output is correct
37 Correct 19 ms 14288 KB Output is correct
38 Correct 19 ms 14308 KB Output is correct
39 Correct 19 ms 14328 KB Output is correct
40 Correct 18 ms 14304 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 324 KB Output is correct
2 Correct 0 ms 320 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 0 ms 320 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 0 ms 332 KB Output is correct
8 Correct 0 ms 332 KB Output is correct
9 Correct 0 ms 316 KB Output is correct
10 Correct 0 ms 332 KB Output is correct
11 Correct 0 ms 448 KB Output is correct
12 Correct 0 ms 460 KB Output is correct
13 Correct 1 ms 460 KB Output is correct
14 Correct 1 ms 452 KB Output is correct
15 Correct 0 ms 460 KB Output is correct
16 Correct 0 ms 452 KB Output is correct
17 Correct 1 ms 460 KB Output is correct
18 Correct 0 ms 460 KB Output is correct
19 Correct 1 ms 460 KB Output is correct
20 Correct 0 ms 460 KB Output is correct
21 Correct 2 ms 2380 KB Output is correct
22 Correct 2 ms 2380 KB Output is correct
23 Correct 2 ms 2380 KB Output is correct
24 Correct 3 ms 2400 KB Output is correct
25 Correct 2 ms 2368 KB Output is correct
26 Correct 2 ms 2380 KB Output is correct
27 Correct 2 ms 2380 KB Output is correct
28 Correct 2 ms 2380 KB Output is correct
29 Correct 2 ms 2380 KB Output is correct
30 Correct 2 ms 2380 KB Output is correct
31 Correct 18 ms 14268 KB Output is correct
32 Correct 18 ms 14284 KB Output is correct
33 Correct 18 ms 14284 KB Output is correct
34 Correct 19 ms 14324 KB Output is correct
35 Correct 19 ms 14328 KB Output is correct
36 Correct 19 ms 14352 KB Output is correct
37 Correct 19 ms 14288 KB Output is correct
38 Correct 19 ms 14308 KB Output is correct
39 Correct 19 ms 14328 KB Output is correct
40 Correct 18 ms 14304 KB Output is correct
41 Correct 768 ms 313016 KB Output is correct
42 Correct 757 ms 313008 KB Output is correct
43 Correct 747 ms 313192 KB Output is correct
44 Correct 737 ms 313044 KB Output is correct
45 Correct 737 ms 313152 KB Output is correct
46 Correct 743 ms 313268 KB Output is correct
47 Correct 752 ms 313188 KB Output is correct
48 Correct 737 ms 313180 KB Output is correct
49 Correct 739 ms 313172 KB Output is correct
50 Correct 742 ms 313248 KB Output is correct
51 Correct 743 ms 313288 KB Output is correct
52 Correct 741 ms 313156 KB Output is correct
53 Correct 734 ms 313284 KB Output is correct
54 Correct 734 ms 313252 KB Output is correct
55 Correct 741 ms 313296 KB Output is correct