Submission #655364

# Submission time Handle Problem Language Result Execution time Memory
655364 2022-11-04T03:01:45 Z SpaimaCarpatilor Fireworks (APIO16_fireworks) C++17
100 / 100
327 ms 104988 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;
        if (x <= *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 5 ms 7252 KB Output is correct
2 Correct 6 ms 7252 KB Output is correct
3 Correct 4 ms 7380 KB Output is correct
4 Correct 4 ms 7324 KB Output is correct
5 Correct 4 ms 7380 KB Output is correct
6 Correct 5 ms 7380 KB Output is correct
7 Correct 4 ms 7380 KB Output is correct
8 Correct 4 ms 7380 KB Output is correct
9 Correct 4 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 7380 KB Output is correct
4 Correct 4 ms 7400 KB Output is correct
5 Correct 4 ms 7360 KB Output is correct
6 Correct 5 ms 7380 KB Output is correct
7 Correct 4 ms 7380 KB Output is correct
8 Correct 4 ms 7364 KB Output is correct
9 Correct 5 ms 7372 KB Output is correct
10 Correct 5 ms 7380 KB Output is correct
11 Correct 4 ms 7380 KB Output is correct
12 Correct 5 ms 7364 KB Output is correct
13 Correct 4 ms 7380 KB Output is correct
14 Correct 5 ms 7364 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 7380 KB Output is correct
18 Correct 4 ms 7380 KB Output is correct
19 Correct 4 ms 7364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7252 KB Output is correct
2 Correct 6 ms 7252 KB Output is correct
3 Correct 4 ms 7380 KB Output is correct
4 Correct 4 ms 7324 KB Output is correct
5 Correct 4 ms 7380 KB Output is correct
6 Correct 5 ms 7380 KB Output is correct
7 Correct 4 ms 7380 KB Output is correct
8 Correct 4 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 7252 KB Output is correct
12 Correct 4 ms 7252 KB Output is correct
13 Correct 4 ms 7380 KB Output is correct
14 Correct 4 ms 7400 KB Output is correct
15 Correct 4 ms 7360 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 7364 KB Output is correct
19 Correct 5 ms 7372 KB Output is correct
20 Correct 5 ms 7380 KB Output is correct
21 Correct 4 ms 7380 KB Output is correct
22 Correct 5 ms 7364 KB Output is correct
23 Correct 4 ms 7380 KB Output is correct
24 Correct 5 ms 7364 KB Output is correct
25 Correct 4 ms 7380 KB Output is correct
26 Correct 4 ms 7380 KB Output is correct
27 Correct 4 ms 7380 KB Output is correct
28 Correct 4 ms 7380 KB Output is correct
29 Correct 4 ms 7364 KB Output is correct
30 Correct 5 ms 7280 KB Output is correct
31 Correct 6 ms 7380 KB Output is correct
32 Correct 5 ms 7380 KB Output is correct
33 Correct 7 ms 7528 KB Output is correct
34 Correct 6 ms 7508 KB Output is correct
35 Correct 6 ms 7636 KB Output is correct
36 Correct 6 ms 7628 KB Output is correct
37 Correct 7 ms 7636 KB Output is correct
38 Correct 9 ms 7636 KB Output is correct
39 Correct 7 ms 7636 KB Output is correct
40 Correct 7 ms 8908 KB Output is correct
41 Correct 6 ms 8916 KB Output is correct
42 Correct 7 ms 7636 KB Output is correct
43 Correct 9 ms 8276 KB Output is correct
44 Correct 8 ms 8088 KB Output is correct
45 Correct 7 ms 8108 KB Output is correct
46 Correct 7 ms 7704 KB Output is correct
47 Correct 7 ms 7636 KB Output is correct
48 Correct 7 ms 7636 KB Output is correct
49 Correct 7 ms 7636 KB Output is correct
50 Correct 6 ms 7684 KB Output is correct
51 Correct 6 ms 7704 KB Output is correct
52 Correct 8 ms 7628 KB Output is correct
53 Correct 7 ms 7748 KB Output is correct
54 Correct 7 ms 7760 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7252 KB Output is correct
2 Correct 6 ms 7252 KB Output is correct
3 Correct 4 ms 7380 KB Output is correct
4 Correct 4 ms 7324 KB Output is correct
5 Correct 4 ms 7380 KB Output is correct
6 Correct 5 ms 7380 KB Output is correct
7 Correct 4 ms 7380 KB Output is correct
8 Correct 4 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 7252 KB Output is correct
12 Correct 4 ms 7252 KB Output is correct
13 Correct 4 ms 7380 KB Output is correct
14 Correct 4 ms 7400 KB Output is correct
15 Correct 4 ms 7360 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 7364 KB Output is correct
19 Correct 5 ms 7372 KB Output is correct
20 Correct 5 ms 7380 KB Output is correct
21 Correct 4 ms 7380 KB Output is correct
22 Correct 5 ms 7364 KB Output is correct
23 Correct 4 ms 7380 KB Output is correct
24 Correct 5 ms 7364 KB Output is correct
25 Correct 4 ms 7380 KB Output is correct
26 Correct 4 ms 7380 KB Output is correct
27 Correct 4 ms 7380 KB Output is correct
28 Correct 4 ms 7380 KB Output is correct
29 Correct 4 ms 7364 KB Output is correct
30 Correct 5 ms 7280 KB Output is correct
31 Correct 6 ms 7380 KB Output is correct
32 Correct 5 ms 7380 KB Output is correct
33 Correct 7 ms 7528 KB Output is correct
34 Correct 6 ms 7508 KB Output is correct
35 Correct 6 ms 7636 KB Output is correct
36 Correct 6 ms 7628 KB Output is correct
37 Correct 7 ms 7636 KB Output is correct
38 Correct 9 ms 7636 KB Output is correct
39 Correct 7 ms 7636 KB Output is correct
40 Correct 7 ms 8908 KB Output is correct
41 Correct 6 ms 8916 KB Output is correct
42 Correct 7 ms 7636 KB Output is correct
43 Correct 9 ms 8276 KB Output is correct
44 Correct 8 ms 8088 KB Output is correct
45 Correct 7 ms 8108 KB Output is correct
46 Correct 7 ms 7704 KB Output is correct
47 Correct 7 ms 7636 KB Output is correct
48 Correct 7 ms 7636 KB Output is correct
49 Correct 7 ms 7636 KB Output is correct
50 Correct 6 ms 7684 KB Output is correct
51 Correct 6 ms 7704 KB Output is correct
52 Correct 8 ms 7628 KB Output is correct
53 Correct 7 ms 7748 KB Output is correct
54 Correct 7 ms 7760 KB Output is correct
55 Correct 12 ms 8276 KB Output is correct
56 Correct 39 ms 10568 KB Output is correct
57 Correct 76 ms 13024 KB Output is correct
58 Correct 88 ms 14752 KB Output is correct
59 Correct 131 ms 17364 KB Output is correct
60 Correct 162 ms 19404 KB Output is correct
61 Correct 204 ms 21388 KB Output is correct
62 Correct 208 ms 23344 KB Output is correct
63 Correct 258 ms 26132 KB Output is correct
64 Correct 327 ms 26384 KB Output is correct
65 Correct 174 ms 104924 KB Output is correct
66 Correct 168 ms 104988 KB Output is correct
67 Correct 167 ms 25324 KB Output is correct
68 Correct 244 ms 67220 KB Output is correct
69 Correct 265 ms 60716 KB Output is correct
70 Correct 240 ms 60568 KB Output is correct
71 Correct 269 ms 30956 KB Output is correct
72 Correct 259 ms 31176 KB Output is correct
73 Correct 254 ms 30464 KB Output is correct
74 Correct 257 ms 30500 KB Output is correct
75 Correct 249 ms 30412 KB Output is correct
76 Correct 294 ms 30264 KB Output is correct
77 Correct 246 ms 30212 KB Output is correct
78 Correct 251 ms 30960 KB Output is correct
79 Correct 294 ms 32000 KB Output is correct