답안 #1037084

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1037084 2024-07-28T03:24:32 Z LucaDantas Candies (JOI18_candies) C++17
100 / 100
80 ms 18868 KB
#include <bits/stdc++.h>
using namespace std;

template<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { os << '['; string sep = ""; for (const auto &x : v) os << sep << x, sep = ", "; return os << ']'; }
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename A> ostream& operator<<(ostream &os, const set<A> &s) { os << '{'; string sep = ""; for (const auto &x : s) os << sep << x, sep = ", "; return os << '}'; }
template<typename A, typename B> ostream& operator<<(ostream &os, const map<A, B> &m) { os << '{'; string sep = ""; for (const auto &x : m) os << sep << x.first << " -> " << x.second, sep = ", "; return os << '}'; }

void debug() { cerr << endl; }
template<typename Ini, typename... Fim> void debug(Ini I, Fim... F) { cerr << I; if(sizeof...(F)) cerr << ", "; debug(F...);}
#define db(...) cerr << "DEBUG (" << #__VA_ARGS__ << ") == ", debug(__VA_ARGS__)

constexpr int maxn = 2e5+10;

#define int long long

struct DSU {
    int pai[maxn], peso[maxn], l[maxn], r[maxn], val[maxn], filho[maxn]; // filho eh se tem algum do meu lado ativado
    void init(int a[]) { for(int i = 0; i < maxn; i++) pai[i] = l[i] = r[i] = i, peso[i] = 1, val[i] = a[i]; }
    int find(int x) { return pai[x] == x ? x : pai[x] = find(pai[x]); }
    void merge_dsu(int a, int b) {
        a = find(a), b = find(b);
        if(peso[a] < peso[b])
            swap(a, b);
        pai[b] = a;
        peso[a] += peso[b];
        l[a] = min(l[a], l[b]);
        r[a] = max(r[a], r[b]);
    }
    void merge(int p, int f1, int f2) {
        // nao eh pra precisar dar find nessa funcao, isso eu farei antes de chamar pros caras necessarios
        int new_val = val[f1] + val[f2] - val[p];
        // filho nao sobe, sempre ta salvo no do pai e eh usado quando vai dar merge, entao n faz nada com ele
        merge_dsu(p, f1);
        merge_dsu(p, f2);
        int raiz = find(p);
        val[raiz] = new_val;
        filho[raiz] = 0; // nao tem mais nenhum filho, deu merge
    }
    bool set_filho(int colocar, int p) { // retorna true se deu merge
        colocar = find(colocar), p = find(p);
        if(!filho[p])
            return (filho[p] = colocar, false);

        int f2 = find(filho[p]);
        merge(p, colocar, f2);

        return true;
    }
} dsu;

int a[maxn];

bool disponiveis[maxn];

priority_queue<pair<int,int>> q;

int32_t main() {
    int n; scanf("%lld", &n);
    for(int i = 1; i <= n; i++)
        scanf("%lld", a+i), q.push({a[i], i});

    memset(disponiveis, 1, sizeof disponiveis);

    dsu.init(a);

    long long ans = 0;
    while(q.size()) {
        auto [val, colocar] = q.top();
        q.pop();
        colocar = dsu.find(colocar);
        int l = dsu.l[colocar], r = dsu.r[colocar];
        // db("TOME PORRA", val, colocar, l, r);
        if(disponiveis[l-1] && disponiveis[r+1]) {
            ans += dsu.val[colocar];
            disponiveis[l] = disponiveis[r] = 0;
            // db("AGORA", ans);
            printf("%lld\n", ans);
            // k++; // so nesse caso q eu conto mais pra resposta
            continue;
        }
        int k = disponiveis[l-1] ? r+1 : l-1; // sei com certeza que pelo menos um deles ta disponivel
        // db("merge", colocar, k);
        if(dsu.set_filho(colocar, k)) {
            // db("CARALLLHOOOO");
            // ja consegui dar um merge, nem continuo pra colocar filho no outro, so recoloco ele na fila e fds
            colocar = dsu.find(colocar);
            q.push({dsu.val[colocar], colocar});
        }
    }
}

Compilation message

candies.cpp: In function 'int32_t main()':
candies.cpp:59:17: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   59 |     int n; scanf("%lld", &n);
      |            ~~~~~^~~~~~~~~~~~
candies.cpp:61:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   61 |         scanf("%lld", a+i), q.push({a[i], i});
      |         ~~~~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10840 KB Output is correct
2 Correct 2 ms 10840 KB Output is correct
3 Correct 2 ms 10844 KB Output is correct
4 Correct 2 ms 10844 KB Output is correct
5 Correct 2 ms 10844 KB Output is correct
6 Correct 2 ms 10844 KB Output is correct
7 Correct 2 ms 10844 KB Output is correct
8 Correct 2 ms 10844 KB Output is correct
9 Correct 2 ms 10844 KB Output is correct
10 Correct 2 ms 10840 KB Output is correct
11 Correct 2 ms 10840 KB Output is correct
12 Correct 2 ms 10844 KB Output is correct
13 Correct 2 ms 10844 KB Output is correct
14 Correct 2 ms 10844 KB Output is correct
15 Correct 2 ms 10844 KB Output is correct
16 Correct 2 ms 10844 KB Output is correct
17 Correct 2 ms 10844 KB Output is correct
18 Correct 2 ms 10844 KB Output is correct
19 Correct 2 ms 10844 KB Output is correct
20 Correct 3 ms 10844 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10840 KB Output is correct
2 Correct 2 ms 10840 KB Output is correct
3 Correct 2 ms 10844 KB Output is correct
4 Correct 2 ms 10844 KB Output is correct
5 Correct 2 ms 10844 KB Output is correct
6 Correct 2 ms 10844 KB Output is correct
7 Correct 2 ms 10844 KB Output is correct
8 Correct 2 ms 10844 KB Output is correct
9 Correct 2 ms 10844 KB Output is correct
10 Correct 2 ms 10840 KB Output is correct
11 Correct 2 ms 10840 KB Output is correct
12 Correct 2 ms 10844 KB Output is correct
13 Correct 2 ms 10844 KB Output is correct
14 Correct 2 ms 10844 KB Output is correct
15 Correct 2 ms 10844 KB Output is correct
16 Correct 2 ms 10844 KB Output is correct
17 Correct 2 ms 10844 KB Output is correct
18 Correct 2 ms 10844 KB Output is correct
19 Correct 2 ms 10844 KB Output is correct
20 Correct 3 ms 10844 KB Output is correct
21 Correct 79 ms 17848 KB Output is correct
22 Correct 78 ms 18612 KB Output is correct
23 Correct 78 ms 18108 KB Output is correct
24 Correct 55 ms 18100 KB Output is correct
25 Correct 56 ms 18096 KB Output is correct
26 Correct 55 ms 17980 KB Output is correct
27 Correct 71 ms 18232 KB Output is correct
28 Correct 67 ms 18104 KB Output is correct
29 Correct 64 ms 18356 KB Output is correct
30 Correct 63 ms 18100 KB Output is correct
31 Correct 67 ms 18196 KB Output is correct
32 Correct 68 ms 18276 KB Output is correct
33 Correct 71 ms 18360 KB Output is correct
34 Correct 69 ms 18568 KB Output is correct
35 Correct 70 ms 17864 KB Output is correct
36 Correct 75 ms 18100 KB Output is correct
37 Correct 80 ms 18356 KB Output is correct
38 Correct 77 ms 18096 KB Output is correct
39 Correct 56 ms 18384 KB Output is correct
40 Correct 55 ms 18104 KB Output is correct
41 Correct 57 ms 18100 KB Output is correct
42 Correct 66 ms 18868 KB Output is correct
43 Correct 66 ms 18276 KB Output is correct
44 Correct 65 ms 18028 KB Output is correct
45 Correct 69 ms 18100 KB Output is correct
46 Correct 65 ms 18120 KB Output is correct
47 Correct 64 ms 18612 KB Output is correct
48 Correct 70 ms 17844 KB Output is correct
49 Correct 71 ms 17844 KB Output is correct
50 Correct 77 ms 17888 KB Output is correct