Submission #778708

# Submission time Handle Problem Language Result Execution time Memory
778708 2023-07-10T15:19:19 Z Boas Xylophone (JOI18_xylophone) C++17
100 / 100
75 ms 688 KB
#include "xylophone.h"
using namespace std;
#include <iostream>
#include <vector>
#include <bitset>
#include <algorithm>
#include <unordered_map>
//#define all(x) x.begin(), x.end()
#define pii pair<int, int>
#include <chrono>
using namespace chrono;


struct pairhash
{
public:
	template <typename T, typename U>
	size_t operator()(const pair<T, U>& x) const
	{
		return hash<T>()(x.first) * 100003 ^ hash<U>()(x.second);
	}
};

unordered_map<pii, int, pairhash> queries;

int ownQuery(int a, int b)
{
	if (queries.find({ a, b }) != queries.end())
	{
		return queries[{a, b}];
	}
	return queries[{a, b}] = query(a, b);
}

void solve(int N)
{
	int a = 1, b = N;
	while (a < b)
	{
		int k = (a + b) / 2;
		int value = ownQuery(1, k);
		if (value == N - 1) b = k;
		else a = k + 1;
	}
	int maxIndex = a == b ? a : -1;
	a = 1, b = maxIndex - 1;
	while (a < b)
	{
		int k = (a + b + 1) / 2;
		int value = ownQuery(k, maxIndex);
		if (value == N - 1) a = k;
		else b = k - 1;
	}
	int oneIndex = a == b ? a : -1;
	bitset<5001> found;
	vector<int> values(N + 1, -1);
	found[0] = true;
	for (int i = N + 1; i < 5001; i++)
	{
		found[i] = true;
	}
	found[1] = true;
	found[N] = true;
	values[oneIndex] = 1;
	values[maxIndex] = N;
	bool cantProgress = true;
	while (!found.all())
	{
		cantProgress = true;
		for (int i = 1; i <= N; i++)
		{
			if (values[i] != -1)
			{
				int j = i - 1;
				if (j > 0 && values[j] == -1)
				{
					int v = ownQuery(j, i);
					int opt1 = values[i] + v;
					int opt2 = values[i] - v;
					if (opt1 <= 1)
					{
						values[j] = opt2; cantProgress = false; found[opt2] = true;
					}
					else if (opt2 <= 1)
					{
						values[j] = opt1; cantProgress = false; found[opt1] = true;
					}
					else if (opt1 >= N)
					{
						values[j] = opt2; cantProgress = false; found[opt2] = true;
					}
					else if (opt2 >= N)
					{
						values[j] = opt1; cantProgress = false; found[opt1] = true;
					}
					else if (found[opt1])
					{
						values[j] = opt2; cantProgress = false; found[opt2] = true;
					}
					else if (found[opt2])
					{
						values[j] = opt1; cantProgress = false; found[opt1] = true;
					}
				}
				j = i + 1;
				if (j < N && values[j] == -1)
				{
					int v = ownQuery(i, j);
					int opt1 = values[i] + v;
					int opt2 = values[i] - v;
					if (opt1 <= 1)
					{
						values[j] = opt2; cantProgress = false; found[opt2] = true;
					}
					else if (opt2 <= 1)
					{
						values[j] = opt1; cantProgress = false; found[opt1] = true;
					}
					else if (opt1 >= N)
					{
						values[j] = opt2; cantProgress = false; found[opt2] = true;
					}
					else if (opt2 >= N)
					{
						values[j] = opt1; cantProgress = false; found[opt1] = true;
					}
					else if (found[opt1])
					{
						values[j] = opt2; cantProgress = false; found[opt2] = true;
					}
					else if (found[opt2])
					{
						values[j] = opt1; cantProgress = false; found[opt1] = true;
					}
				}
			}
		}
		if (cantProgress) break;
	}
	while (!found.all())
	{
		cantProgress = true;
		for (int i = 1; i <= N; i++)
		{
			if (values[i] != -1)
			{
				int j = i - 1;
				if (j > 0 && values[j] == -1)
				{
					int v = ownQuery(j, i);
					int opt1 = values[i] + v;
					int opt2 = values[i] - v;
					if (opt1 <= 1)
					{
						values[j] = opt2; cantProgress = false; found[opt2] = true;
					}
					else if (opt2 <= 1)
					{
						values[j] = opt1; cantProgress = false; found[opt1] = true;
					}
					else if (opt1 >= N)
					{
						values[j] = opt2; cantProgress = false; found[opt2] = true;
					}
					else if (opt2 >= N)
					{
						values[j] = opt1; cantProgress = false; found[opt1] = true;
					}
					else if (found[opt1])
					{
						values[j] = opt2; cantProgress = false; found[opt2] = true;
					}
					else if (found[opt2])
					{
						values[j] = opt1; cantProgress = false; found[opt1] = true;
					}
					else
					{
						int v2 = query(j, j + 2);
						int ma = max(values[j + 2], values[i]);
						int mi = min(values[j + 2], values[i]);
						int v2Opt1 = max(opt1, ma) - min(opt1, mi);
						int v2Opt2 = max(opt2, ma) - min(opt2, mi);
						if (v2 == v2Opt1 && v2 != v2Opt2)
						{
							cantProgress = false;
							found[opt1] = true;
							values[j] = opt1;
						}
						else if (v2 == v2Opt2 && v2 != v2Opt1)
						{
							cantProgress = false;
							found[opt2] = true;
							values[j] = opt2;
						}
						else
						{
							cerr << "I didn't consider this to be possible. Please take a look!";
							throw;
						}
					}
				}
				j = i + 1;
				if (j <= N && values[j] == -1)
				{
					int v = ownQuery(i, j);
					int opt1 = values[i] + v;
					int opt2 = values[i] - v;
					if (opt1 <= 1)
					{
						values[j] = opt2; cantProgress = false; found[opt2] = true;
					}
					else if (opt2 <= 1)
					{
						values[j] = opt1; cantProgress = false; found[opt1] = true;
					}
					else if (opt1 >= N)
					{
						values[j] = opt2; cantProgress = false; found[opt2] = true;
					}
					else if (opt2 >= N)
					{
						values[j] = opt1; cantProgress = false; found[opt1] = true;
					}
					else if (found[opt1])
					{
						values[j] = opt2; cantProgress = false; found[opt2] = true;
					}
					else if (found[opt2])
					{
						values[j] = opt1; cantProgress = false; found[opt1] = true;
					}
					else
					{
						int v2 = ownQuery(j - 2, j);
						int ma = max(values[j - 2], values[i]);
						int mi = min(values[j - 2], values[i]);
						int v2Opt1 = max(opt1, ma) - min(opt1, mi);
						int v2Opt2 = max(opt2, ma) - min(opt2, mi);
						if (v2 == v2Opt1 && v2 != v2Opt2)
						{
							cantProgress = false;
							found[opt1] = true;
							values[j] = opt1;
						}
						else if (v2 == v2Opt2 && v2 != v2Opt1)
						{
							cantProgress = false;
							found[opt2] = true;
							values[j] = opt2;
						}
						else
						{
							cerr << "I didn't consider this to be possible. Please take a look!";
							throw;
						}
					}
				}
			}
		}
		if (cantProgress)
		{
			cerr << "I didn't consider this to be possible. Please take a look!";
			throw;
		}
	}
	for (int i = 1; i <= N; i++)
	{
		if (values[i] == -1) throw;
		answer(i, values[i]);
	}

}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 1 ms 208 KB Output is correct
4 Correct 1 ms 208 KB Output is correct
5 Correct 1 ms 208 KB Output is correct
6 Correct 1 ms 208 KB Output is correct
7 Correct 2 ms 208 KB Output is correct
8 Correct 1 ms 208 KB Output is correct
9 Correct 1 ms 208 KB Output is correct
10 Correct 1 ms 208 KB Output is correct
11 Correct 2 ms 208 KB Output is correct
12 Correct 2 ms 208 KB Output is correct
13 Correct 2 ms 208 KB Output is correct
14 Correct 1 ms 208 KB Output is correct
15 Correct 2 ms 296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 1 ms 208 KB Output is correct
4 Correct 1 ms 208 KB Output is correct
5 Correct 1 ms 208 KB Output is correct
6 Correct 1 ms 208 KB Output is correct
7 Correct 2 ms 208 KB Output is correct
8 Correct 1 ms 208 KB Output is correct
9 Correct 1 ms 208 KB Output is correct
10 Correct 1 ms 208 KB Output is correct
11 Correct 2 ms 208 KB Output is correct
12 Correct 2 ms 208 KB Output is correct
13 Correct 2 ms 208 KB Output is correct
14 Correct 1 ms 208 KB Output is correct
15 Correct 2 ms 296 KB Output is correct
16 Correct 3 ms 208 KB Output is correct
17 Correct 6 ms 304 KB Output is correct
18 Correct 10 ms 336 KB Output is correct
19 Correct 11 ms 352 KB Output is correct
20 Correct 13 ms 336 KB Output is correct
21 Correct 11 ms 332 KB Output is correct
22 Correct 10 ms 476 KB Output is correct
23 Correct 8 ms 336 KB Output is correct
24 Correct 10 ms 296 KB Output is correct
25 Correct 9 ms 300 KB Output is correct
26 Correct 10 ms 356 KB Output is correct
27 Correct 12 ms 336 KB Output is correct
28 Correct 12 ms 328 KB Output is correct
29 Correct 12 ms 336 KB Output is correct
30 Correct 11 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 1 ms 208 KB Output is correct
4 Correct 1 ms 208 KB Output is correct
5 Correct 1 ms 208 KB Output is correct
6 Correct 1 ms 208 KB Output is correct
7 Correct 2 ms 208 KB Output is correct
8 Correct 1 ms 208 KB Output is correct
9 Correct 1 ms 208 KB Output is correct
10 Correct 1 ms 208 KB Output is correct
11 Correct 2 ms 208 KB Output is correct
12 Correct 2 ms 208 KB Output is correct
13 Correct 2 ms 208 KB Output is correct
14 Correct 1 ms 208 KB Output is correct
15 Correct 2 ms 296 KB Output is correct
16 Correct 3 ms 208 KB Output is correct
17 Correct 6 ms 304 KB Output is correct
18 Correct 10 ms 336 KB Output is correct
19 Correct 11 ms 352 KB Output is correct
20 Correct 13 ms 336 KB Output is correct
21 Correct 11 ms 332 KB Output is correct
22 Correct 10 ms 476 KB Output is correct
23 Correct 8 ms 336 KB Output is correct
24 Correct 10 ms 296 KB Output is correct
25 Correct 9 ms 300 KB Output is correct
26 Correct 10 ms 356 KB Output is correct
27 Correct 12 ms 336 KB Output is correct
28 Correct 12 ms 328 KB Output is correct
29 Correct 12 ms 336 KB Output is correct
30 Correct 11 ms 344 KB Output is correct
31 Correct 27 ms 348 KB Output is correct
32 Correct 32 ms 516 KB Output is correct
33 Correct 58 ms 660 KB Output is correct
34 Correct 71 ms 676 KB Output is correct
35 Correct 55 ms 676 KB Output is correct
36 Correct 39 ms 540 KB Output is correct
37 Correct 68 ms 568 KB Output is correct
38 Correct 47 ms 596 KB Output is correct
39 Correct 54 ms 612 KB Output is correct
40 Correct 47 ms 644 KB Output is correct
41 Correct 66 ms 644 KB Output is correct
42 Correct 32 ms 484 KB Output is correct
43 Correct 67 ms 500 KB Output is correct
44 Correct 38 ms 584 KB Output is correct
45 Correct 59 ms 596 KB Output is correct
46 Correct 48 ms 676 KB Output is correct
47 Correct 55 ms 688 KB Output is correct
48 Correct 56 ms 656 KB Output is correct
49 Correct 72 ms 664 KB Output is correct
50 Correct 56 ms 664 KB Output is correct
51 Correct 39 ms 608 KB Output is correct
52 Correct 70 ms 608 KB Output is correct
53 Correct 50 ms 592 KB Output is correct
54 Correct 47 ms 628 KB Output is correct
55 Correct 54 ms 660 KB Output is correct
56 Correct 52 ms 588 KB Output is correct
57 Correct 64 ms 684 KB Output is correct
58 Correct 75 ms 588 KB Output is correct
59 Correct 56 ms 604 KB Output is correct
60 Correct 54 ms 676 KB Output is correct