Submission #571113

# Submission time Handle Problem Language Result Execution time Memory
571113 2022-06-01T09:35:39 Z cadmiumsky Abduction 2 (JOI17_abduction2) C++14
44 / 100
4660 ms 17124 KB
#include <bits/stdc++.h>

using namespace std;
using pii = pair<int,int>;
using tii = tuple<int,int,int>;
using ll = long long;

const int nmax = 2e5 + 5, inf = 1e9 + 5;

//#warning vectorii sa ii indexezi de la 1
int h[nmax], v[nmax]; int L, C;
int get(int a) { return  (a < 0? h[-a] : v[a]); }

namespace G {
  vector< vector<pii> > g;
  vector<ll> dp;
  
  void reset() {
    g.clear();
    dp.clear();
  }
  
  int newnode() {
    g.push_back(vector<pii>());
    dp.push_back(-1);
    return g.size() - 1;
  }
  
  void addedge(int x, int y, int c) { g[x].push_back({y, c}); }
  
  int startdfs(int node) {
    if(dp[node] != -1)
      return dp[node];
    for(auto [a, b] : g[node])
      dp[node] = max((ll)startdfs(a) + b, dp[node]);
    return dp[node];
  }
}

int start(int x, int y, bool hor, bool dir) {
  //cerr << hor << ' ' << dir << '\n';
  //auto cmp = [&](tii a, tii b) { return get(get<0>(a)) > get(get<0>(b)); };
  //priority_queue<tuple<int,int,int>, vector< tuple<int,int,int> >, decltype(cmp) > heap(cmp);
  auto cmp = [&](int a, int b) { return get(a) < get(b); };
  map<int, vector<pii>, decltype(cmp) > heap(cmp);
  G::reset();
  int source, sink = G::newnode(); // == 0
  int l[2] = {x, y}, r[2] = {x, y};
  if(hor)
    heap[-x].push_back({y, source = G::newnode()}),
    (dir == 0? l[1]-- : r[1]++);
  else
    heap[y].push_back({x, source = G::newnode()}),
    (dir == 0? l[0]-- : r[0]++);
  int node;
  while(!heap.empty()) {
    x = heap.begin()->first;
    //cerr << x << '\n';
    int dir = 0, target = get(x);
    if(x < 0)
      dir = 1;
    while(l[dir] > 0 && (dir == 0? h : v)[l[dir]] < target)
      l[dir]--;
    while(r[dir] <= (dir == 0? L : C) && (dir == 0? h : v)[r[dir]] < target)
      r[dir]++;
    //cerr << " > " << x << ' ' << get(x) << ' ' << l[dir] << ' ' << r[dir] << '\n';
    int lnode = sink, rnode = sink;
    if(l[dir] != 0)
      heap[(dir == 0? -1 : 1) * l[dir]].push_back({abs(x), lnode = G::newnode()});
    if(r[dir] <= (dir == 0? L : C))
      heap[(dir == 0? -1 : 1) * r[dir]].push_back({abs(x), rnode = G::newnode()});
  
    auto &vec = heap[x];
    for(auto [coord, atrnode] : vec) {
      G::addedge(atrnode, lnode, abs(coord - l[dir]));
      G::addedge(atrnode, rnode, abs(coord - r[dir]));
    }
    vec.clear();
    heap.erase(heap.begin());
  }
  //cerr << G::startdfs(source) << '\n';
//cerr << '\n';  
  return G::startdfs(source);
  
}

int main() {
  ios_base::sync_with_stdio(0);
  cin >> L >> C;
  int q;
  cin >> q;
  for(int i = 1; i <= L; i++)
    cin >> h[i];
  for(int i = 1; i <= C; i++)
    cin >> v[i];
  for(int i = 0; i < q; i++) {
    int x, y;
    cin >> x >> y;
    cout << max({start(x, y, 1, 1), start(x, y, 0, 0)
              , start(x, y, 1, 0), start(x, y, 0, 1)}) << '\n';
    //cerr << "-------------------\n";
  }
} 

Compilation message

abduction2.cpp: In function 'int G::startdfs(int)':
abduction2.cpp:34:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   34 |     for(auto [a, b] : g[node])
      |              ^
abduction2.cpp: In function 'int start(int, int, bool, bool)':
abduction2.cpp:74:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   74 |     for(auto [coord, atrnode] : vec) {
      |              ^
abduction2.cpp:55:7: warning: unused variable 'node' [-Wunused-variable]
   55 |   int node;
      |       ^~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 328 KB Output is correct
6 Correct 1 ms 324 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 328 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 328 KB Output is correct
6 Correct 1 ms 324 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 328 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 2 ms 344 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 2 ms 340 KB Output is correct
17 Correct 1 ms 376 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 5 ms 596 KB Output is correct
20 Correct 4 ms 596 KB Output is correct
21 Correct 4 ms 596 KB Output is correct
22 Correct 6 ms 756 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 328 KB Output is correct
6 Correct 1 ms 324 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 328 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 2 ms 344 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 2 ms 340 KB Output is correct
17 Correct 1 ms 376 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 5 ms 596 KB Output is correct
20 Correct 4 ms 596 KB Output is correct
21 Correct 4 ms 596 KB Output is correct
22 Correct 6 ms 756 KB Output is correct
23 Correct 12 ms 1616 KB Output is correct
24 Correct 13 ms 1580 KB Output is correct
25 Correct 11 ms 1592 KB Output is correct
26 Correct 13 ms 1632 KB Output is correct
27 Correct 11 ms 1612 KB Output is correct
28 Correct 43 ms 5744 KB Output is correct
29 Correct 14 ms 2260 KB Output is correct
30 Correct 87 ms 7564 KB Output is correct
31 Correct 99 ms 9084 KB Output is correct
32 Correct 16 ms 2092 KB Output is correct
33 Correct 27 ms 3448 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 340 KB Output is correct
2 Correct 5 ms 340 KB Output is correct
3 Correct 5 ms 340 KB Output is correct
4 Correct 5 ms 340 KB Output is correct
5 Correct 5 ms 340 KB Output is correct
6 Correct 65 ms 596 KB Output is correct
7 Correct 67 ms 588 KB Output is correct
8 Correct 180 ms 808 KB Output is correct
9 Correct 146 ms 820 KB Output is correct
10 Correct 167 ms 800 KB Output is correct
11 Correct 220 ms 884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 328 KB Output is correct
6 Correct 1 ms 324 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 328 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 2 ms 344 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 2 ms 340 KB Output is correct
17 Correct 1 ms 376 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 5 ms 596 KB Output is correct
20 Correct 4 ms 596 KB Output is correct
21 Correct 4 ms 596 KB Output is correct
22 Correct 6 ms 756 KB Output is correct
23 Correct 12 ms 1616 KB Output is correct
24 Correct 13 ms 1580 KB Output is correct
25 Correct 11 ms 1592 KB Output is correct
26 Correct 13 ms 1632 KB Output is correct
27 Correct 11 ms 1612 KB Output is correct
28 Correct 43 ms 5744 KB Output is correct
29 Correct 14 ms 2260 KB Output is correct
30 Correct 87 ms 7564 KB Output is correct
31 Correct 99 ms 9084 KB Output is correct
32 Correct 16 ms 2092 KB Output is correct
33 Correct 27 ms 3448 KB Output is correct
34 Correct 5 ms 340 KB Output is correct
35 Correct 5 ms 340 KB Output is correct
36 Correct 5 ms 340 KB Output is correct
37 Correct 5 ms 340 KB Output is correct
38 Correct 5 ms 340 KB Output is correct
39 Correct 65 ms 596 KB Output is correct
40 Correct 67 ms 588 KB Output is correct
41 Correct 180 ms 808 KB Output is correct
42 Correct 146 ms 820 KB Output is correct
43 Correct 167 ms 800 KB Output is correct
44 Correct 220 ms 884 KB Output is correct
45 Correct 42 ms 1620 KB Output is correct
46 Correct 48 ms 1616 KB Output is correct
47 Correct 48 ms 1616 KB Output is correct
48 Correct 46 ms 1652 KB Output is correct
49 Correct 59 ms 1672 KB Output is correct
50 Correct 1506 ms 7224 KB Output is correct
51 Correct 1253 ms 7584 KB Output is correct
52 Correct 4352 ms 14632 KB Output is correct
53 Correct 4426 ms 14492 KB Output is correct
54 Correct 4288 ms 11560 KB Output is correct
55 Incorrect 4660 ms 17124 KB Output isn't correct
56 Halted 0 ms 0 KB -