Submission #246331

# Submission time Handle Problem Language Result Execution time Memory
246331 2020-07-08T17:22:38 Z tonowak Global Warming (CEOI18_glo) C++14
100 / 100
244 ms 7544 KB
#include <bits/stdc++.h> // Tomasz Nowak
using namespace std;     // XIII LO Szczecin
using LL = long long;    // Poland
#define FOR(i, l, r) for(int i = (l); i <= (r); ++i)
#define REP(i, n) FOR(i, 0, (n) - 1)
template<class T> int size(T &&x) {
	return int(x.size());
}
template<class A, class B> ostream& operator<<(ostream &out, const pair<A, B> &p) {
	return out << '(' << p.first << ", " << p.second << ')';
}
template<class T> auto operator<<(ostream &out, T &&x) -> decltype(x.begin(), out) {
	out << '{';
	for(auto it = x.begin(); it != x.end(); ++it)
		out << *it << (it == prev(x.end()) ? "" : ", ");
	return out << '}';
}
void dump() {}
template<class T, class... Args> void dump(T &&x, Args... args) {
	cerr << x << ";  ";
	dump(args...);
}
#ifdef DEBUG
  struct Nl{~Nl(){cerr << '\n';}};
# define debug(x...) cerr << (strcmp(#x, "") ? #x ":  " : ""), dump(x), Nl(), cerr << ""
#else
# define debug(...) 0 && cerr
#endif
mt19937_64 rng(0);
int rd(int l, int r) {
	return uniform_int_distribution<int>(l, r)(rng);
}
// end of templates

struct Tree {
	int sz = 1;
	vector<int> tree;

	Tree(int n) {
		while(sz < n)
			sz *= 2;
		tree.resize(2 * sz);
	}

	void set(int v, int val) {
		tree[v += sz] = val;
		while(v /= 2)
			tree[v] = max(tree[2 * v], tree[2 * v + 1]);
	}

	int get_max(int l, int r) {
		if(l > r)
			return 0;
		int ret = max(tree[l += sz], tree[r += sz]);
		while(l / 2 != r / 2) {
			if(~l & 1)
				ret = max(ret, tree[l + 1]);
			if(r & 1)
				ret = max(ret, tree[r - 1]);
			l /= 2;
			r /= 2;
		}
		return ret;
	}
};

vector<int> sorted_values;

int get_id(int x) {
	return int(lower_bound(sorted_values.begin(), sorted_values.end(), x) - sorted_values.begin());
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int n, x;
	cin >> n >> x;
	vector<int> t(n);
	for(int &ti : t)
		cin >> ti;
	debug(n, x, t);

	sorted_values = t;
	sort(sorted_values.begin(), sorted_values.end());
	sorted_values.erase(unique(sorted_values.begin(), sorted_values.end()), sorted_values.end());
	debug(sorted_values);

	vector<int> answer_left(n), answer_right(n);
	Tree tree(size(sorted_values));
	for(int i = n - 1; i >= 0; --i) {
		int new_val = get_id(t[i]);
		answer_right[i] = tree.get_max(new_val + 1, size(sorted_values) - 1) + 1;
		tree.set(new_val, answer_right[i]);
	}

	fill(tree.tree.begin(), tree.tree.end(), 0);
	REP(i, n) {
		int new_val = get_id(t[i]);
		// int lowest = int(prev(lower_bound(sorted_values.begin(), sorted_values.end(), t[i] + x)) - sorted_values.begin());
		int lowest = get_id(t[i] + x) - 1;
		debug(i, new_val, lowest);
		answer_left[i] = tree.get_max(0, lowest);
		tree.set(new_val, tree.get_max(0, new_val - 1) + 1);
	}
	debug(answer_left, answer_right);
	int answer = 0;
	REP(i, n)
		answer = max(answer, answer_left[i] + answer_right[i]);
	cout << answer << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 4 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 4 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 4 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 4 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 4 ms 384 KB Output is correct
14 Correct 4 ms 384 KB Output is correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 4 ms 384 KB Output is correct
17 Correct 5 ms 384 KB Output is correct
18 Correct 5 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 4 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 4 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 4 ms 384 KB Output is correct
14 Correct 4 ms 384 KB Output is correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 4 ms 384 KB Output is correct
17 Correct 5 ms 384 KB Output is correct
18 Correct 5 ms 384 KB Output is correct
19 Correct 5 ms 512 KB Output is correct
20 Correct 5 ms 384 KB Output is correct
21 Correct 5 ms 384 KB Output is correct
22 Correct 5 ms 384 KB Output is correct
23 Correct 5 ms 384 KB Output is correct
24 Correct 5 ms 384 KB Output is correct
25 Correct 5 ms 384 KB Output is correct
26 Correct 5 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 221 ms 6136 KB Output is correct
2 Correct 239 ms 6008 KB Output is correct
3 Correct 244 ms 6008 KB Output is correct
4 Correct 238 ms 6136 KB Output is correct
5 Correct 107 ms 4984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 52 ms 2048 KB Output is correct
2 Correct 52 ms 2100 KB Output is correct
3 Correct 53 ms 2176 KB Output is correct
4 Correct 28 ms 1664 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 34 ms 1664 KB Output is correct
7 Correct 46 ms 2176 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 91 ms 3584 KB Output is correct
2 Correct 95 ms 3968 KB Output is correct
3 Correct 191 ms 7416 KB Output is correct
4 Correct 111 ms 5624 KB Output is correct
5 Correct 63 ms 3456 KB Output is correct
6 Correct 113 ms 6776 KB Output is correct
7 Correct 114 ms 7168 KB Output is correct
8 Correct 81 ms 3968 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 4 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 4 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 4 ms 384 KB Output is correct
14 Correct 4 ms 384 KB Output is correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 4 ms 384 KB Output is correct
17 Correct 5 ms 384 KB Output is correct
18 Correct 5 ms 384 KB Output is correct
19 Correct 5 ms 512 KB Output is correct
20 Correct 5 ms 384 KB Output is correct
21 Correct 5 ms 384 KB Output is correct
22 Correct 5 ms 384 KB Output is correct
23 Correct 5 ms 384 KB Output is correct
24 Correct 5 ms 384 KB Output is correct
25 Correct 5 ms 384 KB Output is correct
26 Correct 5 ms 384 KB Output is correct
27 Correct 221 ms 6136 KB Output is correct
28 Correct 239 ms 6008 KB Output is correct
29 Correct 244 ms 6008 KB Output is correct
30 Correct 238 ms 6136 KB Output is correct
31 Correct 107 ms 4984 KB Output is correct
32 Correct 52 ms 2048 KB Output is correct
33 Correct 52 ms 2100 KB Output is correct
34 Correct 53 ms 2176 KB Output is correct
35 Correct 28 ms 1664 KB Output is correct
36 Correct 5 ms 384 KB Output is correct
37 Correct 34 ms 1664 KB Output is correct
38 Correct 46 ms 2176 KB Output is correct
39 Correct 91 ms 3584 KB Output is correct
40 Correct 95 ms 3968 KB Output is correct
41 Correct 191 ms 7416 KB Output is correct
42 Correct 111 ms 5624 KB Output is correct
43 Correct 63 ms 3456 KB Output is correct
44 Correct 113 ms 6776 KB Output is correct
45 Correct 114 ms 7168 KB Output is correct
46 Correct 81 ms 3968 KB Output is correct
47 Correct 105 ms 4004 KB Output is correct
48 Correct 108 ms 3836 KB Output is correct
49 Correct 224 ms 7512 KB Output is correct
50 Correct 104 ms 5624 KB Output is correct
51 Correct 103 ms 4600 KB Output is correct
52 Correct 142 ms 6784 KB Output is correct
53 Correct 121 ms 6776 KB Output is correct
54 Correct 124 ms 7544 KB Output is correct
55 Correct 182 ms 7544 KB Output is correct