Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <vector>
- #include <queue>
- #include <algorithm>
- #include <string>
- #include <stack>
- #include <set>
- #include <map>
- #define pii pair <int, int>
- #define pb(x) push_back(x)
- using namespace std;
- using ll = long long;
- using ld = long double;
- using db = double;
- void cv(vector <int> &v) {
- for (auto x : v) cout << x << ' ';
- cout << "\n";
- }
- void cvl(vector <ll> &v) {
- for (auto x : v) cout << x << ' ';
- cout << "\n";
- }
- void cvv(vector <vector <int> > &v) {
- for (auto x : v) cv(x);
- cout << "\n";
- }
- void cvb(vector <bool> v) {
- for (bool x : v) cout << x << ' ';
- cout << "\n";
- }
- void cvs(vector <string> v) {
- for (auto a : v) {
- cout << a << "\n";
- }
- }
- void cvp(vector <pii> a) {
- for (auto p : a) {
- cout << p.first << ' ' << p.second << "\n";
- }
- cout << "\n";
- }
- bool sh = 0;
- void slv() {
- int n, A = 2e9, B = -2e9;
- cin >> n;
- vector <int> t(n), x(n);
- for (int &i: x) cin>>i;
- for (int &i: t) cin>>i;
- vector <pii> a(n); //{crd, t}
- for (int i = 0; i < n; ++i) {
- a[i] = {x[i], t[i]};
- }
- sort(a.begin(), a.end());
- vector <int> pf(n), repf(n);
- //pf = max(ti - xi) (+x0);
- //repf = max(tk + xk) - (x0)
- pf[0] = - a[0].first + a[0].second;
- repf[n - 1] = a[n - 1].first + a[n - 1].second;
- for (int i = 1; i < n; ++i) {
- pf[i] = max(pf[i - 1], - a[i].first + a[i].second);
- }
- for (int i = n - 2; i >= 0; --i) {
- repf[i] = max(repf[i + 1], a[i].first + a[i].second);
- }
- if (sh) {
- cout << "a\n";
- cvp(a);
- cout << "pf\n";
- cv(pf);
- cout<<"repf\n";
- cv(repf);
- cout<<"GO x0 \n";
- }
- //min time * 2
- int x0 = 0;//индекс в векторе = i in vector
- int opt = 2 * (repf[1] - a[0].first);
- if (sh) {
- cout << "first opt = " << opt << "\n";
- }
- if (sh) {
- cout << "option end = " << 2 * (pf[n - 2] + a[n - 1].first) << "\n";
- }
- if (opt > 2 * (pf[n - 2] + a[n - 1].first) ) {
- opt = 2 * (pf[n - 2] + a[n - 1].first);
- x0 = n - 1;
- if (sh) {
- cout << "sec opt = " << opt << "\n";
- }
- }
- //check crd[i] = x0
- for (int i = 1; i < n - 1; ++i) {
- int now = max(2 * (pf[i - 1] + a[i].first), 2 * (repf[i + 1] - a[i].first) );
- if (sh) {
- cout << "i = " << i << "\n";
- cout << "now = " << now << "\n";
- }
- if (now < opt) {
- if (sh) {
- cout << "BETTER!\n";
- }
- opt = now;
- x0 = i;
- }
- }
- //теперь проверим
- if (sh) {
- cout << "\nopt = " << opt << "\n";
- cout << "GO x1\n";
- }
- int bestx1 = -1;
- int x1 = -1; //принадлежит [i; i + 1] в векторе
- for (int i = 0; i < n - 1; ++i) {
- //now * 2 = :
- int now = pf[i] + repf[i + 1];
- //x1 * 2 = :
- x1 = -pf[i] + repf[i + 1] ;
- if(sh) {
- cout << "i = " << i << "\n";
- }
- if (sh) {
- cout << "best x * 2 = " << x1 << "\n";
- cout << "now = " << now << "\n";
- }
- if (x1 <= 2 * a[i].first || x1 >= 2 * a[i + 1].first) {
- if (sh) {
- cout << "skip\n";
- }
- continue; //considered
- }
- if (sh) {
- cout << "check\n";
- }
- if (now < opt) {
- bestx1 = x1;
- opt = now;
- if (sh) {
- cout << "BETTER!\n";
- }
- }
- }
- if (sh) {
- cout << "x0 x1 = " << x0 << ' ' << x1 << "\n";
- }
- x0 = a[x0].first;
- if (bestx1 == -1) {//если x0 подошел
- cout << x0 << "\n";
- return;
- }
- //x1 уже сконвертирован в координату - осталось только поделить на 2
- if (bestx1 % 2 == 0) {
- cout << bestx1 / 2 << "\n";
- return;
- }
- cout << bestx1 / 2 << ".5\n";
- }
- /*
- предпоследний и предпредпоследний
- 6
- 5 4 7 2 10 4
- 3 2 5 1 4 6
- */
- int main() {
- ios::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- int t = 1;
- if (!sh) cin >> t;
- for (int go = 0; go < t; ++ go) {
- slv();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement