Submission #385347

# Submission time Handle Problem Language Result Execution time Memory
385347 2021-04-04T05:44:48 Z mohamedsobhi777 Tram (CEOI13_tram) C++14
45 / 100
1000 ms 808 KB
#include <bits/stdc++.h>

using namespace std;

#define vi vector<int>
#define vll vector<ll>
#define vii vector<pair<int, int>>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define loop(_) for (int __ = 0; __ < (_); ++__)
#define pb push_back
#define f first
#define s second
#define sz(_) ((int)_.size())
#define all(_) _.begin(), _.end()
#define lb lower_bound
#define ub upper_bound

using ll = long long;
using ld = long double;

const int N = 1e5 + 7;
const ll mod = 1e9 + 7;

int n, m;
int vis[N][2];
int xs[N], ys[N];
multiset<int> mul;

struct cmp
{
       bool operator()(pii x, pii y)
       {
              return x.s - x.f != y.s - y.f ? x.s - x.f > y.s - y.f : x.f < y.f;
       }
};

set<pii, cmp> st;
set<pii> rst;

int calc(int x, int y)
{
       int ret = 1e9;
       if (mul.empty())
              return ret;
       auto eval = [&](int u) -> void {
              for (int i = 0; i < 2; ++i)
              {
                     if (vis[u][i])
                     {
                            ret = min(ret, (u - x) * (u - x) + (i - y) * (i - y));
                     }
              }
       };
       auto it = mul.lb(x);
       if (it != mul.end())
              eval(*it);
       if (it != mul.begin())
              eval(*(--it));
       return ret;
}

void Insert(int x, int y)
{
       st.insert({x, y});
       rst.insert({x, y});
}

void Erase(int x, int y)
{
       st.erase({x, y});
       rst.erase({x, y});
}

void recalc()
{
       rst.clear(), st.clear();
       int lst = -1;
       for (int j = 0; j < n; ++j)
       {
              if (vis[j][0] || vis[j][1])
              {
                     if (lst + 1 <= j - 1)
                            Insert(lst + 1, j - 1);
                     lst = j;
              }
       }
       if (lst + 1 < n)
              Insert(lst + 1, n - 1);
}

int main()
{
       ios_base::sync_with_stdio(0);
       cin.tie(0);
#ifndef ONLINE_JUDGE
#endif
       cin >> n >> m;
       Insert(0, n - 1);
       for (int i = 0; i < m; ++i)
       {
              char c;
              cin >> c;
              if (c == 'E')
              {
                     int Min = 0;
                     int wx = 0, wy = 0;
                     if (sz(mul) >= 2)
                     {
                            vii vc = {{0, 0}, {0, 1}, {n - 1, 0}, {n - 1, 1}};
                            for (auto u : vc)
                            {
                                   int sc = calc(u.f, u.s);
                                   if (sc > Min)
                                   {
                                          Min = sc;
                                          wx = u.f, wy = u.s;
                                   }
                            }
                            for (auto u : st)
                            {
                                   int x = u.f, y = u.s;
                                   int mid = (x + y) >> 1;
                                   for (int j = x; j <= y; ++j)
                                          for (int i = 0; i < 2; ++i)
                                          {
                                                 if (j < x || j > y || vis[j][i])
                                                        continue;
                                                 int w = calc(j, i);
                                                 if (w > Min || (w == Min && make_pair(j, i) < make_pair(wx, wy)))
                                                 {
                                                        Min = w;
                                                        wx = j, wy = i;
                                                 }
                                          }
                            }
                            if (Min <= 9)
                            {
                                   Min = 0;
                                   for (int a = 0; a < n; ++a)
                                   {
                                          for (int b = 0; b < 2; ++b)
                                          {
                                                 int rv = calc(a, b);
                                                 if (rv > Min || (rv == Min && make_pair(a, b) < make_pair(wx, wy)))
                                                 {
                                                        wx = a, wy = b;
                                                        Min = rv;
                                                 }
                                          }
                                   }
                            }
                     }
                     else if (!sz(mul))
                            ;
                     else
                     {
                            // wx = sz(mul) - n;
                            // wy = calc(wx, 1) > calc(wx, 0);
                            if (1)
                            {
                                   Min = 0;
                                   for (int a = 0; a < n; ++a)
                                   {
                                          for (int b = 0; b < 2; ++b)
                                          {
                                                 int rv = calc(a, b);
                                                 if (rv > Min || (rv == Min && make_pair(a, b) < make_pair(wx, wy)))
                                                 {
                                                        wx = a, wy = b;
                                                        Min = rv;
                                                 }
                                          }
                                   }
                            }
                     }

                     xs[i] = wx, ys[i] = wy;
                     vis[wx][wy] = 1;
                     for (auto u : st)
                     {
                            if (wx >= u.f && wx <= u.s)
                            {
                                   int x = u.f, y = u.s;
                                   Erase(x, y);
                                   if (x <= wx - 1)
                                          Insert(x, wx - 1);
                                   if (wx + 1 <= y)
                                          Insert(wx + 1, y);
                                   break;
                            }
                     }

                     mul.insert(wx);
                     cout << ++wx << " " << ++wy << "\n";
              }
              else
              {
                     int p;
                     cin >> p;
                     --p;

                     mul.erase(mul.find(xs[p]));
                     vis[xs[p]][ys[p]] = 0;

                     if (vis[xs[p]][0] == vis[xs[p]][1])
                     {
                            int nx = xs[p], ny = xs[p];
                            auto it = rst.lb({xs[p], -1});
                            if (it != rst.end() && (*it).f == ny + 1)
                            {
                                   ny = (*it).s;
                                   Erase((*it).f, (*it).s);
                            }
                            if (it != rst.begin())
                            {
                                   --it;
                                   if ((*it).s == nx - 1)
                                   {
                                          nx = (*it).f;
                                          Erase((*it).f, (*it).s);
                                   }
                            }
                            Insert(nx, ny);
                     }
              }
       }
       return 0;
}
/*
01:*
02: #
03: 
04:* 
05: 
06:
07:*
08:
09:
10: #


*/

Compilation message

tram.cpp: In function 'int main()':
tram.cpp:123:40: warning: unused variable 'mid' [-Wunused-variable]
  123 |                                    int mid = (x + y) >> 1;
      |                                        ^~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 340 ms 492 KB Output is correct
2 Correct 9 ms 364 KB Output is correct
3 Correct 97 ms 492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 335 ms 512 KB Output is correct
2 Correct 9 ms 364 KB Output is correct
3 Correct 99 ms 492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 7 ms 492 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 6 ms 492 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1060 ms 808 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 7 ms 492 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -