結果

問題 No.2029 Swap Min Max Min
ユーザー tnakao0123tnakao0123
提出日時 2022-08-13 21:36:24
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 28 ms / 2,000 ms
コード長 1,228 bytes
コンパイル時間 280 ms
コンパイル使用メモリ 42,632 KB
実行使用メモリ 8,616 KB
最終ジャッジ日時 2023-10-25 06:19:13
合計ジャッジ時間 4,511 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,196 KB
testcase_01 AC 2 ms
5,196 KB
testcase_02 AC 2 ms
5,196 KB
testcase_03 AC 16 ms
8,256 KB
testcase_04 AC 6 ms
5,468 KB
testcase_05 AC 10 ms
5,372 KB
testcase_06 AC 18 ms
5,636 KB
testcase_07 AC 18 ms
8,320 KB
testcase_08 AC 24 ms
8,496 KB
testcase_09 AC 8 ms
5,676 KB
testcase_10 AC 6 ms
5,260 KB
testcase_11 AC 10 ms
5,884 KB
testcase_12 AC 13 ms
5,456 KB
testcase_13 AC 25 ms
8,536 KB
testcase_14 AC 26 ms
5,880 KB
testcase_15 AC 27 ms
8,576 KB
testcase_16 AC 26 ms
5,860 KB
testcase_17 AC 26 ms
8,528 KB
testcase_18 AC 28 ms
8,616 KB
testcase_19 AC 26 ms
5,868 KB
testcase_20 AC 27 ms
5,880 KB
testcase_21 AC 26 ms
5,872 KB
testcase_22 AC 25 ms
5,860 KB
testcase_23 AC 26 ms
8,616 KB
testcase_24 AC 26 ms
8,616 KB
testcase_25 AC 25 ms
5,900 KB
testcase_26 AC 25 ms
5,900 KB
testcase_27 AC 2 ms
5,196 KB
testcase_28 AC 2 ms
5,196 KB
testcase_29 AC 2 ms
5,196 KB
testcase_30 AC 2 ms
5,196 KB
testcase_31 AC 2 ms
5,196 KB
testcase_32 AC 3 ms
5,200 KB
testcase_33 AC 2 ms
5,196 KB
testcase_34 AC 3 ms
5,196 KB
testcase_35 AC 2 ms
5,196 KB
testcase_36 AC 2 ms
5,196 KB
testcase_37 AC 2 ms
5,196 KB
testcase_38 AC 23 ms
8,504 KB
testcase_39 AC 24 ms
5,868 KB
testcase_40 AC 22 ms
5,788 KB
testcase_41 AC 24 ms
8,544 KB
testcase_42 AC 24 ms
8,548 KB
testcase_43 AC 23 ms
5,848 KB
testcase_44 AC 26 ms
8,608 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 2029.cc:  No.2029 Swap Min Max Min - yukicoder
 */

#include<cstdio>
#include<algorithm>
 
using namespace std;

/* constant */

const int MAX_N = 200000;
const long long LINF = 1LL << 62;

/* typedef */

typedef long long ll;

/* global variables */

int as[MAX_N], fs[MAX_N][2];
ll dp[MAX_N + 1][2];

/* subroutines */

inline void setmin(ll &a, ll b) { if (a > b) a = b; }

/* main */

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) scanf("%d", as + i);

  int x = n / 2;
  for (int i = 0, j = 0, k = 0; i < n; i++) {
    if (as[i] > x) j++;
    else {
      fs[k][0] = abs(j - k), fs[k][1] = abs(j - 1 - k);
      k++;
    }
  }
  //for (int i = 0; i < x; i++) printf("%d,%d ", fs[i][0], fs[i][1]);
  //putchar('\n');

  if (n & 1) {
    ll y = 0;
    for (int i = 0; i < x; i++) y += fs[i][1];
    printf("%d %lld\n", x, y);
  }
  else {
    for (int i = 0; i <= x; i++) dp[i][0] = dp[i][1] = LINF;
    dp[0][0] = dp[0][1] = 0;

    for (int i = 0; i < x; i++) {
      setmin(dp[i + 1][0], min(dp[i][0], dp[i][1]) + fs[i][0]);
      setmin(dp[i + 1][1], dp[i][1] + fs[i][1]);
    }

    ll y = min(dp[x][0], dp[x][1]);
    printf("%d %lld\n", x, y);
  }
  return 0;
}
0