Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- using namespace std;
- int n;
- int niza[30005];
- int dp[30005][3];
- int rec(int i, int prev_medal) {
- if(i == n) {
- return 0;
- }
- if(dp[i][prev_medal] != -1) {
- return dp[i][prev_medal];
- }
- int result = 2e9;
- for(int j = 0; j <= prev_medal; j++) {
- result = min(result, rec(i + 1, j) + (niza[i] != prev_medal));
- }
- dp[i][prev_medal] = result;
- return result;
- }
- int main()
- {
- cin >> n;
- for(int i = 0; i < n; i++) {
- cin >> niza[i];
- niza[i]--;
- dp[i][0] = dp[i][1] = dp[i][2] = -1;
- }
- int answer = 2e9;
- for(int i = 0; i <= 2; i++) {
- answer = min(answer, rec(0, i));
- }
- for(int i = 0; i < n; i++) {
- dp[i][0] = dp[i][1] = dp[i][2] = -1;
- }
- reverse(niza, niza + n);
- for(int i = 0; i <= 2; i++) {
- answer = min(answer, rec(0, i));
- }
- cout << answer << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement