Submission #622796

# Submission time Handle Problem Language Result Execution time Memory
622796 2022-08-04T14:37:27 Z cheissmart Sky Walking (IOI19_walk) C++14
43 / 100
3941 ms 219508 KB
#include "walk.h"
#include <bits/stdc++.h>
#define F first
#define S second
#define V vector
#define MP make_pair
#define EB emplace_back
#define PB push_back
#define SZ(v) int((v).size())
#define ALL(v) (v).begin(), (v).end()

using namespace std;

typedef long long ll;
typedef pair<int, int> pi;
typedef V<int> vi;

const int INF = 1e9 + 7, N = 1e5 + 7;
const ll oo = 1e18;

vi asr[N], asl[N];
map<int, ll> dist[N];
map<int, vi> js[N];
vi ys[N];
vi ks[N];

ll brute(vi& x, vi& h, vi& l, vi& r, vi& y, int s, int g) {
	int n = SZ(x);
	int m = SZ(l);

	ys[s].PB(0);
	ys[g].PB(0);

	auto add_ev = [&] (int i, int j) {
		js[i][y[j]].PB(j);
		ys[i].PB(y[j]);
		ks[j].PB(i);
	};

	vi sh(n); iota(ALL(sh), 0);
	sort(ALL(sh), [&] (int i, int j) {
		return h[i] < h[j];
	});
	vi sy(m); iota(ALL(sy), 0);
	sort(ALL(sy), [&] (int i, int j) {
		return y[i] < y[j];
	});
	set<int> st;
	while(SZ(sy)) {
		int j = sy.back(); sy.pop_back();
		while(SZ(sh) && h[sh.back()] >= y[j]) {
			st.insert(sh.back());
			sh.pop_back();
		}
		auto it = st.lower_bound(l[j]);
		while(it != st.end() && *it <= r[j]) {
			add_ev(*it, j);
			it++;
		}
	}

	for(int i = 0; i < n; i++)
		sort(ALL(ys[i]));

	auto get_edges = [&] (int i, int yy) {
		V<pair<pi, int>> res;
		int pos = lower_bound(ALL(ys[i]), yy) - ys[i].begin();
		for(int _ = max(pos - 1, 0); _ <= min(pos + 1, SZ(ys[i]) - 1); _++) {
			int yyy = ys[i][_];
			if(yyy <= h[i]) {
				res.EB(pi(i, yyy), abs(yy - yyy));
			}
		}
		for(int j:js[i][yy]) {
			for(int k:ks[j]) {
				res.EB(pi(k, yy), abs(x[i] - x[k]));
			}
		}
		return res;
	};
	priority_queue<pair<ll, pi>, V<pair<ll, pi>>, greater<pair<ll, pi>>> pq;
	pq.push(MP(0, pi(s, 0)));
	dist[s][0] = 0;
	
	while(pq.size()) {
		auto[dd, state] = pq.top(); pq.pop();
		auto[i, yy] = state;
		if(dd > dist[i][yy]) continue;
		V<pair<pi, int>> res = get_edges(i, yy);
		for(auto[nstate, w]:res) {
			auto[ni, nyy] = nstate;
			if(dist[ni].count(nyy) == 0 || dd + w < dist[ni][nyy]) {
				dist[ni][nyy] = dd + w;
				pq.push(MP(dd + w, nstate));
			}
		}
	}

	if(dist[g].count(0))
		return dist[g][0];
	return -1;
}

ll min_distance(vi x, vi h, vi l, vi r, vi y, int s, int g) {
	int n = SZ(x);
	int m = SZ(l);
	if(s != 0 || g != n - 1) {
		return brute(x, h, l, r, y, s, g);
	}

	for(int i = 0; i < m; i++) {
		asl[l[i]].PB(y[i]);
		asr[r[i]].PB(y[i]);
	}
	map<int, ll> mp;
	mp[0] = 0;
	asr[0].PB(0);

	for(int i = 0; i < n; i++) {
		set<int> no;
		for(int yy:asl[i]) {
			no.insert(yy);
			auto it = mp.lower_bound(yy);
			ll d = oo;
			if(it != mp.end()) 
				d = min(d, it -> S + abs(yy - it -> F));
			if(it != mp.begin()) {
				it--;
				d = min(d, it -> S + abs(yy - it -> F));
			}
			mp[yy] = d;
		}
		if(i < n - 1) for(int yy:asr[i]) {
			if(no.count(yy)) continue;
			mp.erase(yy);
		}
		if(mp.empty()) return -1;
	}

	ll ans = oo;
	for(auto[yy, d]:mp)
		ans = min(ans, d + yy);
	return ans + x[n - 1] - x[0];
}

Compilation message

walk.cpp: In function 'll brute(vi&, vi&, vi&, vi&, vi&, int, int)':
walk.cpp:86:7: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   86 |   auto[dd, state] = pq.top(); pq.pop();
      |       ^
walk.cpp:87:7: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   87 |   auto[i, yy] = state;
      |       ^
walk.cpp:90:11: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   90 |   for(auto[nstate, w]:res) {
      |           ^
walk.cpp:91:8: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   91 |    auto[ni, nyy] = nstate;
      |        ^
walk.cpp: In function 'll min_distance(vi, vi, vi, vi, vi, int, int)':
walk.cpp:141:10: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  141 |  for(auto[yy, d]:mp)
      |          ^
# Verdict Execution time Memory Grader output
1 Correct 11 ms 19028 KB Output is correct
2 Correct 13 ms 19068 KB Output is correct
3 Correct 10 ms 19028 KB Output is correct
4 Correct 11 ms 19028 KB Output is correct
5 Correct 14 ms 19156 KB Output is correct
6 Correct 12 ms 19204 KB Output is correct
7 Correct 11 ms 19108 KB Output is correct
8 Correct 11 ms 19156 KB Output is correct
9 Correct 11 ms 19032 KB Output is correct
10 Correct 12 ms 19204 KB Output is correct
11 Correct 11 ms 19028 KB Output is correct
12 Correct 11 ms 19028 KB Output is correct
13 Correct 10 ms 19028 KB Output is correct
14 Correct 11 ms 18988 KB Output is correct
15 Correct 11 ms 19028 KB Output is correct
16 Correct 11 ms 19028 KB Output is correct
17 Correct 11 ms 19028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 19028 KB Output is correct
2 Correct 11 ms 19028 KB Output is correct
3 Correct 2077 ms 152756 KB Output is correct
4 Correct 1232 ms 162408 KB Output is correct
5 Correct 1051 ms 106412 KB Output is correct
6 Correct 480 ms 96888 KB Output is correct
7 Correct 645 ms 106408 KB Output is correct
8 Correct 3941 ms 191648 KB Output is correct
9 Correct 702 ms 112452 KB Output is correct
10 Correct 1767 ms 219508 KB Output is correct
11 Correct 712 ms 93528 KB Output is correct
12 Correct 149 ms 33288 KB Output is correct
13 Correct 109 ms 33196 KB Output is correct
14 Correct 287 ms 58588 KB Output is correct
15 Correct 344 ms 72396 KB Output is correct
16 Incorrect 333 ms 73288 KB Output isn't correct
17 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 32 ms 21272 KB Output is correct
2 Correct 99 ms 22864 KB Output is correct
3 Correct 117 ms 23496 KB Output is correct
4 Correct 143 ms 27220 KB Output is correct
5 Correct 158 ms 31672 KB Output is correct
6 Correct 180 ms 29484 KB Output is correct
7 Correct 72 ms 24696 KB Output is correct
8 Correct 126 ms 29364 KB Output is correct
9 Correct 151 ms 31144 KB Output is correct
10 Correct 146 ms 30768 KB Output is correct
11 Correct 21 ms 19740 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 32 ms 21272 KB Output is correct
2 Correct 99 ms 22864 KB Output is correct
3 Correct 117 ms 23496 KB Output is correct
4 Correct 143 ms 27220 KB Output is correct
5 Correct 158 ms 31672 KB Output is correct
6 Correct 180 ms 29484 KB Output is correct
7 Correct 72 ms 24696 KB Output is correct
8 Correct 126 ms 29364 KB Output is correct
9 Correct 151 ms 31144 KB Output is correct
10 Correct 146 ms 30768 KB Output is correct
11 Correct 21 ms 19740 KB Output is correct
12 Correct 100 ms 23420 KB Output is correct
13 Correct 88 ms 26920 KB Output is correct
14 Correct 164 ms 31656 KB Output is correct
15 Correct 121 ms 27192 KB Output is correct
16 Correct 121 ms 27356 KB Output is correct
17 Correct 122 ms 27264 KB Output is correct
18 Correct 115 ms 27276 KB Output is correct
19 Correct 115 ms 27272 KB Output is correct
20 Correct 79 ms 24756 KB Output is correct
21 Correct 42 ms 20684 KB Output is correct
22 Correct 104 ms 25164 KB Output is correct
23 Correct 116 ms 25552 KB Output is correct
24 Correct 93 ms 27044 KB Output is correct
25 Correct 101 ms 25572 KB Output is correct
26 Correct 96 ms 29540 KB Output is correct
27 Correct 169 ms 31204 KB Output is correct
28 Correct 78 ms 26804 KB Output is correct
29 Correct 188 ms 29540 KB Output is correct
30 Correct 79 ms 24724 KB Output is correct
31 Correct 143 ms 31024 KB Output is correct
32 Correct 92 ms 28996 KB Output is correct
33 Correct 102 ms 29964 KB Output is correct
34 Correct 100 ms 29260 KB Output is correct
35 Correct 131 ms 28140 KB Output is correct
36 Correct 93 ms 27736 KB Output is correct
37 Correct 108 ms 27340 KB Output is correct
38 Correct 98 ms 29232 KB Output is correct
39 Correct 113 ms 30932 KB Output is correct
40 Correct 95 ms 29052 KB Output is correct
41 Correct 88 ms 27784 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 19028 KB Output is correct
2 Correct 13 ms 19068 KB Output is correct
3 Correct 10 ms 19028 KB Output is correct
4 Correct 11 ms 19028 KB Output is correct
5 Correct 14 ms 19156 KB Output is correct
6 Correct 12 ms 19204 KB Output is correct
7 Correct 11 ms 19108 KB Output is correct
8 Correct 11 ms 19156 KB Output is correct
9 Correct 11 ms 19032 KB Output is correct
10 Correct 12 ms 19204 KB Output is correct
11 Correct 11 ms 19028 KB Output is correct
12 Correct 11 ms 19028 KB Output is correct
13 Correct 10 ms 19028 KB Output is correct
14 Correct 11 ms 18988 KB Output is correct
15 Correct 11 ms 19028 KB Output is correct
16 Correct 11 ms 19028 KB Output is correct
17 Correct 11 ms 19028 KB Output is correct
18 Correct 11 ms 19028 KB Output is correct
19 Correct 11 ms 19028 KB Output is correct
20 Correct 2077 ms 152756 KB Output is correct
21 Correct 1232 ms 162408 KB Output is correct
22 Correct 1051 ms 106412 KB Output is correct
23 Correct 480 ms 96888 KB Output is correct
24 Correct 645 ms 106408 KB Output is correct
25 Correct 3941 ms 191648 KB Output is correct
26 Correct 702 ms 112452 KB Output is correct
27 Correct 1767 ms 219508 KB Output is correct
28 Correct 712 ms 93528 KB Output is correct
29 Correct 149 ms 33288 KB Output is correct
30 Correct 109 ms 33196 KB Output is correct
31 Correct 287 ms 58588 KB Output is correct
32 Correct 344 ms 72396 KB Output is correct
33 Incorrect 333 ms 73288 KB Output isn't correct
34 Halted 0 ms 0 KB -