結果

問題 No.1371 交換門松列・松
ユーザー 👑 hos.lyrichos.lyric
提出日時 2021-01-29 22:27:25
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 531 ms / 4,000 ms
コード長 5,426 bytes
コンパイル時間 823 ms
コンパイル使用メモリ 118,300 KB
実行使用メモリ 24,464 KB
最終ジャッジ日時 2023-09-04 12:05:18
合計ジャッジ時間 11,343 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,384 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,384 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 492 ms
24,396 KB
testcase_19 AC 490 ms
24,344 KB
testcase_20 AC 387 ms
24,464 KB
testcase_21 AC 381 ms
24,376 KB
testcase_22 AC 383 ms
24,452 KB
testcase_23 AC 527 ms
24,384 KB
testcase_24 AC 527 ms
24,424 KB
testcase_25 AC 529 ms
24,440 KB
testcase_26 AC 531 ms
24,364 KB
testcase_27 AC 527 ms
24,396 KB
testcase_28 AC 528 ms
24,368 KB
testcase_29 AC 529 ms
24,428 KB
testcase_30 AC 529 ms
24,448 KB
testcase_31 AC 526 ms
24,428 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }


void bAdd(T)(T[] bit, int pos, T val)
in {
  assert(0 <= pos && pos < bit.length, "bAdd: 0 <= pos < |bit| must hold");
}
do {
  for (int x = pos; x < bit.length; x |= x + 1) {
    bit[x] += val;
  }
}

// sum of [0, pos)
T bSum(T)(T[] bit, int pos)
in {
  assert(0 <= pos && pos <= bit.length, "bSum: 0 <= pos <= |bit| must hold");
}
do {
  T ret = 0;
  for (int x = pos - 1; x >= 0; x = (x & (x + 1)) - 1) {
    ret += bit[x];
  }
  return ret;
}


bool isKadomatsu(int a, int b, int c) {
  return (((a > b && b < c) || (a < b && b > c)) && a != c);
}
bool isWeakKadomatsu(int a, int b, int c) {
  return ((a > b && b < c) || (a < b && b > c));
}

void main() {
  try {
    for (; ; ) {
      const N = readInt();
      auto A = new int[N];
      foreach (i; 0 .. N) {
        A[i] = readInt() - 1;
      }
      
      if (A[0] > A[1]) {
        foreach (i; 0 .. N) {
          A[i] = N - 1 - A[i];
        }
      }
      
      bool check(int i, int j) {
        const ai = A[i], aj = A[j];
        bool ret = true;
        A[i] = aj;
        foreach (k; i - 2 .. i + 1) if (0 <= k && k + 3 <= N) ret = ret && isWeakKadomatsu(A[k], A[k + 1], A[k + 2]);
        A[i] = ai;
        A[j] = ai;
        foreach (k; j - 2 .. j + 1) if (0 <= k && k + 3 <= N) ret = ret && isWeakKadomatsu(A[k], A[k + 1], A[k + 2]);
        A[j] = aj;
        return ret;
      }
      
      long ans;
      foreach (s; 0 .. 2) foreach (t; 0 .. 2) {
        alias Entry = Tuple!(int, "x", int, "y", int, "type");
        Entry[] es;
        for (int i = s; i < N; i += 2) {
          const y = A[i];
          int x;
          if (s == 0) {
            x = N;
            if (i - 1 >= 0) chmin(x, A[i - 1]);
            if (i + 1 <  N) chmin(x, A[i + 1]);
            assert(x > y);
          } else {
            x = -1;
            if (i - 1 >= 0) chmax(x, A[i - 1]);
            if (i + 1 <  N) chmax(x, A[i + 1]);
            assert(x < y);
          }
          es ~= Entry(x, y, 0);
        }
        for (int i = t; i < N; i += 2) {
          const x = A[i];
          int y;
          if (t == 0) {
            y = N;
            if (i - 1 >= 0) chmin(y, A[i - 1]);
            if (i + 1 <  N) chmin(y, A[i + 1]);
            assert(y > x);
          } else {
            y = -1;
            if (i - 1 >= 0) chmax(y, A[i - 1]);
            if (i + 1 <  N) chmax(y, A[i + 1]);
            assert(y < x);
          }
          es ~= Entry(x, y, 1);
        }
        debug {
          writeln("es = ", es);
        }
        if (s == 0) {
          foreach (ref e; es) {
            e.x *= -1;
          }
        }
        es.sort!((e, f) => ((e.x != f.x) ? (e.x < f.x) : (e.type > f.type)));
        debug {
          writeln("es = ", es);
        }
        auto bit = new long[N];
        long num;
        foreach (ref e; es) {
          if (e.type == 0) {
            bit.bAdd(e.y, 1);
          } else {
            if (t == 0) {
              num += bit.bSum(e.y);
            } else {
              num += bit.bSum(N) - bit.bSum(e.y + 1);
            }
          }
        }
        ans += num;
        debug {
          long brt;
          foreach (i; 0 .. N) foreach (j; 0 .. N) {
            if (i % 2 == s && j % 2 == t) {
              if (check(i, j)) {
                ++brt;
              }
            }
          }
          writeln("brt = ", brt);
          writeln("num = ", num);
          assert(brt == num);
        }
      }
      
      foreach (i; 0 .. N) {
        foreach (j; i - 4 .. i + 4 + 1) {
          if (0 <= j && j < N) {
            if (check(i, j)) {
              --ans;
            }
          }
        }
      }
      ans /= 2;
      debug {
        writeln("ans = ", ans);
      }
      foreach (i; 0 .. N) {
        foreach (j; i + 1 .. i + 4 + 1) {
          if (0 <= j && j < N) {
            swap(A[i], A[j]);
            bool ok = true;
            foreach (k; min(i, j) - 2 .. max(i, j) + 1) {
              if (0 <= k && k + 3 <= N) {
                ok = ok && isKadomatsu(A[k], A[k + 1], A[k + 2]);
              }
            }
            swap(A[i], A[j]);
            if (ok) {
              ++ans;
            }
          }
        }
      }
      
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}
0