제출 #1351461

#제출 시각아이디문제언어결과실행 시간메모리
1351461nerrrminDeveloper (BOI25_dev)C++20
12 / 100
62 ms57592 KiB
#include<bits/stdc++.h>
#define endl '\n'
#define pb push_back
using namespace std;
const int maxn = 5e5 + 10;
void speed()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}
int n, a[maxn];
long long dp[maxn][12][3];
int main()
{
    speed();
    cin >> n;
    for (int i = 1; i <= n; ++ i)
        cin >> a[i];
  for (int i = 1; i <= n; ++ i)
  {
      for (int j = 0; j <= 11; ++ j)
      {
          for (int t = 0; t <= 2; ++ t)
            dp[i][j][t] = 1e9;
      }
  }
  long long best = 1e9;
   for (int i = 0; i <= 11; ++ i)
   {
       dp[1][i][0] = abs(i - a[1]);
   }
   for (int i = 1; i < n; ++ i)
   {
       for (int h = 0; h <= 11; ++ h)
       {
           for (int t = 0; t <= 2; ++ t)
           {
               dp[i+1][h][t] = min(dp[i+1][h][t], dp[i][h][t] + abs(a[i+1] - h));
               /// otivame po-malko
               if(t != 2)
               {
                   for (int h2 = h-1; h2 >= 0; -- h2)
                   {
                       dp[i+1][h2][2] = min(dp[i+1][h2][2], dp[i][h][t] + abs(a[i+1] - h2));
                   }
               }
               if(t != 1)
               {
                   for (int h2 = h+1; h2 <=11; ++ h2)
                   {
                       dp[i+1][h2][1] = min(dp[i+1][h2][1], dp[i][h][t] + abs(a[i+1] - h2));
                   }
               }
           }
       }
   }
   for (int h = 0; h <= 11; ++ h)
   {
       for (int t = 0; t < 3; ++ t)
        best = min(best, dp[n][h][t]);
   }
/*for (int i = 1; i <= n; ++ i)
{
    for (int j = 0; j<= 11; ++ j)
    {
        for (int t = 0; t < 3; ++ t)
            cout << i << " " << j << " " << t << " -> " << dp[i][j][t] << endl;
        cout << endl;
    }
}*/
    cout << best << endl;
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...