Submission #707100

# Submission time Handle Problem Language Result Execution time Memory
707100 2023-03-08T12:53:55 Z 600Mihnea Jakarta Skyscrapers (APIO15_skyscraper) C++17
100 / 100
47 ms 9832 KB
#include <cmath>
#include <functional>
#include <fstream>
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <list>
#include <time.h>
#include <math.h>
#include <random>
#include <deque>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <cassert>
#include <bitset>
#include <sstream>
#include <chrono>
#include <cstring>
#include <numeric>

using namespace std;

const int INF = (int)1e9 + 7;

struct T
{
	int pos;
	int delta;
};

bool operator < (T a, T b)
{
	return make_pair(a.pos, a.delta) < make_pair(b.pos, b.delta);
}

signed main()
{
#ifdef ONPC	
	FILE* stream;
	freopen_s(&stream, "input.txt", "r", stdin);
#else
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#endif

	int dim, cntofs;
	cin >> dim >> cntofs;

	vector<T> ofs(cntofs);
	vector<vector<int>> cine(dim);
	for (int i = 0; i < cntofs; i++)
	{
		cin >> ofs[i].pos >> ofs[i].delta;
		assert(0 <= ofs[i].pos && ofs[i].pos < dim);
	}
	int Init = ofs[0].pos;
	int Query = ofs[1].pos;

	sort(ofs.begin(), ofs.end());

	vector<vector<int>> deltas(dim);

	{
		assert(!ofs.empty());
		vector<T> newofs = { ofs[0] };
		for (int i = 1; i < cntofs; i++)
		{
			if (make_pair(ofs[i].pos, ofs[i].delta) != make_pair(ofs[i - 1].pos, ofs[i - 1].delta))
			{
				newofs.push_back(ofs[i]);
			}
		}
		ofs = newofs;
		cntofs = (int)ofs.size();
		for (auto& of : ofs)
		{
			deltas[of.pos].push_back(of.delta);
		}
		for (auto& delta : deltas)
		{
			sort(delta.begin(), delta.end());
		}
	}

	auto has = [&](int pos, int delta)
	{
		int low = 0, high = (int)deltas[pos].size() - 1;
		while (low <= high)
		{
			int mid = (low + high) / 2;
			if (deltas[pos][mid] == delta)
			{
				return 1;
			}
			if (deltas[pos][mid] < delta)
			{
				low = mid + 1;
			}
			else
			{
				high = mid - 1;
			}
		}
		return 0;
	};

	assert((int)ofs.size() == cntofs);

	for (int i = 0; i < cntofs; i++)
	{
		assert(0 <= ofs[i].pos && ofs[i].pos < dim);
		cine[ofs[i].pos].push_back(i);
	}

	
	vector<int> dp(dim, INF);

	priority_queue<pair<int, int>> pq;

	auto relax = [&](int i, int val)
	{
		if (val < dp[i])
		{
			dp[i] = val;
			pq.push({ -dp[i], i });
		}
	};

	set<pair<int, int>> s_delta_rest;
	// {delta, rest}
	for (auto& it : ofs)
	{
		s_delta_rest.insert({ it.delta, it.pos % it.delta });
	}
	vector<pair<int, int>> v_delta_rest;

	for (auto& it : s_delta_rest)
	{
		v_delta_rest.push_back(it);
	}

	int states = (int)v_delta_rest.size();
	vector<vector<int>> cool(states);
	for (int it = 0; it < states; it++)
	{
		int i = v_delta_rest[it].first, j = v_delta_rest[it].second;
		for (int p = j; p < dim; p += i)
		{
			if (!cine[p].empty())
			{
				cool[it].push_back(p);
			}
		}
	}

	auto fnd = [&](pair<int, int> it)
	{
		assert(0 <= it.second && it.second < it.first);
		int l = 0, r = (int)v_delta_rest.size() - 1;
		while (l <= r)
		{
			int m = (l + r) / 2;
			if (v_delta_rest[m] == it)
			{
				return m;
			}
			if (v_delta_rest[m] < it)
			{
				l = m + 1;
			}
			else
			{
				r = m - 1;
			}
		}
		assert(0);
	};

	relax(Init, 0);
	
	while (!pq.empty())
	{
		auto it = pq.top();
		pq.pop();
		int loc = it.second;
		if (-it.first != dp[loc])
		{
			continue;
		}
		for (auto& i : cine[loc])
		{
			int what = fnd({ ofs[i].delta, loc % ofs[i].delta });
			int pz = lower_bound(cool[what].begin(), cool[what].end(), loc) - cool[what].begin();
			assert(0 <= pz && pz < (int)cool[what].size());
			assert(cool[what][pz] == loc);
			for (int j = pz - 1; j >= 0; j--)
			{
				int loc2 = cool[what][j];
				int posdif = abs(ofs[i].pos - loc2);
				relax(loc2, dp[loc] + posdif / ofs[i].delta);
				if (has(cool[what][j], ofs[i].delta)) break;
			}
			for (int j = pz + 1; j < (int)cool[what].size(); j++)
			{
				int loc2 = cool[what][j];
				int posdif = abs(ofs[i].pos - loc2);
				relax(loc2, dp[loc] + posdif / ofs[i].delta);
				if (has(cool[what][j], ofs[i].delta)) break;
			}
		}
	}
	int sol = (dp[Query] == INF) ? (-1) : (dp[Query]);
	cout << sol << "\n";
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 304 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 2 ms 468 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 2 ms 596 KB Output is correct
15 Correct 2 ms 580 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 3 ms 468 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 2 ms 468 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 2 ms 596 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 468 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 1 ms 468 KB Output is correct
24 Correct 2 ms 812 KB Output is correct
25 Correct 2 ms 724 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 468 KB Output is correct
28 Correct 2 ms 852 KB Output is correct
29 Correct 1 ms 468 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 468 KB Output is correct
32 Correct 1 ms 468 KB Output is correct
33 Correct 2 ms 596 KB Output is correct
34 Correct 2 ms 596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 544 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 2 ms 596 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 2 ms 596 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 468 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 1 ms 468 KB Output is correct
24 Correct 2 ms 724 KB Output is correct
25 Correct 2 ms 724 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 468 KB Output is correct
28 Correct 2 ms 852 KB Output is correct
29 Correct 1 ms 468 KB Output is correct
30 Correct 0 ms 340 KB Output is correct
31 Correct 1 ms 468 KB Output is correct
32 Correct 1 ms 468 KB Output is correct
33 Correct 2 ms 596 KB Output is correct
34 Correct 2 ms 596 KB Output is correct
35 Correct 23 ms 3980 KB Output is correct
36 Correct 4 ms 1108 KB Output is correct
37 Correct 23 ms 3512 KB Output is correct
38 Correct 31 ms 4812 KB Output is correct
39 Correct 30 ms 4860 KB Output is correct
40 Correct 30 ms 4848 KB Output is correct
41 Correct 30 ms 4840 KB Output is correct
42 Correct 5 ms 612 KB Output is correct
43 Correct 4 ms 600 KB Output is correct
44 Correct 4 ms 724 KB Output is correct
45 Correct 21 ms 4244 KB Output is correct
46 Correct 19 ms 4324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 216 KB Output is correct
9 Correct 1 ms 216 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 3 ms 476 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 2 ms 468 KB Output is correct
15 Correct 2 ms 472 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 2 ms 596 KB Output is correct
18 Correct 1 ms 472 KB Output is correct
19 Correct 1 ms 468 KB Output is correct
20 Correct 2 ms 468 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 1 ms 468 KB Output is correct
24 Correct 2 ms 724 KB Output is correct
25 Correct 2 ms 724 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 468 KB Output is correct
28 Correct 2 ms 852 KB Output is correct
29 Correct 1 ms 468 KB Output is correct
30 Correct 0 ms 340 KB Output is correct
31 Correct 1 ms 468 KB Output is correct
32 Correct 1 ms 468 KB Output is correct
33 Correct 2 ms 596 KB Output is correct
34 Correct 2 ms 656 KB Output is correct
35 Correct 31 ms 3964 KB Output is correct
36 Correct 5 ms 980 KB Output is correct
37 Correct 22 ms 3512 KB Output is correct
38 Correct 40 ms 4856 KB Output is correct
39 Correct 30 ms 4836 KB Output is correct
40 Correct 31 ms 4876 KB Output is correct
41 Correct 31 ms 4860 KB Output is correct
42 Correct 4 ms 616 KB Output is correct
43 Correct 5 ms 596 KB Output is correct
44 Correct 4 ms 724 KB Output is correct
45 Correct 21 ms 4324 KB Output is correct
46 Correct 20 ms 4324 KB Output is correct
47 Correct 47 ms 8096 KB Output is correct
48 Correct 19 ms 5544 KB Output is correct
49 Correct 18 ms 5256 KB Output is correct
50 Correct 12 ms 4424 KB Output is correct
51 Correct 45 ms 8164 KB Output is correct
52 Correct 45 ms 8292 KB Output is correct
53 Correct 30 ms 7196 KB Output is correct
54 Correct 1 ms 1748 KB Output is correct
55 Correct 1 ms 1748 KB Output is correct
56 Correct 11 ms 4112 KB Output is correct
57 Correct 8 ms 2260 KB Output is correct
58 Correct 5 ms 2120 KB Output is correct
59 Correct 6 ms 2260 KB Output is correct
60 Correct 6 ms 2260 KB Output is correct
61 Correct 7 ms 2644 KB Output is correct
62 Correct 33 ms 9832 KB Output is correct
63 Correct 29 ms 5988 KB Output is correct
64 Correct 30 ms 5860 KB Output is correct
65 Correct 33 ms 5860 KB Output is correct
66 Correct 45 ms 5860 KB Output is correct
67 Correct 40 ms 5912 KB Output is correct