답안 #262667

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
262667 2020-08-13T06:44:05 Z KoD Fireworks (APIO16_fireworks) C++11
26 / 100
32 ms 8832 KB
/**
 * @title Template
 */

#include <iostream>
#include <algorithm>
#include <utility>
#include <numeric>
#include <vector>
#include <array>
#include <cassert>
#include <iomanip>


template <class T, class U>
bool chmin(T &lhs, const U &rhs) {
  if (lhs > rhs) { 
    lhs = rhs; 
    return true; 
  }
  return false;
}

template <class T, class U>
bool chmax(T &lhs, const U &rhs) {
  if (lhs < rhs) { 
    lhs = rhs; 
    return true; 
  }
  return false;
}

/**
 * @title Chmin/Chmax
 */


class range {
public:
  class iterator {
  private:
    int64_t M_position;

  public:
    iterator(int64_t position) noexcept: M_position(position) { }
    void operator ++ () noexcept { ++M_position; }
    bool operator != (iterator other) const noexcept { return M_position != other.M_position; }
    int64_t operator * () const noexcept { return M_position; }

  };

  class reverse_iterator {
  private:
    int64_t M_position;
  
  public:
    reverse_iterator(int64_t position) noexcept: M_position(position) { }
    void operator ++ () noexcept { --M_position; }
    bool operator != (reverse_iterator other) const noexcept { return M_position != other.M_position; }
    int64_t operator * () const noexcept { return M_position; }

  };
  
private:
  const iterator M_first, M_last;

public:
  range(int64_t first, int64_t last) noexcept: M_first(first), M_last(std::max(first, last)) { }
  iterator begin() const noexcept { return M_first; }
  iterator end() const noexcept { return M_last; }
  reverse_iterator rbegin() const noexcept { return reverse_iterator(*M_last - 1); } 
  reverse_iterator rend() const noexcept { return reverse_iterator(*M_first - 1); } 

};

/**
 * @title Range
 */

#include <type_traits>
#include <iterator>

template <class T>
class rev_impl {
public:
  using iterator = decltype(std::declval<T>().rbegin());

private:
  const iterator M_begin;
  const iterator M_end;

public:
  rev_impl(T &&cont) noexcept: M_begin(cont.rbegin()), M_end(cont.rend()) { }
  iterator begin() const noexcept { return M_begin; }
  iterator end() const noexcept { return M_end; }

};

template <class T>
rev_impl<T> rev(T &&cont) {
  return rev_impl<T>(std::forward<T>(cont));
}

/**
 * @title Reverser
 */

using i32 = int32_t;
using i64 = int64_t;
using u32 = uint32_t;
using u64 = uint64_t;

constexpr i32 inf32 = (i32(1) << 30) - 1;
constexpr i64 inf64 = (i64(1) << 62) - 1;

i32 N, M, V;
std::array<i32, 300000> P, C;

struct edge_t {
  const i32 to;
  const i32 cost;
  edge_t(i32 to_, i32 cost_): to(to_), cost(cost_) { }
};

std::array<std::vector<edge_t>, 300000> graph;
std::array<std::array<i64, 301>, 300> dp;

void dfs(i32 u) {
  if (u >= N) {
    dp[u].fill(inf64);
    dp[u][0] = 0;
    return;
  }
  for (auto p: graph[u]) {
    const i32 v = p.to;
    if (v != P[u]) {
      dfs(v);
      std::array<i64, 301> next;
      next.fill(inf64);
      for (auto j: range(0, 301)) {
        for (auto k: range(0, 301 - j)) {
          chmin(next[j + k], dp[v][j] + std::abs(p.cost - k));
        }
      }
      for (auto j: range(0, 301)) {
        dp[u][j] += next[j];
      }
    }
  }
}

int main() {
  std::cin >> N >> M;
  V = N + M;
  --P[0];
  for (auto i: range(1, V)) {
    std::cin >> P[i] >> C[i];
    --P[i];
    graph[P[i]].emplace_back(i, C[i]);
    graph[i].emplace_back(P[i], C[i]);
  }
  if (N == 1) {
    std::vector<i32> vec;
    vec.reserve(M);
    for (auto i: range(N, N + M)) {
      vec.push_back(C[i]);
    }
    std::sort(vec.begin(), vec.end());
    i32 goal = vec[vec.size() / 2];
    i64 ans = 0;
    for (auto x: vec) {
      ans += std::abs(goal - x);
    }
    std::cout << ans << '\n';
  }
  else if (V <= 300) {
    dfs(0);
    i64 ans = inf64;
    for (auto j: range(0, 301)) {
      chmin(ans, dp[0][j]);
    }
    std::cout << ans << '\n';
  }
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 7424 KB Output is correct
2 Correct 6 ms 7424 KB Output is correct
3 Correct 6 ms 7424 KB Output is correct
4 Correct 6 ms 7552 KB Output is correct
5 Correct 6 ms 7424 KB Output is correct
6 Correct 7 ms 7424 KB Output is correct
7 Correct 5 ms 7424 KB Output is correct
8 Correct 6 ms 7424 KB Output is correct
9 Correct 6 ms 7424 KB Output is correct
10 Correct 6 ms 7424 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 7424 KB Output is correct
2 Correct 10 ms 7424 KB Output is correct
3 Correct 10 ms 7552 KB Output is correct
4 Correct 16 ms 7808 KB Output is correct
5 Correct 14 ms 7680 KB Output is correct
6 Correct 20 ms 7680 KB Output is correct
7 Correct 17 ms 7808 KB Output is correct
8 Correct 19 ms 7808 KB Output is correct
9 Correct 20 ms 7808 KB Output is correct
10 Correct 21 ms 7936 KB Output is correct
11 Correct 23 ms 7936 KB Output is correct
12 Correct 25 ms 8064 KB Output is correct
13 Correct 25 ms 8064 KB Output is correct
14 Correct 26 ms 8832 KB Output is correct
15 Correct 32 ms 8064 KB Output is correct
16 Correct 28 ms 8064 KB Output is correct
17 Correct 31 ms 8064 KB Output is correct
18 Correct 28 ms 8064 KB Output is correct
19 Correct 27 ms 8064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 7424 KB Output is correct
2 Correct 6 ms 7424 KB Output is correct
3 Correct 6 ms 7424 KB Output is correct
4 Correct 6 ms 7552 KB Output is correct
5 Correct 6 ms 7424 KB Output is correct
6 Correct 7 ms 7424 KB Output is correct
7 Correct 5 ms 7424 KB Output is correct
8 Correct 6 ms 7424 KB Output is correct
9 Correct 6 ms 7424 KB Output is correct
10 Correct 6 ms 7424 KB Output is correct
11 Correct 6 ms 7424 KB Output is correct
12 Correct 10 ms 7424 KB Output is correct
13 Correct 10 ms 7552 KB Output is correct
14 Correct 16 ms 7808 KB Output is correct
15 Correct 14 ms 7680 KB Output is correct
16 Correct 20 ms 7680 KB Output is correct
17 Correct 17 ms 7808 KB Output is correct
18 Correct 19 ms 7808 KB Output is correct
19 Correct 20 ms 7808 KB Output is correct
20 Correct 21 ms 7936 KB Output is correct
21 Correct 23 ms 7936 KB Output is correct
22 Correct 25 ms 8064 KB Output is correct
23 Correct 25 ms 8064 KB Output is correct
24 Correct 26 ms 8832 KB Output is correct
25 Correct 32 ms 8064 KB Output is correct
26 Correct 28 ms 8064 KB Output is correct
27 Correct 31 ms 8064 KB Output is correct
28 Correct 28 ms 8064 KB Output is correct
29 Correct 27 ms 8064 KB Output is correct
30 Incorrect 6 ms 7424 KB Output isn't correct
31 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 7424 KB Output is correct
2 Correct 6 ms 7424 KB Output is correct
3 Correct 6 ms 7424 KB Output is correct
4 Correct 6 ms 7552 KB Output is correct
5 Correct 6 ms 7424 KB Output is correct
6 Correct 7 ms 7424 KB Output is correct
7 Correct 5 ms 7424 KB Output is correct
8 Correct 6 ms 7424 KB Output is correct
9 Correct 6 ms 7424 KB Output is correct
10 Correct 6 ms 7424 KB Output is correct
11 Correct 6 ms 7424 KB Output is correct
12 Correct 10 ms 7424 KB Output is correct
13 Correct 10 ms 7552 KB Output is correct
14 Correct 16 ms 7808 KB Output is correct
15 Correct 14 ms 7680 KB Output is correct
16 Correct 20 ms 7680 KB Output is correct
17 Correct 17 ms 7808 KB Output is correct
18 Correct 19 ms 7808 KB Output is correct
19 Correct 20 ms 7808 KB Output is correct
20 Correct 21 ms 7936 KB Output is correct
21 Correct 23 ms 7936 KB Output is correct
22 Correct 25 ms 8064 KB Output is correct
23 Correct 25 ms 8064 KB Output is correct
24 Correct 26 ms 8832 KB Output is correct
25 Correct 32 ms 8064 KB Output is correct
26 Correct 28 ms 8064 KB Output is correct
27 Correct 31 ms 8064 KB Output is correct
28 Correct 28 ms 8064 KB Output is correct
29 Correct 27 ms 8064 KB Output is correct
30 Incorrect 6 ms 7424 KB Output isn't correct
31 Halted 0 ms 0 KB -