Advertisement
Korotkodul

CF 25.09 B OK

Sep 23rd, 2022 (edited)
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.41 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4. #include <queue>
  5. #include <algorithm>
  6. #include <string>
  7. #include <stack>
  8. #include <set>
  9. #include <map>
  10. #define pii pair <int, int>
  11. #define pb(x) push_back(x)
  12. using namespace std;
  13. using ll = long long;
  14. using ld = long double;
  15. using db = double;
  16. void cv(vector <int> &v) {
  17.     for (auto x : v) cout << x << ' ';
  18.     cout << "\n";
  19. }
  20.  
  21. void cvl(vector <ll> &v) {
  22.     for (auto x : v) cout << x << ' ';
  23.     cout << "\n";
  24. }
  25.  
  26.  
  27. void cvv(vector <vector <int> > &v) {
  28.     for (auto x : v) cv(x);
  29.     cout << "\n";
  30. }
  31.  
  32. void cvb(vector <bool> v) {
  33.     for (bool x : v) cout << x << ' ';
  34.     cout << "\n";
  35. }
  36.  
  37. void cvs(vector <string>  v) {
  38.     for (auto a : v) {
  39.         cout << a << "\n";
  40.     }
  41. }
  42.  
  43. void cvp(vector <pii> a) {
  44.     for (auto p : a) {
  45.         cout << p.first << ' ' << p.second << "\n";
  46.     }
  47.     cout << "\n";
  48. }
  49.  
  50. bool sh = 0;
  51.  
  52. void slv() {
  53.     int n, A = 2e9, B = -2e9;
  54.     cin >> n;
  55.     vector <int> t(n), x(n);
  56.     for (int &i: x) cin>>i;
  57.     for (int &i: t) cin>>i;
  58.     vector <pii> a(n); //{crd, t}
  59.     for (int i = 0; i < n; ++i) {
  60.         a[i] = {x[i], t[i]};
  61.     }
  62.     sort(a.begin(), a.end());
  63.     vector <int> pf(n), repf(n);
  64.     //pf = max(ti - xi) (+x0);
  65.     //repf = max(tk + xk) - (x0)
  66.     pf[0] = - a[0].first + a[0].second;
  67.     repf[n - 1] = a[n - 1].first + a[n - 1].second;
  68.     for (int i = 1; i < n; ++i) {
  69.         pf[i] = max(pf[i - 1], - a[i].first + a[i].second);
  70.     }
  71.     for (int i = n - 2; i >= 0; --i) {
  72.         repf[i] = max(repf[i + 1], a[i].first + a[i].second);
  73.     }
  74.  
  75.     if (sh) {
  76.         cout << "a\n";
  77.         cvp(a);
  78.         cout << "pf\n";
  79.         cv(pf);
  80.         cout<<"repf\n";
  81.         cv(repf);
  82.         cout<<"GO x0 \n";
  83.     }
  84.  
  85.     //min time * 2
  86.     int x0 = 0;//индекс в векторе = i in vector
  87.     int opt = 2 * (repf[1] - a[0].first);
  88.     if (sh) {
  89.         cout << "first opt = " << opt << "\n";
  90.     }
  91.     if (sh) {
  92.         cout << "option end = " << 2 * (pf[n - 2] + a[n - 1].first) << "\n";
  93.     }
  94.     if (opt > 2 * (pf[n - 2] + a[n - 1].first) ) {
  95.         opt = 2 * (pf[n - 2] + a[n - 1].first);
  96.         x0 = n - 1;
  97.         if (sh) {
  98.             cout << "sec opt = " << opt << "\n";
  99.         }
  100.     }
  101.     //check crd[i] = x0
  102.     for (int i = 1; i < n - 1; ++i) {
  103.  
  104.         int now = max(2 * (pf[i - 1] + a[i].first), 2 * (repf[i + 1] - a[i].first) );
  105.         if (sh) {
  106.             cout << "i = " << i << "\n";
  107.             cout << "now = " << now << "\n";
  108.         }
  109.         if (now < opt) {
  110.             if (sh) {
  111.                 cout << "BETTER!\n";
  112.             }
  113.             opt = now;
  114.             x0 = i;
  115.         }
  116.     }
  117.     //теперь проверим
  118.  
  119.     if (sh) {
  120.         cout << "\nopt = " << opt << "\n";
  121.         cout << "GO x1\n";
  122.     }
  123.     int bestx1 = -1;
  124.     int x1 = -1; //принадлежит [i; i + 1] в векторе
  125.     for (int i = 0; i < n - 1; ++i) {
  126.         //now * 2 = :
  127.         int now = pf[i] + repf[i + 1];
  128.         //x1 * 2 = :
  129.         x1 =  -pf[i] + repf[i + 1] ;
  130.         if(sh) {
  131.             cout << "i = " << i << "\n";
  132.         }
  133.         if (sh) {
  134.             cout << "best x * 2 = " << x1 << "\n";
  135.             cout << "now = " << now << "\n";
  136.         }
  137.         if (x1 <= 2 * a[i].first || x1 >= 2 * a[i + 1].first) {
  138.             if (sh) {
  139.                 cout << "skip\n";
  140.             }
  141.             continue; //considered
  142.         }
  143.         if (sh) {
  144.             cout << "check\n";
  145.         }
  146.         if (now < opt) {
  147.             bestx1 = x1;
  148.             opt = now;
  149.             if (sh) {
  150.                 cout << "BETTER!\n";
  151.             }
  152.         }
  153.     }
  154.  
  155.     if (sh) {
  156.         cout << "x0 x1 = " << x0 << ' ' << x1 << "\n";
  157.     }
  158.  
  159.     x0 = a[x0].first;
  160.     if (bestx1 == -1) {//если x0 подошел
  161.         cout << x0 << "\n";
  162.         return;
  163.     }
  164.  
  165.     //x1 уже сконвертирован в координату - осталось только поделить на 2
  166.     if (bestx1 % 2 == 0) {
  167.         cout << bestx1 / 2 << "\n";
  168.         return;
  169.     }
  170.     cout << bestx1 / 2 << ".5\n";
  171. }
  172.  
  173. /*
  174. предпоследний и предпредпоследний
  175.  
  176. 6
  177. 5 4 7 2 10 4
  178. 3 2 5 1 4 6
  179. */
  180.  
  181. int main() {
  182.     ios::sync_with_stdio(0);
  183.     cin.tie(0);
  184.     cout.tie(0);
  185.     int t = 1;
  186.     if (!sh) cin >> t;
  187.     for (int go = 0; go < t; ++ go) {
  188.         slv();
  189.     }
  190. }
  191.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement