Submission #330028

# Submission time Handle Problem Language Result Execution time Memory
330028 2020-11-23T13:09:26 Z dolphingarlic Circus (Balkan15_CIRCUS) C++14
100 / 100
769 ms 10596 KB
#include "circus.h"

#include <bits/stdc++.h>
using namespace std;

int n, m;
vector<int> p, mn;
deque<pair<int, int>> to_left, to_right;

void init(int N, int M, int P[]) {
	for (int i = 0; i < N; i++) p.push_back(P[i]);
	sort(p.begin(), p.end());
	p.erase(unique(p.begin(), p.end()), p.end());
	n = p.size(), m = M;

	mn = vector<int>(n, INT_MAX);
	for (int i = n - 1; ~i; i--) {
		int dist = m - p[i];
		auto lb = lower_bound(to_left.begin(), to_left.end(), make_pair(p[i], 0));
		if (lb != to_left.end()) dist = min(dist, lb->second - p[i]);
		mn[i] = min(mn[i], dist);
		while (to_left.size() && p[i] - mn[i] >= to_left[0].first)
			to_left.pop_front();
		to_left.push_front({p[i] - mn[i], p[i]});
	}
	to_left.clear();

	for (int i = 0; i < n; i++) {
		int dist = m - p[i];
		auto ub = upper_bound(to_right.begin(), to_right.end(), make_pair(p[i], INT_MAX));
		if (ub != to_right.begin()) dist = min(dist, p[i] - prev(ub)->second);
		mn[i] = min(mn[i], dist);
		while (to_right.size() && p[i] + mn[i] <= to_right.back().first)
			to_right.pop_back();
		to_right.push_back({p[i] + mn[i], p[i]});
	}
	to_right.clear();

	for (int i = n - 1; ~i; i--) {
		int dist = m - p[i];
		auto lb = lower_bound(to_left.begin(), to_left.end(), make_pair(p[i], 0));
		if (lb != to_left.end()) dist = min(dist, lb->second - p[i]);
		mn[i] = min(mn[i], dist);
		while (to_left.size() && p[i] - mn[i] >= to_left[0].first)
			to_left.pop_front();
		to_left.push_front({p[i] - mn[i], p[i]});
	}
	to_left.clear();

	for (int i = 0; i < n; i++)
		if (!to_left.size() || p[i] - mn[i] > to_left.back().first)
			to_left.push_back({p[i] - mn[i], p[i]});
	for (int i = n - 1; ~i; i--)
		if (!to_right.size() || p[i] + mn[i] < to_right[0].first)
			to_right.push_front({p[i] + mn[i], p[i]});
}

int minLength(int D) {
	int ans = m - D;
	auto lb = lower_bound(to_left.begin(), to_left.end(), make_pair(D, 0));
	if (lb != to_left.end()) ans = min(ans, lb->second - D);
	auto ub = upper_bound(to_right.begin(), to_right.end(), make_pair(D, INT_MAX));
	if (ub != to_right.begin()) ans = min(ans, D - prev(ub)->second);
	return ans;
}

Compilation message

grader.cpp: In function 'int main()':
grader.cpp:14:12: warning: unused variable 'max_code' [-Wunused-variable]
   14 |  long long max_code;
      |            ^~~~~~~~
grader.cpp:16:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   16 |  scanf("%d%d", &N, &M);
      |  ~~~~~^~~~~~~~~~~~~~~~
grader.cpp:18:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   18 |   scanf("%d", &P[i]);
      |   ~~~~~^~~~~~~~~~~~~
grader.cpp:21:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   21 |  scanf("%d", &Q);
      |  ~~~~~^~~~~~~~~~
grader.cpp:23:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   23 |   scanf("%d", &d);
      |   ~~~~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 49 ms 2672 KB Output is correct
2 Correct 51 ms 2532 KB Output is correct
3 Correct 49 ms 2660 KB Output is correct
4 Correct 49 ms 2660 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 0 ms 364 KB Output is correct
3 Correct 0 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 0 ms 364 KB Output is correct
3 Correct 0 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 2 ms 364 KB Output is correct
6 Correct 2 ms 364 KB Output is correct
7 Correct 2 ms 364 KB Output is correct
8 Correct 2 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 0 ms 364 KB Output is correct
3 Correct 0 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 2 ms 364 KB Output is correct
6 Correct 2 ms 364 KB Output is correct
7 Correct 2 ms 364 KB Output is correct
8 Correct 2 ms 364 KB Output is correct
9 Correct 479 ms 8556 KB Output is correct
10 Correct 443 ms 4972 KB Output is correct
11 Correct 433 ms 4332 KB Output is correct
12 Correct 482 ms 9068 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 49 ms 2672 KB Output is correct
2 Correct 51 ms 2532 KB Output is correct
3 Correct 49 ms 2660 KB Output is correct
4 Correct 49 ms 2660 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 0 ms 364 KB Output is correct
7 Correct 0 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 2 ms 364 KB Output is correct
10 Correct 2 ms 364 KB Output is correct
11 Correct 2 ms 364 KB Output is correct
12 Correct 2 ms 364 KB Output is correct
13 Correct 55 ms 2660 KB Output is correct
14 Correct 51 ms 2648 KB Output is correct
15 Correct 49 ms 2660 KB Output is correct
16 Correct 50 ms 2660 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 49 ms 2672 KB Output is correct
2 Correct 51 ms 2532 KB Output is correct
3 Correct 49 ms 2660 KB Output is correct
4 Correct 49 ms 2660 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 0 ms 364 KB Output is correct
7 Correct 0 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 2 ms 364 KB Output is correct
10 Correct 2 ms 364 KB Output is correct
11 Correct 2 ms 364 KB Output is correct
12 Correct 2 ms 364 KB Output is correct
13 Correct 479 ms 8556 KB Output is correct
14 Correct 443 ms 4972 KB Output is correct
15 Correct 433 ms 4332 KB Output is correct
16 Correct 482 ms 9068 KB Output is correct
17 Correct 55 ms 2660 KB Output is correct
18 Correct 51 ms 2648 KB Output is correct
19 Correct 49 ms 2660 KB Output is correct
20 Correct 50 ms 2660 KB Output is correct
21 Correct 761 ms 7780 KB Output is correct
22 Correct 747 ms 7788 KB Output is correct
23 Correct 769 ms 9316 KB Output is correct
24 Correct 761 ms 10596 KB Output is correct