답안 #655338

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
655338 2022-11-03T21:14:23 Z SpaimaCarpatilor Fireworks (APIO16_fireworks) C++17
7 / 100
5 ms 7380 KB
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <stack>
#include <cassert>
#include <map>
#include <numeric>
#include <cstring>
#include <set>
#include <ctime>
#include <queue>
#include <cmath>
#include <iomanip>
#include <iterator>
#include <bitset>
#include <unordered_map>
#include <complex>
#include <unordered_set>
#include <chrono>
#include <random>
#include <array>
#include <functional>
#include <random>

///1:54
using namespace std;

const int maxN = 300009;
int N, M, t[maxN], d[maxN], sz[maxN];
vector<int> v[maxN];

void dfsSz(int nod) {
    int pos = 0, heaviest = -1;
    sz[nod] = 1;
    for (auto it : v[nod]) {
        dfsSz(it);
        sz[nod] += sz[it];
        if (heaviest == -1 || sz[it] > sz[v[nod][heaviest]])
            heaviest = pos;
        pos ++;
    }
    if (heaviest > 0)
        swap(v[nod][heaviest], v[nod][0]);
}

struct dpFunction {
    multiset<long long> S;
    long long platformLength = 0, smallest = 0;

    long long platformStart() {
        if (S.empty()) return 0;
        return *S.rbegin();
    }

    void addD(int d) {
        if (S.empty()) {
            S.insert(d);
            return ;
        }
        auto it = S.end(); it --;
        S.insert(*it + d);
        S.erase(it);
    }

    void operator += (const dpFunction& other) {
        for (auto it : other.S)
            S.insert(it);
        smallest += other.smallest;
    }

    void print() {
        printf("{");
        for (auto it : S)
            printf("%lld,", it);
        printf("\b} min=%lld, platform=%lld", smallest, platformLength);
    }
}dp[20];

void solve(int nod, int lev) {
    if (v[nod].empty()) {
        dp[lev] = dpFunction();
        return ;
    }
    vector<long long> rightEnds;
    for (auto it : v[nod]) {
        if (it != v[nod][0]) {
            dp[lev + 1] = dpFunction();
            solve(it, lev + 1);
            dp[lev + 1].addD(d[it]);
            dp[lev] += dp[lev + 1];
            rightEnds.push_back(dp[lev + 1].platformStart() + dp[lev + 1].platformLength);
        } else {
            solve(it, lev);
            dp[lev].addD(d[it]);
            rightEnds.push_back(dp[lev].platformStart() + dp[lev].platformLength);
        }
    }
    /*printf("rightEnds:[");
    for (auto it : rightEnds)
        printf("%lld ", it);
    printf("\b]\n");*/
    //sort(rightEnds.begin(), rightEnds.end());
    for (auto x : rightEnds) {
        dp[lev].S.insert(x);
        //dp[lev].print(), printf("following %d\n", x);
        auto it2 = dp[lev].S.end(); it2 --;
        auto it = it2; it --;
        dp[lev].platformLength = *it2 - *it;
        dp[lev].smallest += *it2 - x;
        dp[lev].S.erase(it2);
    }
    //printf("%d ", nod), dp[lev].print(), printf("\n");
}

int main() {
    //freopen("../input", "r", stdin);
    //freopen("../output", "w", stdout);

    scanf("%d %d", &N, &M), N += M;
    for (int i=2; i<=N; i++) {
        scanf("%d %d", &t[i], &d[i]);
        v[t[i]].push_back(i);
    }
    dfsSz(1);
    solve(1, 0);
    printf("%lld\n", dp[0].smallest);
    return 0;
}

/*const int seed = time (0);
mt19937 gen (seed);
long long getRand(long long a, long long b) {return uniform_int_distribution < long long > (a, b) (gen);}*/

Compilation message

fireworks.cpp: In function 'int main()':
fireworks.cpp:120:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  120 |     scanf("%d %d", &N, &M), N += M;
      |     ~~~~~^~~~~~~~~~~~~~~~~
fireworks.cpp:122:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  122 |         scanf("%d %d", &t[i], &d[i]);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7252 KB Output is correct
2 Correct 4 ms 7252 KB Output is correct
3 Correct 4 ms 7252 KB Output is correct
4 Correct 5 ms 7252 KB Output is correct
5 Correct 4 ms 7252 KB Output is correct
6 Correct 4 ms 7252 KB Output is correct
7 Correct 4 ms 7336 KB Output is correct
8 Correct 4 ms 7252 KB Output is correct
9 Correct 5 ms 7248 KB Output is correct
10 Correct 4 ms 7380 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 7252 KB Output is correct
2 Correct 4 ms 7252 KB Output is correct
3 Incorrect 4 ms 7252 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7252 KB Output is correct
2 Correct 4 ms 7252 KB Output is correct
3 Correct 4 ms 7252 KB Output is correct
4 Correct 5 ms 7252 KB Output is correct
5 Correct 4 ms 7252 KB Output is correct
6 Correct 4 ms 7252 KB Output is correct
7 Correct 4 ms 7336 KB Output is correct
8 Correct 4 ms 7252 KB Output is correct
9 Correct 5 ms 7248 KB Output is correct
10 Correct 4 ms 7380 KB Output is correct
11 Correct 3 ms 7252 KB Output is correct
12 Correct 4 ms 7252 KB Output is correct
13 Incorrect 4 ms 7252 KB Output isn't correct
14 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7252 KB Output is correct
2 Correct 4 ms 7252 KB Output is correct
3 Correct 4 ms 7252 KB Output is correct
4 Correct 5 ms 7252 KB Output is correct
5 Correct 4 ms 7252 KB Output is correct
6 Correct 4 ms 7252 KB Output is correct
7 Correct 4 ms 7336 KB Output is correct
8 Correct 4 ms 7252 KB Output is correct
9 Correct 5 ms 7248 KB Output is correct
10 Correct 4 ms 7380 KB Output is correct
11 Correct 3 ms 7252 KB Output is correct
12 Correct 4 ms 7252 KB Output is correct
13 Incorrect 4 ms 7252 KB Output isn't correct
14 Halted 0 ms 0 KB -