Submission #291156

# Submission time Handle Problem Language Result Execution time Memory
291156 2020-09-04T19:31:39 Z thiago4532 JJOOII 2 (JOI20_ho_t2) C++17
100 / 100
34 ms 8948 KB
#include <bits/stdc++.h>

using namespace std;
const int maxn = 2e5 + 10;
char v[maxn];
int jsuf[maxn], osuf[maxn], isuf[maxn];
int jqtd[maxn], oqtd[maxn], iqtd[maxn];
int jp[maxn], op[maxn], ip[maxn];
vector<int> jv, ov, iv;

int main() {
	memset(jsuf, 0x3f, sizeof jsuf);
	memset(osuf, 0x3f, sizeof osuf);
	memset(isuf, 0x3f, sizeof isuf);

	int n_, k, n=0;
	cin >> n_ >> k;
	bool json = false;
	for (int i = 1; i <= n_; i++) {
		char x;
		cin >> x;
		if (!json && x != 'J') continue;
		json = true;
		v[++n] = x;
	}
	while (n > 0 && v[n] != 'I') n--;
	
	for (int i = 1; i <= n; i++) {
		if (v[i] == 'J') jv.push_back(i), jp[i] = jv.size() - 1;
		if (v[i] == 'O') ov.push_back(i), op[i] = ov.size() - 1;
		if (v[i] == 'I') iv.push_back(i), ip[i] = iv.size() - 1;
	}
	for (int i = n; i >= 1; i--) {
		jqtd[i] = jqtd[i+1];
		oqtd[i] = oqtd[i+1];
		iqtd[i] = iqtd[i+1];
		if (v[i] == 'J') jqtd[i] = jqtd[i+1] + 1;
		if (v[i] == 'O') oqtd[i] = oqtd[i+1] + 1;
		if (v[i] == 'I') iqtd[i] = iqtd[i+1] + 1;
	}
	if (jqtd[1] < k || oqtd[1] < k || iqtd[1] < k) {
		cout << "-1\n";
		return 0;
	}

	for (int i = 1; i <= n; i++) {
		if (v[i] != 'I') continue;
		int j = ip[i] + k - 1;
		if (j >= iv.size()) break;
		j = iv[j];

		isuf[i] = j - i - 1 - k + 2;
	}

	for (int i = n; i >= 1; i--) {
		isuf[i] = min(isuf[i], isuf[i+1] + 1);
	}

	for (int i = 1; i <= n; i++) {
		if (v[i] != 'O') continue;
		int j = op[i] + k - 1;
		if (j >= ov.size()) break;
		j = ov[j];
		
		osuf[i] = j - i - 1 - k + 2;
	}

	for (int i = n; i >= 1; i--) {
		int j = op[i] + k - 1;
		if (v[i] == 'O' && j < ov.size()) osuf[i] = min(osuf[i] + isuf[ov[j]+1], osuf[i+1] + 1);
		else osuf[i] = osuf[i+1] + 1;
	}

	for (int i = 1; i <= n; i++) {
		if (v[i] != 'J') continue;
		int j = jp[i] + k - 1;
		if (j >= jv.size()) break;
		j = jv[j];

		jsuf[i] = j - i - 1 - k + 2;
	}

	for (int i = n; i >= 1; i--) {
		int j = jp[i] + k - 1;
		if (v[i] == 'J' && j < jv.size()) jsuf[i] = min(jsuf[i] + osuf[jv[j]+1], jsuf[i+1]);
		else jsuf[i] = jsuf[i+1];
	}
	/*for (int i = 1; i<=n;i++)
		cout << isuf[i] <<  " \n"[i==n];

	for (int i = 1; i<=n;i++)
		cout << osuf[i] <<  " \n"[i==n];

	for (int i = 1; i<=n;i++)
		cout << jsuf[i] <<  " \n"[i==n];*/

	if (jsuf[1] >= 0x3f3f3f3f) cout << "-1\n";
	else cout << jsuf[1] << "\n";
	return 0;
}

Compilation message

ho_t2.cpp: In function 'int main()':
ho_t2.cpp:49:9: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   49 |   if (j >= iv.size()) break;
      |       ~~^~~~~~~~~~~~
ho_t2.cpp:62:9: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   62 |   if (j >= ov.size()) break;
      |       ~~^~~~~~~~~~~~
ho_t2.cpp:70:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   70 |   if (v[i] == 'O' && j < ov.size()) osuf[i] = min(osuf[i] + isuf[ov[j]+1], osuf[i+1] + 1);
      |                      ~~^~~~~~~~~~~
ho_t2.cpp:77:9: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   77 |   if (j >= jv.size()) break;
      |       ~~^~~~~~~~~~~~
ho_t2.cpp:85:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   85 |   if (v[i] == 'J' && j < jv.size()) jsuf[i] = min(jsuf[i] + osuf[jv[j]+1], jsuf[i+1]);
      |                      ~~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2688 KB Output is correct
2 Correct 2 ms 2688 KB Output is correct
3 Correct 2 ms 2688 KB Output is correct
4 Correct 2 ms 2688 KB Output is correct
5 Correct 2 ms 2688 KB Output is correct
6 Correct 2 ms 2688 KB Output is correct
7 Correct 2 ms 2688 KB Output is correct
8 Correct 2 ms 2688 KB Output is correct
9 Correct 2 ms 2688 KB Output is correct
10 Correct 2 ms 2688 KB Output is correct
11 Correct 2 ms 2688 KB Output is correct
12 Correct 2 ms 2688 KB Output is correct
13 Correct 2 ms 2688 KB Output is correct
14 Correct 2 ms 2688 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2688 KB Output is correct
2 Correct 2 ms 2688 KB Output is correct
3 Correct 2 ms 2688 KB Output is correct
4 Correct 2 ms 2688 KB Output is correct
5 Correct 2 ms 2688 KB Output is correct
6 Correct 2 ms 2688 KB Output is correct
7 Correct 2 ms 2688 KB Output is correct
8 Correct 2 ms 2688 KB Output is correct
9 Correct 2 ms 2688 KB Output is correct
10 Correct 2 ms 2688 KB Output is correct
11 Correct 2 ms 2688 KB Output is correct
12 Correct 2 ms 2688 KB Output is correct
13 Correct 2 ms 2688 KB Output is correct
14 Correct 2 ms 2688 KB Output is correct
15 Correct 3 ms 2816 KB Output is correct
16 Correct 2 ms 2816 KB Output is correct
17 Correct 3 ms 2816 KB Output is correct
18 Correct 2 ms 2816 KB Output is correct
19 Correct 2 ms 2816 KB Output is correct
20 Correct 2 ms 2816 KB Output is correct
21 Correct 2 ms 2816 KB Output is correct
22 Correct 2 ms 2816 KB Output is correct
23 Correct 2 ms 2816 KB Output is correct
24 Correct 3 ms 2944 KB Output is correct
25 Correct 2 ms 2816 KB Output is correct
26 Correct 2 ms 2816 KB Output is correct
27 Correct 2 ms 2816 KB Output is correct
28 Correct 2 ms 2816 KB Output is correct
29 Correct 2 ms 2816 KB Output is correct
30 Correct 2 ms 2816 KB Output is correct
31 Correct 2 ms 2688 KB Output is correct
32 Correct 3 ms 2816 KB Output is correct
33 Correct 3 ms 2732 KB Output is correct
34 Correct 2 ms 2816 KB Output is correct
35 Correct 2 ms 2816 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2688 KB Output is correct
2 Correct 2 ms 2688 KB Output is correct
3 Correct 2 ms 2688 KB Output is correct
4 Correct 2 ms 2688 KB Output is correct
5 Correct 2 ms 2688 KB Output is correct
6 Correct 2 ms 2688 KB Output is correct
7 Correct 2 ms 2688 KB Output is correct
8 Correct 2 ms 2688 KB Output is correct
9 Correct 2 ms 2688 KB Output is correct
10 Correct 2 ms 2688 KB Output is correct
11 Correct 2 ms 2688 KB Output is correct
12 Correct 2 ms 2688 KB Output is correct
13 Correct 2 ms 2688 KB Output is correct
14 Correct 2 ms 2688 KB Output is correct
15 Correct 3 ms 2816 KB Output is correct
16 Correct 2 ms 2816 KB Output is correct
17 Correct 3 ms 2816 KB Output is correct
18 Correct 2 ms 2816 KB Output is correct
19 Correct 2 ms 2816 KB Output is correct
20 Correct 2 ms 2816 KB Output is correct
21 Correct 2 ms 2816 KB Output is correct
22 Correct 2 ms 2816 KB Output is correct
23 Correct 2 ms 2816 KB Output is correct
24 Correct 3 ms 2944 KB Output is correct
25 Correct 2 ms 2816 KB Output is correct
26 Correct 2 ms 2816 KB Output is correct
27 Correct 2 ms 2816 KB Output is correct
28 Correct 2 ms 2816 KB Output is correct
29 Correct 2 ms 2816 KB Output is correct
30 Correct 2 ms 2816 KB Output is correct
31 Correct 2 ms 2688 KB Output is correct
32 Correct 3 ms 2816 KB Output is correct
33 Correct 3 ms 2732 KB Output is correct
34 Correct 2 ms 2816 KB Output is correct
35 Correct 2 ms 2816 KB Output is correct
36 Correct 29 ms 8316 KB Output is correct
37 Correct 32 ms 8940 KB Output is correct
38 Correct 32 ms 8680 KB Output is correct
39 Correct 32 ms 8812 KB Output is correct
40 Correct 31 ms 8948 KB Output is correct
41 Correct 34 ms 8688 KB Output is correct
42 Correct 32 ms 8940 KB Output is correct
43 Correct 21 ms 6652 KB Output is correct
44 Correct 24 ms 7420 KB Output is correct
45 Correct 31 ms 8688 KB Output is correct
46 Correct 31 ms 8668 KB Output is correct
47 Correct 31 ms 8948 KB Output is correct
48 Correct 31 ms 8860 KB Output is correct
49 Correct 21 ms 6772 KB Output is correct
50 Correct 31 ms 8688 KB Output is correct
51 Correct 33 ms 8808 KB Output is correct
52 Correct 28 ms 8444 KB Output is correct
53 Correct 30 ms 8680 KB Output is correct
54 Correct 28 ms 7160 KB Output is correct
55 Correct 25 ms 7160 KB Output is correct
56 Correct 25 ms 7152 KB Output is correct