結果

問題 No.190 Dry Wet Moist
ユーザー te-sh
提出日時 2016-09-15 12:04:33
言語 D
(dmd 2.109.1)
結果
AC  
実行時間 39 ms / 2,000 ms
コード長 1,693 bytes
コンパイル時間 901 ms
コンパイル使用メモリ 120,960 KB
実行使用メモリ 12,704 KB
最終ジャッジ日時 2024-06-12 04:23:43
合計ジャッジ時間 2,579 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

diff #

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

const maxA = 10 ^^ 5;

void main()
{
  auto n = readln.chomp.to!size_t;
  auto ai = readln.split.map!(to!int).array;

  auto buf = new int[](maxA * 2 + 1);
  foreach (a; ai) ++buf[a + maxA];

  auto d = dry(buf.dup);
  auto w = wet(buf.dup);
  auto m = moist(buf.dup);
  writeln(d, " ", w, " ", m);
}

int move(int[] buf, int i, int d)
{
  while (buf[i + maxA] == 0 && i != 0) i += d;
  return i;
}

int dry(int[] buf)
{
  auto i = move(buf, -maxA, 1), j = move(buf, maxA, -1), r = 0;
  while (i != 0 && j != 0) {
    if (-i > j) {
      ++r;
      --buf[i + maxA];
      --buf[j + maxA];
      i = move(buf, i, 1);
      j = move(buf, j, -1);
    } else {
      j = move(buf, j - 1, -1);
    }
  }
  if (j == 0) {
    auto a = buf[i + maxA..maxA].sum;
    auto b = buf[maxA];
    auto c = min(a, b);
    r += c;
    if (a - c > 0) r += (a - c) / 2;
  }
  return r;
}

int wet(int[] buf)
{
  auto i = move(buf, -maxA, 1), j = move(buf, maxA, -1), r = 0;
  while (i != 0 && j != 0) {
    if (-i < j) {
      ++r;
      --buf[i + maxA];
      --buf[j + maxA];
      i = move(buf, i, 1);
      j = move(buf, j, -1);
    } else {
      i = move(buf, i + 1, 1);
    }
  }
  if (i == 0) {
    auto a = buf[maxA + 1..j + 1 + maxA].sum;
    auto b = buf[maxA];
    auto c = min(a, b);
    r += c;
    if (a - c > 0) r += (a - c) / 2;
  }
  return r;
}

int moist(int[] buf)
{
  auto r = 0;
  r += buf[maxA] / 2;
  foreach (i; 1..maxA+1)
    r += min(buf[-i + maxA], buf[i + maxA]);
  return r;
}
0