Advertisement
alexpak

8 - Траектория обучения

Apr 14th, 2017
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.52 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define DB(v) cerr << #v << ' ' << v << endl
  4. #define sz(v) i64(v.size())
  5. #define For(i, a, b) for(i64 i = a;i <= b; ++i)
  6.  
  7. typedef long long i64;
  8.  
  9. const i64 N = 1e6 + 9;
  10.  
  11. template <typename T>
  12. inline ostream &operator << (ostream &out, const vector <T> &v) {
  13.     for(auto to: v)
  14.         out << to << ' ';
  15.     out << '\n';
  16.     return out;
  17. }
  18.  
  19. template <typename T>
  20. inline ostream &operator << (ostream &out, const set <T> &v) {
  21.     for(auto to: v)
  22.         out << to << ' ';
  23.     out << '\n';
  24.     return out;
  25. }
  26.  
  27.  
  28. i64 n, m;
  29.  
  30. vector <i64> lesson_one, lesson_two, cost_one, cost_two;
  31. i64 pos [N];
  32.  
  33. vector <i64> pref_sum;
  34.  
  35. inline i64 get_sum(i64 l, i64 r) {
  36.     if(l > r)
  37.         return 0;
  38.     if(l == 0)
  39.         return pref_sum[r];
  40.     return pref_sum[r] - pref_sum[l - 1];
  41. }
  42.  
  43. struct segment{
  44.     i64 l, r;
  45.  
  46.     segment (i64 a = 0, i64 b = 0) {
  47.         l = a;
  48.         r = b;
  49.     }
  50.  
  51.     bool operator < (const segment &other) const {
  52.         return get_sum(l, r) < get_sum(other.l, other.r);
  53.     }
  54. };
  55.  
  56. struct answer {
  57.     i64 l_one, r_one, l_two, r_two;
  58.     i64 all_sum;
  59.     answer (i64 a = 0, i64 b = 0, i64 c = 0, i64 d = 0, i64 e = 0) {
  60.         tie(l_one, r_one, l_two, r_two, all_sum) = make_tuple(a, b, c, d, e);
  61.     }
  62.  
  63.     bool operator < (const answer &other) const {
  64.         return all_sum < other.all_sum;
  65.     }
  66.  
  67.     void print() {
  68.         cout << all_sum << '\n';
  69.         cout << l_one << ' ' << r_one << '\n';
  70.         cout << l_two << ' ' << r_two << '\n';
  71.     }
  72. };
  73.  
  74. int main()
  75. {
  76. #ifdef HOME
  77.     freopen("input.txt","r",stdin);
  78. #endif // HOME
  79.     ios::sync_with_stdio(NULL); cin.tie(NULL);
  80.     cin >> n >> m;
  81.     memset(pos, -1, sizeof(pos));
  82.  
  83.     lesson_one.resize(n + 1);
  84.     lesson_two.resize(m + 1);
  85.     cost_one.resize(n + 1);
  86.     cost_two.resize(m + 1);
  87.     pref_sum.resize(m + 1);
  88.  
  89.  
  90.     For(i, 1, n) {
  91.         cin >> lesson_one[i];
  92.     }
  93.     For(i, 1, n) {
  94.         cin >> cost_one[i];
  95.     }
  96.     For(i, 1, m) {
  97.         cin >> lesson_two[i];
  98.         pos[lesson_two[i]] = i;
  99.     }
  100.     For(i, 1, m) {
  101.         cin >> cost_two[i];
  102.         pref_sum[i] = pref_sum[i - 1] + cost_two[i];
  103.     }
  104.  
  105.     answer result = answer(0, 0, 1, m, get_sum(1, m));
  106.  
  107.     set <i64> erased;
  108.     set <segment> possible;
  109.  
  110.     for(i64 l = 1;l <= n; ++l) {
  111.         possible.clear();
  112.         erased.clear();
  113.         erased.insert(0);
  114.         erased.insert(m + 1);
  115.         possible.insert(segment(1, m));
  116.  
  117.         i64 cur = 0;
  118.  
  119.         for(i64 r = l; r <= n; ++r) {
  120.             cur += cost_one[r];
  121.             i64 cur_pos = pos[lesson_one[r]];
  122.             if(cur_pos != -1) {
  123.                 i64 l_pos = *(--erased.lower_bound(cur_pos)), r_pos = *erased.upper_bound(cur_pos);
  124.                 possible.erase(segment(l_pos + 1, r_pos - 1));
  125.                 if(l_pos + 1 <= cur_pos - 1)
  126.                     possible.insert(segment(l_pos + 1, cur_pos - 1));
  127.                 if(cur_pos + 1 <= r_pos - 1)
  128.                     possible.insert(segment(cur_pos + 1, r_pos - 1));
  129.                 erased.insert(cur_pos);
  130.             }
  131.             segment big = segment(0, 0);
  132.             if(!possible.empty())
  133.                 big = (*(--possible.end()));
  134.             if(get_sum(big.l, big.r) == 0)
  135.                 big = segment(0, 0);
  136.             if(cur + get_sum(big.l, big.r) > result.all_sum) {
  137.                 result = answer(l, r, big.l, big.r, cur + get_sum(big.l, big.r));
  138.             }
  139.         }
  140.     }
  141.  
  142.     result.print();
  143.     return 0;
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement