Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <bits/stdc++.h>
- using namespace std;
- const int maxn = 30005;
- const int INF = 2e9;
- int n, a[maxn];
- int dp[maxn][4];
- int rec(int at, int medal) {
- if(at == n) {
- return 0;
- }
- if(dp[at][medal] != -1) {
- return dp[at][medal];
- }
- int res = INF;
- if(medal == 1) {
- for(int new_medal = 1; new_medal <= 3; new_medal++) {
- int promena = 0;
- if(medal != a[at]) {
- promena = 1;
- }
- res = min(res, rec(at + 1, new_medal) + promena);
- }
- }
- else if(medal == 2) {
- for(int new_medal = 2; new_medal <= 3; new_medal++) {
- int promena = 0;
- if(medal != a[at]) {
- promena = 1;
- }
- res = min(res, rec(at + 1, new_medal) + promena);
- }
- }
- else {
- int promena = 0;
- if(medal != a[at]) {
- promena = 1;
- }
- res = min(res, rec(at + 1, 3) + promena);
- }
- dp[at][medal] = res;
- return res;
- }
- int main()
- {
- cin >> n;
- for(int i = 0; i < n; i++) {
- cin >> a[i];
- }
- memset(dp, -1, sizeof dp);
- int res = INF;
- for(int i = 1; i <= 3; i++) {
- res = min(res, rec(0, i));
- }
- reverse(a, a + n);
- memset(dp, -1, sizeof dp);
- for(int i = 1; i <= 3; i++) {
- res = min(res, rec(0, i));
- }
- cout << res << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment