Advertisement
Galebickosikasa

Untitled

Jun 5th, 2021
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.28 KB | None | 0 0
  1. #include "test_runner.h"
  2.  
  3. #include <algorithm>
  4. #include <iostream>
  5. #include <optional>
  6. #include <string>
  7. #include <string_view>
  8. #include <vector>
  9. #include <ctime>
  10.  
  11. using namespace std;
  12.  
  13. class Date {
  14. private:
  15. class Year {
  16. public:
  17. Year (int year_) : year (year_) {}
  18.  
  19. int year;
  20.  
  21. bool is_leap () const {
  22. if (year % 400 == 0) return true;
  23. if (year % 100 == 0) return false;
  24. return year % 4 == 0;
  25. }
  26. };
  27.  
  28. class Month {
  29. private:
  30. Year& year;
  31. public:
  32. Month (Year& year_, int month_) : year (year_), month (month_) {}
  33.  
  34. int month;
  35.  
  36. int get_count_days () const {
  37. vector <bool> is31 {true, false, true, false, true, false, true, true, false, true, false, true};
  38. if (month == 2) {
  39. if (year.is_leap ()) return 29;
  40. return 28;
  41. } else {
  42. if (is31[static_cast<uint32_t>(month - 1)]) return 31;
  43. return 30;
  44. }
  45. }
  46. };
  47. Year year;
  48. Month month;
  49. int day;
  50. public:
  51. Date (int year_, int month_, int day_) : year (year_), month (year, month_), day (day_) {}
  52.  
  53. bool operator< (const Date& other) const {
  54. return tie (year.year, month.month, day) < tie (other.year.year, other.month.month, other.day);
  55. }
  56. void setDay (int day_) {
  57. day = day_;
  58. }
  59. int get_count_days () const {
  60. return month.get_count_days ();
  61. }
  62. };
  63.  
  64. class BudgetManager {
  65. public:
  66. BudgetManager () {
  67. dates.reserve (36525);
  68. for (int year = 2000; year <= 2099; ++year) {
  69. for (int month = 1; month <= 12; ++month) {
  70. Date date (year, month, 1);
  71. for (int day = 1; day <= date.get_count_days (); ++day) {
  72. date.setDay (day);
  73. dates.push_back (date);
  74. }
  75. }
  76. }
  77. cost = spending = vector <double> (dates.size ());
  78. }
  79.  
  80. double ComputeIncome (Date from, Date to) {
  81. int from_ = get_ind (from), to_ = get_ind (to);
  82. double sum = 0;
  83. for (int i = from_; i <= to_; ++i) sum += cost[i] - spending[i];
  84. return sum;
  85. }
  86.  
  87. void Earn (Date from, Date to, double value) {
  88. int from_ = get_ind (from), to_ = get_ind (to);
  89. double cnt = to_ - from_ + 1;
  90. value /= cnt;
  91. for (int i = from_; i <= to_; ++i) cost[i] += value;
  92. }
  93.  
  94. void Spend (Date from, Date to, double value) {
  95. int from_ = get_ind (from), to_ = get_ind (to);
  96. double cnt = to_ - from_ + 1;
  97. value /= cnt;
  98. for (int i = from_; i <= to_; ++i) spending[i] += value;
  99. }
  100.  
  101. void PayTax (Date from, Date to, double percentage) {
  102. int from_ = get_ind (from), to_ = get_ind (to);
  103. for (int i = from_; i <= to_; ++i) cost[i] *= 1.0 - percentage / 100;
  104. }
  105. private:
  106. vector <Date> dates;
  107. vector <double> cost, spending;
  108. int get_ind (Date date) const {
  109. return static_cast<int>(lower_bound(dates.begin (), dates.end (), date) - dates.begin ());
  110. }
  111. };
  112.  
  113. Date parse_date (istream& in = cin) {
  114. int year, month, day;
  115. char x;
  116. in >> year >> x >> month >> x >> day;
  117. return {year, month, day};
  118. }
  119.  
  120. int main() {
  121. ios_base::sync_with_stdio (false);
  122. cin.tie (nullptr);
  123. cout.precision (25);
  124. int q;
  125. cin >> q;
  126. BudgetManager bm;
  127. while (q--) {
  128. string t;
  129. cin >> t;
  130. if (t.front () == 'E') {
  131. Date from = parse_date ();
  132. Date to = parse_date ();
  133. int value;
  134. cin >> value;
  135. bm.Earn (from, to, value);
  136. } else if (t.front () == 'C') {
  137. Date from = parse_date ();
  138. Date to = parse_date ();
  139. cout << bm.ComputeIncome (from, to) << '\n';
  140. } else if (t.front () == 'S') {
  141. Date from = parse_date ();
  142. Date to = parse_date ();
  143. int value;
  144. cin >> value;
  145. bm.Spend (from, to, value);
  146. } else {
  147. Date from = parse_date ();
  148. Date to = parse_date ();
  149. int tax;
  150. cin >> tax;
  151. bm.PayTax (from, to, tax);
  152. }
  153. }
  154. }
  155.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement