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 main()
- {
- int n;
- vector<int>v;
- 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]=10000000;
- }
- }
- dp[0][1]=(v[0] != 1);
- dp[0][2]=(v[0] != 2);
- dp[0][3]=(v[0] != 3);
- for(int i=1; i<n; i++){
- for(int j=1; j<4; j++){
- for(int k = 1; k <= j; k++) {
- dp[i][k] = min(dp[i][k], dp[i - 1][j] + (v[i] != k));
- }
- }
- }
- int result=min(dp[n - 1][1], min(dp[n - 1][2], dp[n - 1][3]));
- reverse(v.begin(), v.end());
- for(int i=0; i<n; i++){
- for(int j=0; j<4; j++){
- dp[i][j]=10000000;
- }
- }
- dp[0][1]=(v[0] != 1);
- dp[0][2]=(v[0] != 2);
- dp[0][3]=(v[0] != 3);
- for(int i=1; i<n; i++){
- for(int j=1; j<4; j++){
- for(int k = 1; k <= j; k++) {
- dp[i][k] = min(dp[i][k], dp[i - 1][j] + (v[i] != k));
- }
- }
- }
- result=min(min(result, dp[n - 1][1]), min(dp[n - 1][2], dp[n - 1][3]));
- cout<<result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement