-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path12026.cpp
More file actions
58 lines (35 loc) · 1009 Bytes
/
12026.cpp
File metadata and controls
58 lines (35 loc) · 1009 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <queue>
using namespace std;
char arr[1001];
int dp[1001];
const int INF = 123456789;
bool isValid(char curr,char next) {
if (curr == 'B' && next == 'O')return true;
if (curr == 'O' && next == 'J')return true;
if (curr == 'J' && next == 'B')return true;
return false;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
fill(&dp[0], &dp[1001], INF);
int N; cin >> N;
for (int i = 1; i <= N; i++)cin >> arr[i];
dp[1] = 0;
for (int i = 1; i <= N; i++) {
for (int j = i + 1; j <= N; j++) {
if (!isValid(arr[i], arr[j])) continue;
if (dp[j] > dp[i] + (j - i) * (j - i)) {
dp[j] = dp[i] + (j - i) * (j - i);
}
}
}
if (dp[N] == INF)cout << "-1";
else cout << dp[N];
}