Submission #4676658


Source Code Expand

Copy
#define DEBUG 0

/**
 * File    : D.cpp
 * Author  : Kazune Takahashi
 * Created : 3/23/2019, 10:52:02 PM
 * Powered by Visual Studio Code
 */

#include <iostream>
#include <iomanip>   // << fixed << setprecision(xxx)
#include <algorithm> // do { } while ( next_permutation(A, A+xxx) ) ;
#include <vector>
#include <string> // to_string(nnn) // substr(m, n) // stoi(nnn)
#include <complex>
#include <tuple>
#include <queue>
#include <stack>
#include <map> // if (M.find(key) != M.end()) { }
#include <set>
#include <functional>
#include <random> // auto rd = bind(uniform_int_distribution<int>(0, 9), mt19937(10101010));
#include <chrono> // std::chrono::system_clock::time_point start_time, end_time;
// start = std::chrono::system_clock::now();
// double elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end_time-start_time).count();
#include <cctype>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
using namespace std;

typedef long long ll;

// const int dx[4] = {1, 0, -1, 0};
// const int dy[4] = {0, 1, 0, -1};

// const int C = 1e6+10;
const ll infty = 10000000000000007;

ll N, A, B;
int p[5010];
ll R[5010][5010];
ll L[5010][5010];
ll cost[5010][5010];

ll calc(int l, int r)
{
  if (cost[l][r] < infty)
  {
    return cost[l][r];
  }
  else if (l + 1 == r)
  {
    cost[l][r] = 0;
  }
  else
  {
    cost[l][r] = min(A, B * max(0LL, R[l][r - 1])) + calc(l + 1, r);
    cost[l][r] = min(cost[l][r], min(B, A * max(0LL, L[r - 1][l])) + calc(l, r - 1));
  }
#if DEBUG == 1
  cerr << "cost[" << l << "][" << r << "] = " << cost[l][r] << endl;
#endif
  return cost[l][r];
}

int main()
{
  cin >> N >> A >> B;
  for (auto i = 0; i < N; i++)
  {
    cin >> p[i];
    p[i]--;
  }
  fill(&cost[0][0], &cost[0][0] + 5010 * 5010, infty);
  for (auto i = 0; i < N; i++)
  {
    R[i][i] = 0;
    for (auto j = i + 1; j < N; j++)
    {
      R[i][j] = R[i][j - 1];
      if (p[i] > p[j])
      {
        R[i][j]++;
      }
#if DEBUG == 1
      cerr << "R[" << i << "][" << j << "] = " << R[i][j] << endl;
#endif
    }
  }
  for (auto i = 0; i < N; i++)
  {
    L[i][i] = 0;
    for (auto j = i - 1; j >= 0; j--)
    {
      L[i][j] = L[i][j + 1];
      if (p[i] < p[j])
      {
        L[i][j]++;
      }
#if DEBUG == 1
      cerr << "L[" << i << "][" << j << "] = " << L[i][j] << endl;
#endif
    }
  }
  cout << calc(0, N) << endl;
}

Submission Info

Submission Time
Task D - Rotation Sort
User kazunetakahashi
Language C++14 (GCC 5.4.1)
Score 0
Code Size 2481 Byte
Status
Exec Time 525 ms
Memory 588416 KB

Test Cases

Set Name Score / Max Score Test Cases
Sample 0 / 0 0_00.txt, 0_01.txt, 0_02.txt, 0_03.txt, 0_04.txt
All 0 / 1000 0_00.txt, 0_01.txt, 0_02.txt, 0_03.txt, 0_04.txt, 1_00.txt, 1_01.txt, 1_02.txt, 1_03.txt, 1_04.txt, 1_05.txt, 1_06.txt, 1_07.txt, 1_08.txt, 1_09.txt, 1_10.txt, 1_11.txt, 1_12.txt, 1_13.txt, 1_14.txt, 1_15.txt, 1_16.txt, 1_17.txt, 1_18.txt, 1_19.txt, 1_20.txt, 1_21.txt, 1_22.txt, 1_23.txt, 1_24.txt, 1_25.txt, 1_26.txt, 1_27.txt, 1_28.txt, 1_29.txt, 1_30.txt, 1_31.txt, 1_32.txt, 1_33.txt, 1_34.txt, 1_35.txt, 1_36.txt, 1_37.txt, 1_38.txt, 1_39.txt
Case Name Status Exec Time Memory
0_00.txt 50 ms 198912 KB
0_01.txt 49 ms 198912 KB
0_02.txt 50 ms 198912 KB
0_03.txt 50 ms 198912 KB
0_04.txt 49 ms 198912 KB
1_00.txt 49 ms 198912 KB
1_01.txt 473 ms 588416 KB
1_02.txt 488 ms 588416 KB
1_03.txt 522 ms 588416 KB
1_04.txt 476 ms 588416 KB
1_05.txt 475 ms 588416 KB
1_06.txt 475 ms 588416 KB
1_07.txt 474 ms 588416 KB
1_08.txt 511 ms 586368 KB
1_09.txt 516 ms 588416 KB
1_10.txt 505 ms 584320 KB
1_11.txt 506 ms 584320 KB
1_12.txt 490 ms 576128 KB
1_13.txt 519 ms 588416 KB
1_14.txt 513 ms 586368 KB
1_15.txt 518 ms 588416 KB
1_16.txt 517 ms 588416 KB
1_17.txt 522 ms 588416 KB
1_18.txt 511 ms 586368 KB
1_19.txt 511 ms 586368 KB
1_20.txt 507 ms 584320 KB
1_21.txt 519 ms 588416 KB
1_22.txt 515 ms 588416 KB
1_23.txt 514 ms 588416 KB
1_24.txt 513 ms 588416 KB
1_25.txt 525 ms 588416 KB
1_26.txt 497 ms 580224 KB
1_27.txt 516 ms 588416 KB
1_28.txt 515 ms 588416 KB
1_29.txt 517 ms 588416 KB
1_30.txt 508 ms 584320 KB
1_31.txt 499 ms 582272 KB
1_32.txt 518 ms 588416 KB
1_33.txt 521 ms 588416 KB
1_34.txt 499 ms 580224 KB
1_35.txt 514 ms 588416 KB
1_36.txt 486 ms 576128 KB
1_37.txt 510 ms 586368 KB
1_38.txt 521 ms 588416 KB
1_39.txt 521 ms 588416 KB