fork download
  1. // you can use includes, for example:
  2. #include <algorithm>
  3. #include <iterator>
  4. #include <map>
  5. #include <string>
  6. #include <iostream>
  7.  
  8. using namespace std;
  9.  
  10. // you can write to stdout for debugging purposes, e.g.
  11. // cout << "this is a debug message" << endl;
  12.  
  13. class Duration
  14. {
  15. public:
  16. Duration(int hours, int minutes, int seconds) :
  17. seconds_ ((hours*60*60) + (minutes*60) + seconds)
  18. {
  19. }
  20.  
  21. template<typename It>
  22. static Duration fromLogLine(It pos) {
  23. int components[3] = {0};
  24.  
  25. for (auto i = 0; i < 3; i ++) {
  26. components[i] = std::stoi(std::string(pos, pos + 2));
  27. pos += 3;
  28. }
  29.  
  30. return Duration(components[0], components[1], components[2]);
  31. }
  32.  
  33. bool operator<(const Duration &other) const {
  34. return seconds_ < other.seconds_;
  35. }
  36.  
  37. auto seconds() const { return seconds_; }
  38.  
  39. private:
  40. int seconds_;
  41. };
  42.  
  43. class Number
  44. {
  45. public:
  46. Number(int number) :
  47. number_ (number)
  48. {
  49. }
  50.  
  51. template<typename It>
  52. static Number fromLogLine(It pos) {
  53. constexpr auto NUMBER_OFFSET = 9;
  54. constexpr auto PART_SIZE = 3;
  55. constexpr auto PART_OFFSET = 4;
  56. pos += NUMBER_OFFSET;
  57. int number = 0;
  58.  
  59. for (auto i = 0; i < 3; i ++) {
  60. number *= 1000;
  61. number += std::stoi(std::string(pos, pos+PART_SIZE));
  62. pos += PART_OFFSET;
  63. }
  64.  
  65. return Number(number);
  66. }
  67.  
  68. auto number() const { return number_; }
  69.  
  70. auto operator<(const Number &other) const {
  71. return number_ < other.number_;
  72. }
  73.  
  74. private:
  75. int number_;
  76. };
  77.  
  78. class LogEntry
  79. {
  80. public:
  81. LogEntry(Duration duration, Number number) :
  82. duration_ (duration),
  83. number_ (number)
  84. {}
  85.  
  86. template<typename It>
  87. static LogEntry fromStringRange(It begin, It end) {
  88.  
  89. return LogEntry(Duration::fromLogLine(begin), Number::fromLogLine(begin));
  90. }
  91.  
  92. auto& duration() const { return duration_; }
  93. auto& number() const { return number_; }
  94.  
  95. private:
  96. Duration duration_;
  97. Number number_;
  98. };
  99.  
  100. class CallLogParser
  101. {
  102. static const auto LINE_LENGTH = 21;
  103.  
  104. public:
  105. struct iterator {
  106. iterator(std::string::const_iterator iter) : position_(iter) {}
  107.  
  108. void operator++() {
  109. position_ += LINE_LENGTH;
  110. }
  111.  
  112. void operator++(int) {
  113. position_ += LINE_LENGTH;
  114. }
  115.  
  116. bool operator!=(iterator &other) {
  117. return position_ < other.position_;
  118. }
  119.  
  120. LogEntry operator*() {
  121. return LogEntry::fromStringRange(position_, position_ + LINE_LENGTH - 1);
  122. }
  123.  
  124. private:
  125. std::string::const_iterator position_;
  126. };
  127.  
  128. CallLogParser(const std::string &call_log) :
  129. call_log_ (call_log)
  130. {
  131. }
  132.  
  133. iterator begin() {
  134. return iterator(call_log_.begin());
  135. }
  136.  
  137. iterator end() {
  138. return iterator(call_log_.end());
  139. }
  140.  
  141. private:
  142. const std::string &call_log_;
  143. };
  144.  
  145. class NormalCallCost {
  146. public:
  147. NormalCallCost(LogEntry entry) :
  148. entry_ (entry)
  149. {
  150. }
  151.  
  152. auto cost() const {
  153. if (entry_.duration() < Duration(0, 5, 0)) {
  154. return entry_.duration().seconds() * 3;
  155. } else {
  156. return ((entry_.duration().seconds() / 60) + 1) * 150;
  157. }
  158. }
  159.  
  160. void debug_dump() {
  161. std::cout << "Number: " << entry_.number().number() << " seconds: " << entry_.duration().seconds() << " cost: " << cost() << std::endl;
  162. }
  163.  
  164. private:
  165. LogEntry entry_;
  166. };
  167.  
  168. int solution(string &S) {
  169. CallLogParser parser(S);
  170. std::map<Number, NormalCallCost> summary;
  171.  
  172. for(auto call : parser) {
  173. summary[call.number()] = NormalCallCost(call);
  174. }
  175.  
  176. return 0;
  177. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
In file included from /usr/include/c++/6/bits/stl_map.h:63:0,
                 from /usr/include/c++/6/map:61,
                 from prog.cpp:4:
/usr/include/c++/6/tuple: In instantiation of ‘std::pair<_T1, _T2>::pair(std::tuple<_Args1 ...>&, std::tuple<_Args2 ...>&, std::_Index_tuple<_Indexes1 ...>, std::_Index_tuple<_Indexes2 ...>) [with _Args1 = {const Number&}; long unsigned int ..._Indexes1 = {0ul}; _Args2 = {}; long unsigned int ..._Indexes2 = {}; _T1 = const Number; _T2 = NormalCallCost]’:
/usr/include/c++/6/tuple:1579:63:   required from ‘std::pair<_T1, _T2>::pair(std::piecewise_construct_t, std::tuple<_Args1 ...>, std::tuple<_Args2 ...>) [with _Args1 = {const Number&}; _Args2 = {}; _T1 = const Number; _T2 = NormalCallCost]’
/usr/include/c++/6/ext/new_allocator.h:120:4:   required from ‘void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = std::pair<const Number, NormalCallCost>; _Args = {const std::piecewise_construct_t&, std::tuple<const Number&>, std::tuple<>}; _Tp = std::_Rb_tree_node<std::pair<const Number, NormalCallCost> >]’
/usr/include/c++/6/bits/alloc_traits.h:455:4:   required from ‘static void std::allocator_traits<std::allocator<_Tp> >::construct(std::allocator_traits<std::allocator<_Tp> >::allocator_type&, _Up*, _Args&& ...) [with _Up = std::pair<const Number, NormalCallCost>; _Args = {const std::piecewise_construct_t&, std::tuple<const Number&>, std::tuple<>}; _Tp = std::_Rb_tree_node<std::pair<const Number, NormalCallCost> >; std::allocator_traits<std::allocator<_Tp> >::allocator_type = std::allocator<std::_Rb_tree_node<std::pair<const Number, NormalCallCost> > >]’
/usr/include/c++/6/bits/stl_tree.h:543:32:   required from ‘void std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_construct_node(std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Link_type, _Args&& ...) [with _Args = {const std::piecewise_construct_t&, std::tuple<const Number&>, std::tuple<>}; _Key = Number; _Val = std::pair<const Number, NormalCallCost>; _KeyOfValue = std::_Select1st<std::pair<const Number, NormalCallCost> >; _Compare = std::less<Number>; _Alloc = std::allocator<std::pair<const Number, NormalCallCost> >; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Link_type = std::_Rb_tree_node<std::pair<const Number, NormalCallCost> >*]’
/usr/include/c++/6/bits/stl_tree.h:560:4:   required from ‘std::_Rb_tree_node<_Val>* std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_create_node(_Args&& ...) [with _Args = {const std::piecewise_construct_t&, std::tuple<const Number&>, std::tuple<>}; _Key = Number; _Val = std::pair<const Number, NormalCallCost>; _KeyOfValue = std::_Select1st<std::pair<const Number, NormalCallCost> >; _Compare = std::less<Number>; _Alloc = std::allocator<std::pair<const Number, NormalCallCost> >; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Link_type = std::_Rb_tree_node<std::pair<const Number, NormalCallCost> >*]’
/usr/include/c++/6/bits/stl_tree.h:2196:64:   required from ‘std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_emplace_hint_unique(std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::const_iterator, _Args&& ...) [with _Args = {const std::piecewise_construct_t&, std::tuple<const Number&>, std::tuple<>}; _Key = Number; _Val = std::pair<const Number, NormalCallCost>; _KeyOfValue = std::_Select1st<std::pair<const Number, NormalCallCost> >; _Compare = std::less<Number>; _Alloc = std::allocator<std::pair<const Number, NormalCallCost> >; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator = std::_Rb_tree_iterator<std::pair<const Number, NormalCallCost> >; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::const_iterator = std::_Rb_tree_const_iterator<std::pair<const Number, NormalCallCost> >]’
/usr/include/c++/6/bits/stl_map.h:483:8:   required from ‘std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const key_type&) [with _Key = Number; _Tp = NormalCallCost; _Compare = std::less<Number>; _Alloc = std::allocator<std::pair<const Number, NormalCallCost> >; std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type = NormalCallCost; std::map<_Key, _Tp, _Compare, _Alloc>::key_type = Number]’
prog.cpp:173:30:   required from here
/usr/include/c++/6/tuple:1590:70: error: no matching function for call to ‘NormalCallCost::NormalCallCost()’
         second(std::forward<_Args2>(std::get<_Indexes2>(__tuple2))...)
                                                                      ^
prog.cpp:147:5: note: candidate: NormalCallCost::NormalCallCost(LogEntry)
     NormalCallCost(LogEntry entry) :
     ^~~~~~~~~~~~~~
prog.cpp:147:5: note:   candidate expects 1 argument, 0 provided
prog.cpp:145:7: note: candidate: constexpr NormalCallCost::NormalCallCost(const NormalCallCost&)
 class NormalCallCost {
       ^~~~~~~~~~~~~~
prog.cpp:145:7: note:   candidate expects 1 argument, 0 provided
prog.cpp:145:7: note: candidate: constexpr NormalCallCost::NormalCallCost(NormalCallCost&&)
prog.cpp:145:7: note:   candidate expects 1 argument, 0 provided
stdout
Standard output is empty