답안 #851765

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
851765 2023-09-20T14:47:51 Z danikoynov 별자리 3 (JOI20_constellation3) C++14
35 / 100
891 ms 524288 KB
#include<bits/stdc++.h>
#define endl '\n'

using namespace std;
typedef long long ll;

void speed()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}

struct poll
{
    ll x, y, c;
    bool operator < (const poll &p) const
    {
        if (y == p.y)
            return x < p.x;
        return y < p.y;
    }
};

const ll maxn = 2e5 + 10;

ll n, m, a[maxn], lc[maxn], rc[maxn];
poll s[maxn];
vector < ll > star[maxn];
vector < ll > dp[maxn];
void input()
{
    cin >> n;
    for (ll i = 1; i <= n; i ++)
        cin >> a[i];

    cin >> m;


    for (ll i = 1; i <= m; i ++)
    {
        cin >> s[i].x >> s[i].y >> s[i].c;
    }

    sort(s + 1, s + m + 1);

    for (ll i = 1; i <= m; i ++)
    {

        star[s[i].x].push_back(i);
    }
}

ll cartesian_tree()
{

    for (ll i = 1; i <= n; i ++)
    {
        lc[i] = rc[i] = -1;
    }

    stack < ll > st;
    for (ll i = 1; i <= n; i ++)
    {
        while(!st.empty() && a[st.top()] < a[i])
        {
            lc[i] = st.top();
            st.pop();
        }

        st.push(i);
    }

    while(!st.empty())
        st.pop();
    for (ll i = n; i > 0; i --)
    {
        while(!st.empty() && a[st.top()] <= a[i])
        {
            rc[i] = st.top();
            st.pop();
        }

        st.push(i);
    }

    ll root;
    while(!st.empty())
    {

        root = st.top();
        st.pop();
    }
    /**cout << "cartesian tree" << endl;
    cout << root << endl;
    for (ll i = 1; i <= n; i ++)
    {
        cout << lc[i] << " " << rc[i] << endl;
    }*/
    return root;
}

const ll inf = 1e18;

ll rev[maxn];
ll temp[maxn];
pair < vector < ll >, vector < ll > > combine(vector < ll > &dp_lf, vector < ll > &dp_rf,
        vector < ll > &ls, vector < ll > &rs, ll height)
{

    int p1 = 0;
    int p2 = 0;
    vector < ll > comb;
    while(p1 < ls.size() && p2 < rs.size())
    {
        if (ls[p1] < rs[p2])
            comb.push_back(ls[p1 ++]);
        else
            comb.push_back(rs[p2 ++]);
    }
    while(p1 < ls.size())
        comb.push_back(ls[p1 ++]);
    while(p2 < rs.size())
        comb.push_back(rs[p2 ++]);


    ll lf_sz = ls.size(), rf_sz = rs.size();
    for (ll i = 0; i <= lf_sz + rf_sz; i ++)
    {
        if (i > 0)
            rev[comb[i - 1]] = i;
        temp[i] = inf;
    }



    /**for (ll i = 1; i <= lf_sz; i ++)
        for (ll j = 1; j <= rf_sz; j ++)
        {
            if (s[ls[i - 1]].y > height && s[rs[j - 1]].y > height)
                continue;
            ///cout << "combine " << i << " " << j << " " << s[ls[i - 1]].y << " " << s[rs[j - 1]].y << endl;
            ll idx = max(ls[i - 1], rs[j - 1]);
            temp[rev[idx]] = min(temp[rev[idx]], dp_lf[i] + dp_rf[j]);
        }*/

    int pt = 1;
    ll best = dp_rf[0];
    for (ll i = 1; i <= lf_sz; i ++)
    {
        while(pt <= rf_sz && s[rs[pt - 1]].y <= min(s[ls[i - 1]].y, height))
        {
            best = min(best, dp_rf[pt]);
            pt ++;
        }
        temp[rev[ls[i - 1]]] = min(temp[rev[ls[i - 1]]], dp_lf[i] + best);
    }

    pt = 1;
    best = dp_lf[0];
    for (ll i = 1; i <= rf_sz; i ++)
    {
        while(pt <= lf_sz && s[ls[pt - 1]].y <= min(s[rs[i - 1]].y, height))
        {
            best = min(best, dp_lf[pt]);
            pt ++;
        }
        temp[rev[rs[i - 1]]] = min(temp[rev[rs[i - 1]]], dp_rf[i] + best);
    }

    temp[0] = min(temp[0], dp_lf[0] + dp_rf[0]);

    vector < ll > dp(comb.size() + 1);
    for (ll i = 0; i <= comb.size(); i ++)
    {
        dp[i] = temp[i];
    }

    vector < ll > new_dp, new_comb;
    ll und = dp[0];
    for (int i = 1; i <= comb.size(); i ++)
        if (s[comb[i - 1]].y <= height)
            und = min(und, dp[i]);

    new_dp.push_back(und);
    for (int i = 1; i <= comb.size(); i ++)
    {
        if (s[comb[i - 1]].y > height && new_dp.back() > dp[i])
        {
            new_dp.push_back(dp[i]);
            new_comb.push_back(comb[i - 1]);
        }
    }


    return {new_dp, new_comb};


}
void divide(ll col)
{

    if (lc[col] != -1) divide(lc[col]);
    if (rc[col] != -1) divide(rc[col]);

    ll col_sz = star[col].size();
    dp[col].resize(col_sz + 1);
    ll sum = 0;
    for (ll i = 0; i < col_sz; i ++)
        sum += s[star[col][i]].c;

    dp[col][0] = sum;
    for (ll i = 1; i <= col_sz; i ++)
        dp[col][i] = sum - s[star[col][i - 1]].c;


    if (lc[col] != -1)
    {
        pair < vector < ll >, vector < ll > > cur =
            combine(dp[lc[col]], dp[col], star[lc[col]], star[col], a[col]);

        star[col] = cur.second;
        dp[col] = cur.first;
    }

    if (rc[col] != -1)
    {
        pair < vector < ll >, vector < ll > > cur =
            combine(dp[rc[col]], dp[col], star[rc[col]], star[col], a[col]);

        star[col] = cur.second;
        dp[col] = cur.first;
    }


    /**cout << "step "<< col << endl;
    for (ll cur : star[col])
        cout << cur << " ";
    cout << endl;
    for (ll cur : dp[col])
        cout << cur << " ";
    cout << endl;*/
}
void solve()
{
    input();
    ll root = cartesian_tree();

    divide(root);
    ll ans = inf;
    for (ll i = 0; i < dp[root].size(); i ++)
        ans = min(ans, dp[root][i]);
    cout << ans << endl;
}
int main()
{
    ///freopen("test.txt", "r", stdin);
    speed();
    solve();
    return 0;
}

/**
5
3 2 2 2 3
3
1 5 3
4 3 2
2 4 2

*/

Compilation message

constellation3.cpp: In function 'std::pair<std::vector<long long int>, std::vector<long long int> > combine(std::vector<long long int>&, std::vector<long long int>&, std::vector<long long int>&, std::vector<long long int>&, ll)':
constellation3.cpp:114:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  114 |     while(p1 < ls.size() && p2 < rs.size())
      |           ~~~^~~~~~~~~~~
constellation3.cpp:114:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  114 |     while(p1 < ls.size() && p2 < rs.size())
      |                             ~~~^~~~~~~~~~~
constellation3.cpp:121:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  121 |     while(p1 < ls.size())
      |           ~~~^~~~~~~~~~~
constellation3.cpp:123:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  123 |     while(p2 < rs.size())
      |           ~~~^~~~~~~~~~~
constellation3.cpp:174:22: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  174 |     for (ll i = 0; i <= comb.size(); i ++)
      |                    ~~^~~~~~~~~~~~~~
constellation3.cpp:181:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  181 |     for (int i = 1; i <= comb.size(); i ++)
      |                     ~~^~~~~~~~~~~~~~
constellation3.cpp:186:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  186 |     for (int i = 1; i <= comb.size(); i ++)
      |                     ~~^~~~~~~~~~~~~~
constellation3.cpp: In function 'void solve()':
constellation3.cpp:251:22: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  251 |     for (ll i = 0; i < dp[root].size(); i ++)
      |                    ~~^~~~~~~~~~~~~~~~~
constellation3.cpp: In function 'll cartesian_tree()':
constellation3.cpp:100:12: warning: 'root' may be used uninitialized in this function [-Wmaybe-uninitialized]
  100 |     return root;
      |            ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 18776 KB Output is correct
2 Correct 4 ms 18780 KB Output is correct
3 Correct 4 ms 18780 KB Output is correct
4 Correct 4 ms 18776 KB Output is correct
5 Correct 3 ms 18776 KB Output is correct
6 Correct 3 ms 18776 KB Output is correct
7 Correct 4 ms 18776 KB Output is correct
8 Correct 3 ms 18776 KB Output is correct
9 Correct 4 ms 18780 KB Output is correct
10 Correct 3 ms 18776 KB Output is correct
11 Correct 4 ms 18776 KB Output is correct
12 Correct 4 ms 18776 KB Output is correct
13 Correct 3 ms 18776 KB Output is correct
14 Correct 5 ms 18776 KB Output is correct
15 Correct 3 ms 18776 KB Output is correct
16 Correct 3 ms 18776 KB Output is correct
17 Correct 3 ms 18780 KB Output is correct
18 Correct 4 ms 19032 KB Output is correct
19 Correct 3 ms 18776 KB Output is correct
20 Correct 3 ms 18776 KB Output is correct
21 Correct 4 ms 19032 KB Output is correct
22 Correct 3 ms 18776 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 18776 KB Output is correct
2 Correct 4 ms 18780 KB Output is correct
3 Correct 4 ms 18780 KB Output is correct
4 Correct 4 ms 18776 KB Output is correct
5 Correct 3 ms 18776 KB Output is correct
6 Correct 3 ms 18776 KB Output is correct
7 Correct 4 ms 18776 KB Output is correct
8 Correct 3 ms 18776 KB Output is correct
9 Correct 4 ms 18780 KB Output is correct
10 Correct 3 ms 18776 KB Output is correct
11 Correct 4 ms 18776 KB Output is correct
12 Correct 4 ms 18776 KB Output is correct
13 Correct 3 ms 18776 KB Output is correct
14 Correct 5 ms 18776 KB Output is correct
15 Correct 3 ms 18776 KB Output is correct
16 Correct 3 ms 18776 KB Output is correct
17 Correct 3 ms 18780 KB Output is correct
18 Correct 4 ms 19032 KB Output is correct
19 Correct 3 ms 18776 KB Output is correct
20 Correct 3 ms 18776 KB Output is correct
21 Correct 4 ms 19032 KB Output is correct
22 Correct 3 ms 18776 KB Output is correct
23 Correct 4 ms 19288 KB Output is correct
24 Correct 5 ms 19032 KB Output is correct
25 Correct 4 ms 19032 KB Output is correct
26 Correct 4 ms 19032 KB Output is correct
27 Correct 5 ms 19032 KB Output is correct
28 Correct 4 ms 19032 KB Output is correct
29 Correct 4 ms 19032 KB Output is correct
30 Correct 5 ms 19032 KB Output is correct
31 Correct 4 ms 19032 KB Output is correct
32 Correct 6 ms 19032 KB Output is correct
33 Correct 5 ms 19032 KB Output is correct
34 Correct 5 ms 19032 KB Output is correct
35 Correct 5 ms 19036 KB Output is correct
36 Correct 5 ms 19292 KB Output is correct
37 Correct 5 ms 19288 KB Output is correct
38 Correct 4 ms 19288 KB Output is correct
39 Correct 5 ms 19032 KB Output is correct
40 Correct 14 ms 27224 KB Output is correct
41 Correct 5 ms 19032 KB Output is correct
42 Correct 5 ms 19032 KB Output is correct
43 Correct 16 ms 27992 KB Output is correct
44 Correct 5 ms 19288 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 18776 KB Output is correct
2 Correct 4 ms 18780 KB Output is correct
3 Correct 4 ms 18780 KB Output is correct
4 Correct 4 ms 18776 KB Output is correct
5 Correct 3 ms 18776 KB Output is correct
6 Correct 3 ms 18776 KB Output is correct
7 Correct 4 ms 18776 KB Output is correct
8 Correct 3 ms 18776 KB Output is correct
9 Correct 4 ms 18780 KB Output is correct
10 Correct 3 ms 18776 KB Output is correct
11 Correct 4 ms 18776 KB Output is correct
12 Correct 4 ms 18776 KB Output is correct
13 Correct 3 ms 18776 KB Output is correct
14 Correct 5 ms 18776 KB Output is correct
15 Correct 3 ms 18776 KB Output is correct
16 Correct 3 ms 18776 KB Output is correct
17 Correct 3 ms 18780 KB Output is correct
18 Correct 4 ms 19032 KB Output is correct
19 Correct 3 ms 18776 KB Output is correct
20 Correct 3 ms 18776 KB Output is correct
21 Correct 4 ms 19032 KB Output is correct
22 Correct 3 ms 18776 KB Output is correct
23 Correct 4 ms 19288 KB Output is correct
24 Correct 5 ms 19032 KB Output is correct
25 Correct 4 ms 19032 KB Output is correct
26 Correct 4 ms 19032 KB Output is correct
27 Correct 5 ms 19032 KB Output is correct
28 Correct 4 ms 19032 KB Output is correct
29 Correct 4 ms 19032 KB Output is correct
30 Correct 5 ms 19032 KB Output is correct
31 Correct 4 ms 19032 KB Output is correct
32 Correct 6 ms 19032 KB Output is correct
33 Correct 5 ms 19032 KB Output is correct
34 Correct 5 ms 19032 KB Output is correct
35 Correct 5 ms 19036 KB Output is correct
36 Correct 5 ms 19292 KB Output is correct
37 Correct 5 ms 19288 KB Output is correct
38 Correct 4 ms 19288 KB Output is correct
39 Correct 5 ms 19032 KB Output is correct
40 Correct 14 ms 27224 KB Output is correct
41 Correct 5 ms 19032 KB Output is correct
42 Correct 5 ms 19032 KB Output is correct
43 Correct 16 ms 27992 KB Output is correct
44 Correct 5 ms 19288 KB Output is correct
45 Correct 191 ms 34260 KB Output is correct
46 Correct 190 ms 33668 KB Output is correct
47 Correct 170 ms 33616 KB Output is correct
48 Correct 170 ms 33872 KB Output is correct
49 Correct 171 ms 33496 KB Output is correct
50 Correct 178 ms 33524 KB Output is correct
51 Correct 169 ms 33616 KB Output is correct
52 Correct 171 ms 33872 KB Output is correct
53 Correct 173 ms 33928 KB Output is correct
54 Correct 239 ms 60496 KB Output is correct
55 Correct 221 ms 54044 KB Output is correct
56 Correct 209 ms 48492 KB Output is correct
57 Correct 212 ms 46928 KB Output is correct
58 Correct 225 ms 56268 KB Output is correct
59 Correct 214 ms 57828 KB Output is correct
60 Correct 136 ms 61156 KB Output is correct
61 Correct 242 ms 49652 KB Output is correct
62 Runtime error 891 ms 524288 KB Execution killed with signal 9
63 Halted 0 ms 0 KB -