(200713) 코드포스

Codeforces materials usage license (v. 0.1) 에 따라 codeforces.com 의 문제임을 알립니다.

Original problem link: https://codeforces.com/problemset/problem/1380/D


Example

Input

5 2
5 2 3
3 1 4 5 2
3 5

Output

8

Input

4 4
5 1 4
4 3 1 2
2 4 3 1

Output

-1

Input

4 4
2 1 11
1 3 2 4
1 3 2 4

Output

0


Common codes for Codeforces
#include <algorithm>
#include <array>
#include <cmath>
#include <functional> 
#include <iostream>
#include <queue>
#include <string>
#include <type_traits>
#include <unordered_map>
#include <vector>

namespace predefined {

#ifdef CODEFORCES_DEBUG
  constexpr bool kIsDebug = true;
#else
  constexpr bool kIsDebug = false;
#endif

enum class LogType {
  INFO,
  DEBUG
};

template <LogType Type, bool... Bools>
struct Log;

template <>
struct Log<LogType::INFO> {
  Log() {}
  ~Log() { std::cout << '\n'; }

  template <typename T>
  Log& operator<< (const T &t) {
    std::cout << t;
    return *this;
  }
};

template <>
struct Log<LogType::DEBUG, true>{
  Log() {}
  ~Log() { std::cout << '\n'; }

  void Open() {
    freopen("input.txt", "r", stdin);
  }

  template <typename T>
  Log& operator<< (const T &t) {
    std::cout << t;
    return *this;
  }
};

template <>
struct Log<LogType::DEBUG, false> {
  Log() {}
  ~Log() {}

  void Open() {}

  template <typename T>
  Log& operator<< (const T &t) { return *this; }
};

namespace solver {

using SolveFunc = std::function<void()>;

template<typename MultiRunner>
void Run(SolveFunc solve_func);

template<>
void Run<std::true_type>(SolveFunc solve_func) {
  int test_case;
  std::cin >> test_case;

  while (test_case--) {
    solve_func();
  }
}

template<>
void Run<std::false_type>(SolveFunc solve_func) {
  solve_func();
}

} // solver

template <bool MultiTest>
struct Solver {
  constexpr static bool condition = MultiTest | kIsDebug;
  typedef typename std::conditional<condition, std::true_type, std::false_type>::type run_type;

  explicit Solver(solver::SolveFunc solve_func) {
    solver::Run<run_type>(solve_func);
  }
};

template<typename T>
struct BoolMap {
  explicit BoolMap(const T &_true_value, const T &_false_value) 
    : true_value(_true_value),
      false_value(_false_value) {}

  const T true_value;
  const T false_value;

  const T inline GetValue(const bool bool_key) const {
    return bool_key ? true_value : false_value;  
  };
};

}  // predefined;

#define LOG_INFO predefined::Log<predefined::LogType::INFO>()
#define LOG_DEBUG predefined::Log<predefined::LogType::DEBUG, predefined::kIsDebug>()
#define LOG(LEVEL) LOG_##LEVEL
#define LABEL(variable_name) #variable_name << ": " << variable_name << " "

#define INIT_ANSWER(type, true_value, false_value) \
  const predefined::BoolMap<type> predefined_bool_map(true_value, false_value)
#define INIT_STRING_ANSWER(true_string, false_string) \
  INIT_ANSWER(std::string, true_string, false_string)
#define GET_ANSWER(bool_key) \
  predefined_bool_map.GetValue(bool_key)

#define INIT_CODEFORCES() \
  std::ios::sync_with_stdio(false); \
  std::cin.tie(NULL); \
  LOG(DEBUG).Open(); \
1 Like

음 ㅜ
판단로직은 정답인데,
데이터를 가공하는데 있어서 강건하지 못하게 짰던거 같네요.

1380D
namespace {
using namespace std;

constexpr bool kMultiTestCase = false;
using Solver = predefined::Solver<kMultiTestCase>;

}  // unnamed namespace;

void solve() {

  int n, m;
  int64_t x, k, y;
  cin >> n >> m >> x >> k >> y;

  vector<int64_t> a_array(n);
  vector<int64_t> b_array(m);

  for (auto &input : a_array) cin >> input;
  for (auto &input : b_array) cin >> input;
  b_array.emplace_back(-1);
  
  vector<vector<int64_t>> subsequences;
  vector<pair<int64_t, int64_t>> start_end;

  int64_t ans = 0;

  int64_t start = -1;
  size_t b_index = 0;

  vector<int64_t> temp;
  for (const auto a : a_array) {
    if (a == b_array[b_index]) {
      if(!temp.empty()) {
        subsequences.emplace_back(std::move(temp));
        start_end.emplace_back(start, b_array[b_index]);
      }

      start = b_array[b_index];
      ++b_index;
      temp.clear();
      continue;
    }
    temp.push_back(a);
  }

  if(!temp.empty()) {
    subsequences.emplace_back(std::move(temp));
    start_end.emplace_back(start, -1);
  }

  if (b_index != m) {
    LOG_INFO << -1;
    return;
  }

  auto boundary_it = start_end.begin();
  bool useless_fireball = x > k * y;
  for (const auto &subsequence : subsequences) {
    int64_t max_value = *max_element(subsequence.begin(), subsequence.end());
    bool boundary_berserker = (boundary_it->first > max_value) || (boundary_it->second > max_value);

    if (boundary_berserker) {
      if (useless_fireball) {
        ans += y * subsequence.size();
      } else {
        ans += x * (subsequence.size() / k);
        ans += y * (subsequence.size() % k);
      }
    } else {
      if (subsequence.size() < k) {
        ans = -1;
        break;
      }

      if (useless_fireball) {
        ans += x * 1;
        ans += y * (subsequence.size() - k);
      } else {
        ans += x * (subsequence.size() / k);
        ans += y * (subsequence.size() % k);
      }
    }

    ++boundary_it;
  }

  LOG_INFO << ans;
}

int main(int argc, char** argv) {
  INIT_CODEFORCES();

  Solver(std::bind(&solve));

  return 0;
}