답안 #896552

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
896552 2024-01-01T16:37:24 Z danikoynov Worst Reporter 4 (JOI21_worst_reporter4) C++14
79 / 100
732 ms 184076 KB
/**
 ____ ____ ____ ____ ____ ____
||l |||e |||i |||n |||a |||d ||
||__|||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|/__\|

**/

#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);
}


const int maxn = 2e5 + 10;
const ll inf = 1e18;

int n, a[maxn], h[maxn];
ll c[maxn];
unordered_set < int > adj[maxn], g[maxn];
void input()
{
    cin >> n;
    for (int i = 1; i <= n; i ++)
    {
        cin >> a[i] >> h[i] >> c[i];
        if (a[i] != i)
            adj[a[i]].insert(i);
        if (a[i] != i)
            g[i].insert(a[i]);
    }
}

unordered_map < int, int > tag;

set < int > met;
vector < int > cycle;
int used[maxn], par[maxn];

void find_cycle(int v)
{
    used[v] = 1;
    for (int u : g[v])
    {
        if (used[u] == 2)
            continue;
        if (used[u] == 0)
        {
            par[u] = v;
            find_cycle(u);
        }
        else
        {
            int cur = v;
            while(cur != u)
            {
                ///cout << cur << " : " << u << endl;
                cycle.push_back(cur);
                met.insert(cur);
                cur = par[cur];
            }
            met.insert(cur);
            cycle.push_back(cur);
        }

    }
    used[v] = 2;
}

bool cmp(int v, int u)
{
    return h[v] > h[u];
}

void fix_graph()
{
    for (int i = 1; i <= n; i ++)
    {
        if (!used[i])
        {
            cycle.clear();
            met.clear();
            par[i] = -1;
            find_cycle(i);
            if (cycle.empty())
                continue;
            /**for (int v : cycle)
            cout << v << " ";
            cout << endl;
            cout << "-------------" << endl;*/
            vector < int > pivots;
            for (int cur : adj[cycle.back()])
            {
                if (met.find(cur) == met.end())
                    pivots.push_back(cur);
            }
            for (int i = 0; i < cycle.size(); i ++)
            {
                int j = i + 1;
                j %= (int)(cycle.size());
                ///assert(adj[cycle[i]].find(cycle[j]) != adj[cycle[i]].end());
                adj[cycle[i]].erase(cycle[j]);
            }

            for (int last : pivots)
                adj[cycle.back()].erase(last);

                //for (int ver : pivots)
                  ///  cout << "last " << ver << " " << cycle.back() << endl;
            sort(cycle.begin(), cycle.end(), cmp);
            for (int last : pivots)
                adj[cycle.back()].insert(last);
            for (int i = 1; i < cycle.size(); i ++)
            {
                adj[cycle[i - 1]].insert(cycle[i]);
                //cout << "edge " << cycle[i - 1] << " " << cycle[i] << endl;
            }
            /**cout << "--------" << endl;
            for (int cur : cycle)
                cout << cur << " " << h[cur] << endl;
            cout << endl;*/
        }
    }
}
void compress()
{
    vector < int > values;
    for (int i = 1; i <= n; i ++)
        values.push_back(h[i]);

    sort(values.begin(), values.end());

    for (int i = 0; i < n; i ++)
        tag[values[i]] = i + 1;

}

ll temp[maxn];

set < pair < int, ll > > spot[maxn];

void add_to_set(int v, pair < int, ll > cur)
{
    set < pair < int, ll > > :: iterator it = spot[v].lower_bound({cur.first, -inf});

    if (it != spot[v].end() && it -> first == cur.first)
    {
        cur.second += it -> second;

        spot[v].erase(it);
    }
    spot[v].insert(cur);
}

void dfs(int v)
{
    ///cout << "dfs " << v << endl;

    used[v] = 1;
    for (int u : adj[v])
    {
        ///cout << "nb " << v << " " << u << endl;
        dfs(u);

        if (spot[u].size() > spot[v].size())
            swap(spot[v], spot[u]);

        for (pair < int, ll > cur : spot[u])
            add_to_set(v, cur);

    }


    set < pair < int, ll > > :: iterator it;
    ll sum = 0;
    while(spot[v].size() > 0)
    {

        it = spot[v].lower_bound({tag[h[v]], inf});
        ///if (it != spot[v].end())

        if (it == spot[v].begin())
            break;
        it = prev(it);

        if (sum + it -> second > c[v])
        {
            pair < int, ll > nw = *it;
            nw.second -= (c[v] - sum);
            sum = c[v];
            spot[v].erase(it);
            add_to_set(v, nw);
            ///spot[v].insert(nw);
            break;
        }
        else
        {
            sum += it -> second;
            spot[v].erase(it);
        }

    }

    add_to_set(v, {1, sum});
    add_to_set(v, {tag[h[v]] + 1, c[v]});



}
int deg[maxn];
void calculate_states()
{
    for (int i = 1; i <= n; i ++)
        for (int u : adj[i])
        deg[u] ++;

    ll ans = 0;
    for (int i = 1; i <= n; i ++)
    {
        if (deg[i] != 0)
            continue;

        dfs(i);

        ll s = 0;
        for (pair < int, ll > cur : spot[i])
        {
            if (cur.first == 1)
                s += cur.second;
        }
        ans += s;
    }

    cout << ans << endl;
}
void solve()
{
    input();
    fix_graph();
    memset(used, 0, sizeof(used));
    compress();
    calculate_states();
}

int main()
{
    ///speed();
    ///freopen("test.txt", "r", stdin);
    solve();
    return 0;
}
/**
4
2 3 5
3 2 4
4 5 7
1 4 3
*/

Compilation message

worst_reporter2.cpp: In function 'void fix_graph()':
worst_reporter2.cpp:105:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  105 |             for (int i = 0; i < cycle.size(); i ++)
      |                             ~~^~~~~~~~~~~~~~
worst_reporter2.cpp:121:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  121 |             for (int i = 1; i < cycle.size(); i ++)
      |                             ~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 37212 KB Output is correct
2 Correct 8 ms 37208 KB Output is correct
3 Correct 8 ms 37352 KB Output is correct
4 Correct 9 ms 37468 KB Output is correct
5 Correct 19 ms 40028 KB Output is correct
6 Correct 17 ms 39408 KB Output is correct
7 Correct 15 ms 39004 KB Output is correct
8 Correct 18 ms 39960 KB Output is correct
9 Correct 17 ms 39264 KB Output is correct
10 Correct 15 ms 39004 KB Output is correct
11 Correct 15 ms 38752 KB Output is correct
12 Correct 17 ms 39520 KB Output is correct
13 Correct 15 ms 39264 KB Output is correct
14 Correct 17 ms 39264 KB Output is correct
15 Correct 16 ms 39008 KB Output is correct
16 Correct 21 ms 40280 KB Output is correct
17 Correct 16 ms 39412 KB Output is correct
18 Correct 14 ms 39120 KB Output is correct
19 Correct 17 ms 39520 KB Output is correct
20 Correct 15 ms 39184 KB Output is correct
21 Correct 16 ms 39012 KB Output is correct
22 Correct 16 ms 39528 KB Output is correct
23 Correct 15 ms 38936 KB Output is correct
24 Correct 18 ms 39524 KB Output is correct
25 Correct 15 ms 39008 KB Output is correct
26 Correct 18 ms 39480 KB Output is correct
27 Correct 16 ms 39524 KB Output is correct
28 Correct 16 ms 39448 KB Output is correct
29 Correct 15 ms 39504 KB Output is correct
30 Correct 16 ms 39768 KB Output is correct
31 Correct 17 ms 39772 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 37212 KB Output is correct
2 Correct 8 ms 37208 KB Output is correct
3 Correct 8 ms 37352 KB Output is correct
4 Correct 9 ms 37468 KB Output is correct
5 Correct 19 ms 40028 KB Output is correct
6 Correct 17 ms 39408 KB Output is correct
7 Correct 15 ms 39004 KB Output is correct
8 Correct 18 ms 39960 KB Output is correct
9 Correct 17 ms 39264 KB Output is correct
10 Correct 15 ms 39004 KB Output is correct
11 Correct 15 ms 38752 KB Output is correct
12 Correct 17 ms 39520 KB Output is correct
13 Correct 15 ms 39264 KB Output is correct
14 Correct 17 ms 39264 KB Output is correct
15 Correct 16 ms 39008 KB Output is correct
16 Correct 21 ms 40280 KB Output is correct
17 Correct 16 ms 39412 KB Output is correct
18 Correct 14 ms 39120 KB Output is correct
19 Correct 17 ms 39520 KB Output is correct
20 Correct 15 ms 39184 KB Output is correct
21 Correct 16 ms 39012 KB Output is correct
22 Correct 16 ms 39528 KB Output is correct
23 Correct 15 ms 38936 KB Output is correct
24 Correct 18 ms 39524 KB Output is correct
25 Correct 15 ms 39008 KB Output is correct
26 Correct 18 ms 39480 KB Output is correct
27 Correct 16 ms 39524 KB Output is correct
28 Correct 16 ms 39448 KB Output is correct
29 Correct 15 ms 39504 KB Output is correct
30 Correct 16 ms 39768 KB Output is correct
31 Correct 17 ms 39772 KB Output is correct
32 Correct 19 ms 40156 KB Output is correct
33 Correct 732 ms 168496 KB Output is correct
34 Correct 557 ms 130916 KB Output is correct
35 Correct 685 ms 168396 KB Output is correct
36 Correct 525 ms 130796 KB Output is correct
37 Correct 441 ms 106876 KB Output is correct
38 Correct 458 ms 101416 KB Output is correct
39 Correct 465 ms 125016 KB Output is correct
40 Correct 369 ms 116044 KB Output is correct
41 Correct 305 ms 116012 KB Output is correct
42 Correct 427 ms 117200 KB Output is correct
43 Correct 369 ms 108236 KB Output is correct
44 Correct 611 ms 184076 KB Output is correct
45 Correct 439 ms 139264 KB Output is correct
46 Correct 289 ms 102060 KB Output is correct
47 Correct 531 ms 125404 KB Output is correct
48 Correct 362 ms 109632 KB Output is correct
49 Correct 302 ms 109512 KB Output is correct
50 Correct 539 ms 126796 KB Output is correct
51 Correct 296 ms 105268 KB Output is correct
52 Correct 607 ms 127124 KB Output is correct
53 Correct 346 ms 111028 KB Output is correct
54 Correct 341 ms 125132 KB Output is correct
55 Correct 497 ms 129600 KB Output is correct
56 Correct 442 ms 130816 KB Output is correct
57 Correct 452 ms 132652 KB Output is correct
58 Correct 476 ms 135204 KB Output is correct
59 Correct 460 ms 135112 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 37212 KB Output is correct
2 Correct 8 ms 37208 KB Output is correct
3 Correct 8 ms 37352 KB Output is correct
4 Correct 9 ms 37468 KB Output is correct
5 Correct 19 ms 40028 KB Output is correct
6 Correct 17 ms 39408 KB Output is correct
7 Correct 15 ms 39004 KB Output is correct
8 Correct 18 ms 39960 KB Output is correct
9 Correct 17 ms 39264 KB Output is correct
10 Correct 15 ms 39004 KB Output is correct
11 Correct 15 ms 38752 KB Output is correct
12 Correct 17 ms 39520 KB Output is correct
13 Correct 15 ms 39264 KB Output is correct
14 Correct 17 ms 39264 KB Output is correct
15 Correct 16 ms 39008 KB Output is correct
16 Correct 21 ms 40280 KB Output is correct
17 Correct 16 ms 39412 KB Output is correct
18 Correct 14 ms 39120 KB Output is correct
19 Correct 17 ms 39520 KB Output is correct
20 Correct 15 ms 39184 KB Output is correct
21 Correct 16 ms 39012 KB Output is correct
22 Correct 16 ms 39528 KB Output is correct
23 Correct 15 ms 38936 KB Output is correct
24 Correct 18 ms 39524 KB Output is correct
25 Correct 15 ms 39008 KB Output is correct
26 Correct 18 ms 39480 KB Output is correct
27 Correct 16 ms 39524 KB Output is correct
28 Correct 16 ms 39448 KB Output is correct
29 Correct 15 ms 39504 KB Output is correct
30 Correct 16 ms 39768 KB Output is correct
31 Correct 17 ms 39772 KB Output is correct
32 Correct 19 ms 40156 KB Output is correct
33 Correct 732 ms 168496 KB Output is correct
34 Correct 557 ms 130916 KB Output is correct
35 Correct 685 ms 168396 KB Output is correct
36 Correct 525 ms 130796 KB Output is correct
37 Correct 441 ms 106876 KB Output is correct
38 Correct 458 ms 101416 KB Output is correct
39 Correct 465 ms 125016 KB Output is correct
40 Correct 369 ms 116044 KB Output is correct
41 Correct 305 ms 116012 KB Output is correct
42 Correct 427 ms 117200 KB Output is correct
43 Correct 369 ms 108236 KB Output is correct
44 Correct 611 ms 184076 KB Output is correct
45 Correct 439 ms 139264 KB Output is correct
46 Correct 289 ms 102060 KB Output is correct
47 Correct 531 ms 125404 KB Output is correct
48 Correct 362 ms 109632 KB Output is correct
49 Correct 302 ms 109512 KB Output is correct
50 Correct 539 ms 126796 KB Output is correct
51 Correct 296 ms 105268 KB Output is correct
52 Correct 607 ms 127124 KB Output is correct
53 Correct 346 ms 111028 KB Output is correct
54 Correct 341 ms 125132 KB Output is correct
55 Correct 497 ms 129600 KB Output is correct
56 Correct 442 ms 130816 KB Output is correct
57 Correct 452 ms 132652 KB Output is correct
58 Correct 476 ms 135204 KB Output is correct
59 Correct 460 ms 135112 KB Output is correct
60 Correct 8 ms 37208 KB Output is correct
61 Correct 8 ms 37212 KB Output is correct
62 Correct 8 ms 37344 KB Output is correct
63 Correct 9 ms 37392 KB Output is correct
64 Correct 671 ms 144280 KB Output is correct
65 Incorrect 528 ms 118592 KB Output isn't correct
66 Halted 0 ms 0 KB -