Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- using namespace std;
- int dp[30005][4];
- int n;
- vector<int>v;
- int rec(int i, int type){
- if(i==n){
- return 0;
- }
- if(dp[i][type]!=-1){
- return(dp[i][type]);
- }
- int result=1000000000;
- if(type==3){
- int greska=0;
- if(v[i]!=type){
- greska=1;
- }
- result=min(result, rec(i+1, 3)+greska);
- result=min(result, rec(i+1, 2)+greska);
- result=min(result, rec(i+1, 1)+greska);
- }
- if(type==2){
- int greska=0;
- if(v[i]!=type){
- greska=1;
- }
- result=min(result, rec(i+1, 2)+greska);
- result=min(result, rec(i+1, 1)+greska);
- }
- if(type==1){
- int greska=0;
- if(v[i]!=type){
- greska=1;
- }
- result=min(result, rec(i+1, 1)+greska);
- }
- dp[i][type]=result;
- return(result);
- }
- int main()
- {
- cin>>n;
- for(int i=0; i<n; i++){
- int a;
- cin>>a;
- v.push_back(a);
- }
- for(int i=0; i<n; i++){
- for(int j=0; j<4; j++){
- dp[i][j]=-1;
- }
- }
- int au=rec(0, 1);
- int srebro=rec(0, 2);
- int bronza=rec(0, 3);
- int rez = min(au, min(srebro, bronza));
- reverse(v.begin(), v.end());
- for(int i=0; i<n; i++){
- for(int j=0; j<4; j++){
- dp[i][j]=-1;
- }
- }
- au=rec(0, 1);
- srebro=rec(0, 2);
- bronza=rec(0, 3);
- rez = min(min(rez, au), min(srebro, bronza));
- cout << rez << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement