답안 #960059

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
960059 2024-04-09T15:13:38 Z mickytanawin Group Photo (JOI21_ho_t3) C++17
100 / 100
4993 ms 72084 KB
// Tanawin Thongbai

#include <bits/extc++.h>
using namespace std;


class Segm_Elem {
public:
    bool is_real, is_added;
    int sum, unreal, cnt;

    Segm_Elem() {
        is_real = false;
        is_added = false;
        sum = 0;
        unreal = 0;
        cnt = 0;
    }

    Segm_Elem(int sum, int unreal, int cnt, bool is_real) {
        this -> is_real = is_real;
        this -> is_added = true;
        this -> sum = sum;
        this -> unreal = unreal;
        this -> cnt = cnt;
    }
};


const int N = 5000;

int orig[N + 1], indices[N + 1], dp_L[N + 1][N + 1], dp[N + 1];


Segm_Elem segm_merge(const Segm_Elem &a, const Segm_Elem &b) {
    if(!a.is_added) {
        return b;
    }
    if(!b.is_added) {
        return a;
    }
    if(!a.is_real) {
        if(b.is_real) {
            return Segm_Elem(b.sum, a.unreal + b.unreal, b.cnt, true);
        }
        return Segm_Elem(0, a.unreal + b.unreal, 0, false);
    }
    return Segm_Elem(a.sum + b.sum + a.cnt * b.unreal, a.unreal + b.unreal, a.cnt + b.cnt, true);
}


void segm_update(Segm_Elem *const &segm, const int &clogn, int index, Segm_Elem tr) {
    index += (1 << clogn);
    segm[index] = tr;
    for(int i = index / 2; i >= 1; i /= 2) {
        segm[i] = segm_merge(segm[i * 2], segm[i * 2 + 1]);
    }
}


Segm_Elem segm_query(Segm_Elem *const &segm, const int &clogn, int st, int ed) {
    int l = st + (1 << clogn);
    int r = ed + (1 << clogn);
    queue<Segm_Elem> l_part = queue<Segm_Elem>();
    stack<Segm_Elem> r_part = stack<Segm_Elem>();
    while(l <= r) {
        if(l % 2 == 1) {
            l_part.push(segm[l++]);
        }
        if(r % 2 == 0) {
            r_part.push(segm[r--]);
        }
        l /= 2;
        r /= 2;
    }
    bool first_time = true;
    Segm_Elem ans;
    while(!l_part.empty()) {
        Segm_Elem s = l_part.front();
        l_part.pop();
        if(first_time) {
            first_time = false;
            ans = s;
        } else {
            ans = segm_merge(ans, s);
        }
    }
    while(!r_part.empty()) {
        Segm_Elem s = r_part.top();
        r_part.pop();
        if(first_time) {
            first_time = false;
            ans = s;
        } else {
            ans = segm_merge(ans, s);
        }
    }
    return ans;
}


void fenw_update(int *const fenw, const int &n, int i, int x) {
    for(; i <= n; i += i & -i) {
        fenw[i] += x;
    }
}


int fenw_query(int *const fenw, const int &n, int i) {
    int ans = 0;
    for(; i >= 1; i -= i & -i) {
        ans += fenw[i];
    }
    return ans;
}


int main() {
    int n_elem;
    scanf("%d", &n_elem);
    for(int i = 1; i <= n_elem; ++i) {
        scanf("%d", &orig[i]);
        indices[orig[i]] = i;
    }
    for(int i = 1; i <= n_elem - 1; ++i) {
        dp_L[i][i] = 0;
        int *fenw = new int[n_elem + 1]{};
        int k = 1e9;
        for(int j = i + 1; j <= n_elem; ++j) {
            fenw_update(fenw, n_elem, n_elem - indices[j - 1] + 1, 1);
            int mv = 0;
            if(k > indices[j - 1]) {
                k = indices[j - 1];
            }
            if(k != 1e9 && k < indices[j]) {
                mv = fenw_query(fenw, n_elem, n_elem - k + 1) - fenw_query(fenw, n_elem, n_elem - indices[j]);
            }
            dp_L[i][j] = mv + dp_L[i][j - 1];
        }
        delete[] fenw;
    }
    int clogn = (int)ceil(log2(n_elem));
    Segm_Elem *segm = new Segm_Elem[1 << (clogn + 1)]{};
    for(int i = 1; i <= n_elem; ++i) {
        deque<int> deq = deque<int>();
        for(int j = 1; j <= n_elem; ++j) {
            if(i >= orig[n_elem - j + 1]) {
                segm_update(segm, clogn, j - 1, Segm_Elem(0, 1, 0, false));
            }
            if(orig[j] > i) {
                continue;
            }
            if(deq.empty() || orig[deq.back()] < orig[j]) {
                deq.push_back(j);
            }
        }
        int mn = 1e9;
        for(int j = 0; j <= i - 1; ++j) {
            while(!deq.empty() && orig[deq.front()] <= j) {
                deq.pop_front();
            }
            if(j > 0) {
                segm_update(segm, clogn, n_elem - indices[j], Segm_Elem(0, 0, 1, true));
            }
            int mv = 0;
            if(j > 0 && !deq.empty()) {
                int k = deq.front();
                Segm_Elem ans = segm_query(segm, clogn, 0, n_elem - k);
                int x = ans.sum;
                mv = x;
            }
            mn = min(mn, dp[j] + dp_L[j + 1][i] + mv);
        }
        dp[i] = mn;
        for(int j = 1; j <= n_elem; ++j) {
            if(i >= orig[n_elem - j + 1]) {
                segm_update(segm, clogn, j - 1, Segm_Elem());
            }
        }
    }
    printf("%d\n", dp[n_elem]);

    return 0;
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:120:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  120 |     scanf("%d", &n_elem);
      |     ~~~~~^~~~~~~~~~~~~~~
Main.cpp:122:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  122 |         scanf("%d", &orig[i]);
      |         ~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 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 0 ms 348 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 2 ms 2396 KB Output is correct
9 Correct 1 ms 2392 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
# 결과 실행 시간 메모리 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 0 ms 348 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 2 ms 2396 KB Output is correct
9 Correct 1 ms 2392 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2652 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 1 ms 2396 KB Output is correct
20 Correct 1 ms 2396 KB Output is correct
# 결과 실행 시간 메모리 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 0 ms 348 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 2 ms 2396 KB Output is correct
9 Correct 1 ms 2392 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2652 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 1 ms 2396 KB Output is correct
20 Correct 1 ms 2396 KB Output is correct
21 Correct 6 ms 4444 KB Output is correct
22 Correct 6 ms 4444 KB Output is correct
23 Correct 5 ms 4604 KB Output is correct
24 Correct 5 ms 4444 KB Output is correct
25 Correct 5 ms 4444 KB Output is correct
26 Correct 5 ms 4444 KB Output is correct
27 Correct 5 ms 4440 KB Output is correct
28 Correct 6 ms 4444 KB Output is correct
29 Correct 5 ms 4444 KB Output is correct
30 Correct 5 ms 4444 KB Output is correct
# 결과 실행 시간 메모리 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 0 ms 348 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 2 ms 2396 KB Output is correct
9 Correct 1 ms 2392 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2652 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 1 ms 2396 KB Output is correct
20 Correct 1 ms 2396 KB Output is correct
21 Correct 6 ms 4444 KB Output is correct
22 Correct 6 ms 4444 KB Output is correct
23 Correct 5 ms 4604 KB Output is correct
24 Correct 5 ms 4444 KB Output is correct
25 Correct 5 ms 4444 KB Output is correct
26 Correct 5 ms 4444 KB Output is correct
27 Correct 5 ms 4440 KB Output is correct
28 Correct 6 ms 4444 KB Output is correct
29 Correct 5 ms 4444 KB Output is correct
30 Correct 5 ms 4444 KB Output is correct
31 Correct 96 ms 16928 KB Output is correct
32 Correct 97 ms 16728 KB Output is correct
33 Correct 97 ms 16844 KB Output is correct
34 Correct 95 ms 16944 KB Output is correct
35 Correct 95 ms 16732 KB Output is correct
36 Correct 110 ms 16928 KB Output is correct
37 Correct 95 ms 16728 KB Output is correct
38 Correct 97 ms 16928 KB Output is correct
39 Correct 96 ms 16932 KB Output is correct
40 Correct 99 ms 16732 KB Output is correct
# 결과 실행 시간 메모리 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 0 ms 348 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 2 ms 2396 KB Output is correct
9 Correct 1 ms 2392 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2652 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 1 ms 2396 KB Output is correct
20 Correct 1 ms 2396 KB Output is correct
21 Correct 6 ms 4444 KB Output is correct
22 Correct 6 ms 4444 KB Output is correct
23 Correct 5 ms 4604 KB Output is correct
24 Correct 5 ms 4444 KB Output is correct
25 Correct 5 ms 4444 KB Output is correct
26 Correct 5 ms 4444 KB Output is correct
27 Correct 5 ms 4440 KB Output is correct
28 Correct 6 ms 4444 KB Output is correct
29 Correct 5 ms 4444 KB Output is correct
30 Correct 5 ms 4444 KB Output is correct
31 Correct 96 ms 16928 KB Output is correct
32 Correct 97 ms 16728 KB Output is correct
33 Correct 97 ms 16844 KB Output is correct
34 Correct 95 ms 16944 KB Output is correct
35 Correct 95 ms 16732 KB Output is correct
36 Correct 110 ms 16928 KB Output is correct
37 Correct 95 ms 16728 KB Output is correct
38 Correct 97 ms 16928 KB Output is correct
39 Correct 96 ms 16932 KB Output is correct
40 Correct 99 ms 16732 KB Output is correct
41 Correct 4853 ms 70744 KB Output is correct
42 Correct 4893 ms 72084 KB Output is correct
43 Correct 4895 ms 70796 KB Output is correct
44 Correct 4983 ms 70228 KB Output is correct
45 Correct 4822 ms 70484 KB Output is correct
46 Correct 4831 ms 70352 KB Output is correct
47 Correct 4875 ms 70660 KB Output is correct
48 Correct 4914 ms 70748 KB Output is correct
49 Correct 4928 ms 70040 KB Output is correct
50 Correct 4877 ms 68340 KB Output is correct
51 Correct 4993 ms 68040 KB Output is correct
52 Correct 4865 ms 69404 KB Output is correct
53 Correct 4886 ms 69476 KB Output is correct
54 Correct 4877 ms 70288 KB Output is correct
55 Correct 4922 ms 70036 KB Output is correct