Submission #655365

# Submission time Handle Problem Language Result Execution time Memory
655365 2022-11-04T03:07:56 Z SpaimaCarpatilor Fireworks (APIO16_fireworks) C++17
100 / 100
293 ms 100164 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];
const bool DBG = 0;

namespace brute {
    const int maxK = 100;
    long long bruteDp[maxN][100];

    int mod(int x) { return (x < 0 ? -x : x); }

    void doBrute() {
        for (int i = N; i >= 1; i--) {
            if (v[i].empty()) {
                for (int j = 0; j < maxK; j++)
                    bruteDp[i][j] = j;
            } else {
                for (int j = 0; j < maxK; j++) {
                    for (auto it: v[i]) {
                        long long best = 0;
                        for (int k = 0; k <= j; k++) {
                            long long curr = bruteDp[it][k] + mod((j - k) - d[it]);
                            if (curr < best || k == 0)
                                best = curr;
                        }
                        bruteDp[i][j] += best;
                    }
                    if (j > 0)
                        bruteDp[i][j] = min(bruteDp[i][j], bruteDp[i][j - 1] + 1);
                }
            }
        }
        long long ans = bruteDp[1][0];
        for (int i = 1; i < maxK; i++)
            ans = min(ans, bruteDp[1][i]);
        printf("brute: %lld\n", ans);
    }
};

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

    void printVals(int nodeToCompare = -1) {
        if (S.empty()) {
            printf("flat at [%lld, %lld]\n", platformStart(), platformStart() + platformLength);
            return ;
        }
        vector<long long> f(max<int>(platformStart() + platformLength + 10, brute::maxK), 0LL);
        auto it = S.end(); it --;
        int slope = 0;
        long long curr = smallest;
        for (int i=platformStart() + platformLength; i>=0; i--) {
            while (it != S.begin() && *it >= i)
                slope ++, it --;
            f[i] = curr, curr += slope + (*it >= i);
        }
        curr = smallest;
        for (int i=platformStart() + platformLength + 1; i<f.size(); i++)
            curr ++, f[i] = curr;
        if (nodeToCompare != -1) {
            bool areSame = 1;
            for (int i = 0; i < f.size(); i++)
                areSame &= (f[i] == brute::bruteDp[nodeToCompare][i]);
            if (!areSame) {
                printf("WA %d:\n", nodeToCompare);
                for (int i = 0; i < f.size(); i++)
                    printf("%2d ", i);
                printf("\n");
                for (auto it: f)
                    printf("%2lld ", it);
                printf("\ninstead of \n");
                for (int i = 0; i < f.size(); i++)
                    printf("%2lld ", brute::bruteDp[nodeToCompare][i]);
                exit(0);
            }
        } else {
            for (auto it : f)
                printf("%2d ", it);
            printf("\n");
        }
    }
}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);
        }
    }
    if (DBG) {
        printf("rightEnds:[");
        for (auto it: rightEnds)
            printf("%lld ", it);
        printf("\b]\n");
        printf("before rightEnds: "), dp[lev].print(), printf("\n");
        dp[lev].printVals();
    }
    sort(rightEnds.rbegin(), rightEnds.rend());
    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);
    }
    if (DBG) {
        printf("node %d: "), dp[lev].print(), printf("\n");
        dp[lev].printVals(nod);
    }
}

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);
    }
    if (DBG) brute::doBrute();
    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 member function 'void dpFunction::printVals(int)':
fireworks.cpp:129:59: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  129 |         for (int i=platformStart() + platformLength + 1; i<f.size(); i++)
      |                                                          ~^~~~~~~~~
fireworks.cpp:133:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  133 |             for (int i = 0; i < f.size(); i++)
      |                             ~~^~~~~~~~~~
fireworks.cpp:137:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  137 |                 for (int i = 0; i < f.size(); i++)
      |                                 ~~^~~~~~~~~~
fireworks.cpp:143:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  143 |                 for (int i = 0; i < f.size(); i++)
      |                                 ~~^~~~~~~~~~
fireworks.cpp:149:27: warning: format '%d' expects argument of type 'int', but argument 2 has type 'long long int' [-Wformat=]
  149 |                 printf("%2d ", it);
      |                         ~~^    ~~
      |                           |    |
      |                           int  long long int
      |                         %2lld
fireworks.cpp: In function 'void solve(int, int)':
fireworks.cpp:193:23: warning: format '%d' expects a matching 'int' argument [-Wformat=]
  193 |         printf("node %d: "), dp[lev].print(), printf("\n");
      |                      ~^
      |                       |
      |                       int
fireworks.cpp: In function 'int main()':
fireworks.cpp:202:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  202 |     scanf("%d %d", &N, &M), N += M;
      |     ~~~~~^~~~~~~~~~~~~~~~~
fireworks.cpp:204:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  204 |         scanf("%d %d", &t[i], &d[i]);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7252 KB Output is correct
2 Correct 3 ms 7380 KB Output is correct
3 Correct 3 ms 7380 KB Output is correct
4 Correct 4 ms 7296 KB Output is correct
5 Correct 4 ms 7380 KB Output is correct
6 Correct 4 ms 7380 KB Output is correct
7 Correct 5 ms 7380 KB Output is correct
8 Correct 3 ms 7380 KB Output is correct
9 Correct 3 ms 7380 KB Output is correct
10 Correct 4 ms 7380 KB Output is correct
# Verdict Execution time Memory 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 7400 KB Output is correct
5 Correct 4 ms 7380 KB Output is correct
6 Correct 4 ms 7380 KB Output is correct
7 Correct 4 ms 7388 KB Output is correct
8 Correct 3 ms 7380 KB Output is correct
9 Correct 4 ms 7380 KB Output is correct
10 Correct 4 ms 7380 KB Output is correct
11 Correct 4 ms 7380 KB Output is correct
12 Correct 4 ms 7380 KB Output is correct
13 Correct 4 ms 7372 KB Output is correct
14 Correct 5 ms 7380 KB Output is correct
15 Correct 4 ms 7280 KB Output is correct
16 Correct 5 ms 7380 KB Output is correct
17 Correct 4 ms 7380 KB Output is correct
18 Correct 4 ms 7324 KB Output is correct
19 Correct 4 ms 7380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7252 KB Output is correct
2 Correct 3 ms 7380 KB Output is correct
3 Correct 3 ms 7380 KB Output is correct
4 Correct 4 ms 7296 KB Output is correct
5 Correct 4 ms 7380 KB Output is correct
6 Correct 4 ms 7380 KB Output is correct
7 Correct 5 ms 7380 KB Output is correct
8 Correct 3 ms 7380 KB Output is correct
9 Correct 3 ms 7380 KB Output is correct
10 Correct 4 ms 7380 KB Output is correct
11 Correct 4 ms 7252 KB Output is correct
12 Correct 4 ms 7252 KB Output is correct
13 Correct 4 ms 7252 KB Output is correct
14 Correct 5 ms 7400 KB Output is correct
15 Correct 4 ms 7380 KB Output is correct
16 Correct 4 ms 7380 KB Output is correct
17 Correct 4 ms 7388 KB Output is correct
18 Correct 3 ms 7380 KB Output is correct
19 Correct 4 ms 7380 KB Output is correct
20 Correct 4 ms 7380 KB Output is correct
21 Correct 4 ms 7380 KB Output is correct
22 Correct 4 ms 7380 KB Output is correct
23 Correct 4 ms 7372 KB Output is correct
24 Correct 5 ms 7380 KB Output is correct
25 Correct 4 ms 7280 KB Output is correct
26 Correct 5 ms 7380 KB Output is correct
27 Correct 4 ms 7380 KB Output is correct
28 Correct 4 ms 7324 KB Output is correct
29 Correct 4 ms 7380 KB Output is correct
30 Correct 4 ms 7380 KB Output is correct
31 Correct 5 ms 7380 KB Output is correct
32 Correct 6 ms 7380 KB Output is correct
33 Correct 5 ms 7508 KB Output is correct
34 Correct 7 ms 7508 KB Output is correct
35 Correct 6 ms 7508 KB Output is correct
36 Correct 6 ms 7508 KB Output is correct
37 Correct 6 ms 7636 KB Output is correct
38 Correct 7 ms 7636 KB Output is correct
39 Correct 6 ms 7636 KB Output is correct
40 Correct 6 ms 8904 KB Output is correct
41 Correct 6 ms 8816 KB Output is correct
42 Correct 7 ms 7508 KB Output is correct
43 Correct 7 ms 8208 KB Output is correct
44 Correct 6 ms 8020 KB Output is correct
45 Correct 7 ms 8020 KB Output is correct
46 Correct 7 ms 7596 KB Output is correct
47 Correct 7 ms 7636 KB Output is correct
48 Correct 6 ms 7636 KB Output is correct
49 Correct 6 ms 7636 KB Output is correct
50 Correct 6 ms 7636 KB Output is correct
51 Correct 6 ms 7636 KB Output is correct
52 Correct 6 ms 7692 KB Output is correct
53 Correct 7 ms 7636 KB Output is correct
54 Correct 7 ms 7636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7252 KB Output is correct
2 Correct 3 ms 7380 KB Output is correct
3 Correct 3 ms 7380 KB Output is correct
4 Correct 4 ms 7296 KB Output is correct
5 Correct 4 ms 7380 KB Output is correct
6 Correct 4 ms 7380 KB Output is correct
7 Correct 5 ms 7380 KB Output is correct
8 Correct 3 ms 7380 KB Output is correct
9 Correct 3 ms 7380 KB Output is correct
10 Correct 4 ms 7380 KB Output is correct
11 Correct 4 ms 7252 KB Output is correct
12 Correct 4 ms 7252 KB Output is correct
13 Correct 4 ms 7252 KB Output is correct
14 Correct 5 ms 7400 KB Output is correct
15 Correct 4 ms 7380 KB Output is correct
16 Correct 4 ms 7380 KB Output is correct
17 Correct 4 ms 7388 KB Output is correct
18 Correct 3 ms 7380 KB Output is correct
19 Correct 4 ms 7380 KB Output is correct
20 Correct 4 ms 7380 KB Output is correct
21 Correct 4 ms 7380 KB Output is correct
22 Correct 4 ms 7380 KB Output is correct
23 Correct 4 ms 7372 KB Output is correct
24 Correct 5 ms 7380 KB Output is correct
25 Correct 4 ms 7280 KB Output is correct
26 Correct 5 ms 7380 KB Output is correct
27 Correct 4 ms 7380 KB Output is correct
28 Correct 4 ms 7324 KB Output is correct
29 Correct 4 ms 7380 KB Output is correct
30 Correct 4 ms 7380 KB Output is correct
31 Correct 5 ms 7380 KB Output is correct
32 Correct 6 ms 7380 KB Output is correct
33 Correct 5 ms 7508 KB Output is correct
34 Correct 7 ms 7508 KB Output is correct
35 Correct 6 ms 7508 KB Output is correct
36 Correct 6 ms 7508 KB Output is correct
37 Correct 6 ms 7636 KB Output is correct
38 Correct 7 ms 7636 KB Output is correct
39 Correct 6 ms 7636 KB Output is correct
40 Correct 6 ms 8904 KB Output is correct
41 Correct 6 ms 8816 KB Output is correct
42 Correct 7 ms 7508 KB Output is correct
43 Correct 7 ms 8208 KB Output is correct
44 Correct 6 ms 8020 KB Output is correct
45 Correct 7 ms 8020 KB Output is correct
46 Correct 7 ms 7596 KB Output is correct
47 Correct 7 ms 7636 KB Output is correct
48 Correct 6 ms 7636 KB Output is correct
49 Correct 6 ms 7636 KB Output is correct
50 Correct 6 ms 7636 KB Output is correct
51 Correct 6 ms 7636 KB Output is correct
52 Correct 6 ms 7692 KB Output is correct
53 Correct 7 ms 7636 KB Output is correct
54 Correct 7 ms 7636 KB Output is correct
55 Correct 11 ms 8008 KB Output is correct
56 Correct 37 ms 9928 KB Output is correct
57 Correct 61 ms 11624 KB Output is correct
58 Correct 87 ms 13136 KB Output is correct
59 Correct 117 ms 15212 KB Output is correct
60 Correct 153 ms 16692 KB Output is correct
61 Correct 181 ms 18328 KB Output is correct
62 Correct 203 ms 20008 KB Output is correct
63 Correct 247 ms 21848 KB Output is correct
64 Correct 265 ms 21924 KB Output is correct
65 Correct 165 ms 100164 KB Output is correct
66 Correct 162 ms 100140 KB Output is correct
67 Correct 164 ms 20396 KB Output is correct
68 Correct 191 ms 62484 KB Output is correct
69 Correct 227 ms 55864 KB Output is correct
70 Correct 224 ms 55904 KB Output is correct
71 Correct 242 ms 26640 KB Output is correct
72 Correct 249 ms 26780 KB Output is correct
73 Correct 242 ms 26440 KB Output is correct
74 Correct 242 ms 26500 KB Output is correct
75 Correct 257 ms 26444 KB Output is correct
76 Correct 245 ms 26440 KB Output is correct
77 Correct 253 ms 26104 KB Output is correct
78 Correct 243 ms 26976 KB Output is correct
79 Correct 293 ms 28432 KB Output is correct