Submission #912709

# Submission time Handle Problem Language Result Execution time Memory
912709 2024-01-19T18:22:34 Z cadmiumsky Sightseeing in Kyoto (JOI22_kyoto) C++17
10 / 100
2000 ms 16372 KB
#include <bits/stdc++.h>
#define all(x) (x).begin(),(x).end()
using namespace std;

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

#define int ll
#define sz(x) ((int)(x).size())

using pii = pair<int,int>;
using tii = tuple<int,int,int>;

const int inf = 1e9 + 5;

auto compare_frac = [](pii a, pii b) -> bool { return (ll)a.first * b.second < (ll)b.first * a.second; };
auto convoluted_cmpfrac = [](pair<pii,int> a, pair<pii,int> b) -> bool { return compare_frac(a.first, b.first) || (!compare_frac(a.first, b.first) && !compare_frac(b.first, a.first) && a.second < b.second); };

struct Turneu {
  
  set<int> appears;
  int *a;
  
  set<pair<pii, int>, decltype(convoluted_cmpfrac)> heap;
  Turneu(): heap(convoluted_cmpfrac) {;}
  
  int n;
  
  template<class iteratorlmao>
  void insertIt(iteratorlmao it) {
    if(*next(it) == n + 1 || *it == -1) return;
    //cerr << *it << '\t' << a[*next(it)] << ' ' << a[*it] << '\n';
    
    cerr << "inseram\n";
    cerr << a << '\t' << *it << ", " << *next(it) << '\t';
    cerr << a[*next(it)] - a[*it] << ' ' << *next(it) - *it << '\t' << *it << '\n';
    
    heap.emplace(pii{a[*next(it)] - a[*it], *next(it) - *it}, *it);
  }
  
  template<class iteratorlmao>
  void eraseIt(iteratorlmao it) {
    if(*next(it) == n + 1 || *it == -1) return;
    cerr << a << '\t' << *it << ' ' << *next(it) << '\t';
    cerr << a[*next(it)] - a[*it] << ' ' << *next(it) - *it << '\t' << *it << '\n' << "iata ";
    heap.erase(heap.find(pair<pii,int>{pii{a[*next(it)] - a[*it], *next(it) - *it}, *it}));
    cerr << "ca nu-i aici greseala\n";
  }
  
  
  void init(int _n, int *v) {
    n = _n;
    a = v;
    
    appears.insert(-1);
    appears.insert(n + 1);
    
    for(int i = 1; i <= n; i++)
      insertIt(prev(appears.insert(i).first));
      
    heap.emplace(pii{inf, 0}, -1);
  }
  
  void erase(int p) {
    assert(p > -1);
    
    cerr << "stergem " << p << "\n--\n";
    
    auto it = appears.find(p);
    
    eraseIt(it);
    eraseIt(prev(it));
    
    auto nvit = prev(it);
    appears.erase(it);
    cerr << *nvit << '\n';
    insertIt(nvit);
  }
};

const int nmax = 1e5 + 5;

int A[nmax], B[nmax];

signed main() {
  cin.tie(0) -> sync_with_stdio(0);
  int H, W;
  cin >> H >> W;
  
  for(int i = 1; i <= H; i++) cin >> A[i];
  for(int i = 1; i <= W; i++) cin >> B[i];
  
  Turneu linii, coloane;
  
  linii.init(H, A);
  coloane.init(W, B);
  
  ll cost = 0;
  int x = 1, y = 1;
  
  auto move_col = [&]() {
    int nvy = *next(coloane.appears.begin());
    
    cost += (ll)(nvy - y) * A[x];
    y = nvy;
  };
  
  auto move_row = [&]() {
    int nvx = *next(linii.appears.begin());
    
    cost += (ll)(nvx - x) * B[y];
    x = nvx;
  };
  
  while(x != H || y != W) {
    
    //auto [Ax, Ay] = linii.heap.begin() -> first;
    //auto [Bx, By] = coloane.heap.begin() -> first;
    
    //cerr << Ax << "/" << Ay << '\t' << Bx << "/" << By << '\n';
    
    if(compare_frac(linii.heap.begin() -> first, coloane.heap.begin() -> first))
      //cerr << "1\n",
      linii.erase(linii.heap.begin() -> second);
    else
      //cerr << "2\n",
      coloane.erase(coloane.heap.begin() -> second);
    
    move_col(); // gen, merge sa le fac pe ambele albeit e retardat
    move_row();
  }
  
  cout << cost << '\n';
}

/**
  Anul asta nu se da centroid
  -- Rugaciunile mele
*/

# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 66 ms 680 KB Output is correct
5 Correct 32 ms 568 KB Output is correct
6 Correct 23 ms 344 KB Output is correct
7 Correct 14 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 126 ms 1060 KB Output is correct
10 Correct 125 ms 1104 KB Output is correct
11 Correct 127 ms 1108 KB Output is correct
12 Correct 126 ms 1032 KB Output is correct
13 Correct 69 ms 852 KB Output is correct
14 Correct 126 ms 1016 KB Output is correct
15 Correct 128 ms 1072 KB Output is correct
16 Correct 129 ms 1108 KB Output is correct
17 Correct 68 ms 816 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 344 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 87 ms 644 KB Output is correct
4 Execution timed out 2012 ms 16372 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 66 ms 680 KB Output is correct
5 Correct 32 ms 568 KB Output is correct
6 Correct 23 ms 344 KB Output is correct
7 Correct 14 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 126 ms 1060 KB Output is correct
10 Correct 125 ms 1104 KB Output is correct
11 Correct 127 ms 1108 KB Output is correct
12 Correct 126 ms 1032 KB Output is correct
13 Correct 69 ms 852 KB Output is correct
14 Correct 126 ms 1016 KB Output is correct
15 Correct 128 ms 1072 KB Output is correct
16 Correct 129 ms 1108 KB Output is correct
17 Correct 68 ms 816 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 344 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 344 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 87 ms 644 KB Output is correct
32 Execution timed out 2012 ms 16372 KB Time limit exceeded
33 Halted 0 ms 0 KB -