結果
| 問題 |
No.2799 Cut and Eat
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-06-29 04:14:27 |
| 言語 | D (dmd 2.109.1) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 2,000 ms |
| コード長 | 3,051 bytes |
| コンパイル時間 | 2,570 ms |
| コンパイル使用メモリ | 157,448 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-06-29 04:14:31 |
| 合計ジャッジ時間 | 3,440 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 31 |
ソースコード
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; }
string COLOR(string s = "") { return "\x1b[" ~ s ~ "m"; }
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)); }
long[string] cache;
long brute(long K, long[] A) {
A.sort;
const key = A.to!string;
auto ptr = key in cache;
if (ptr) return *ptr;
long ret;
const N = cast(int)(A.length);
if (N) {
ret = long.min;
foreach (i; 0 .. N) {
if (A[i] <= K) {
chmax(ret, A[i] - brute(K, A[0 .. i] ~ A[i + 1 .. N]));
} else {
foreach (b; 1 .. A[i]) {
chmax(ret, 0 - brute(K, A[0 .. i] ~ [b, A[i] - b] ~ A[i + 1 .. $]));
}
}
}
}
return cache[key] = ret;
}
long solve(long K, long[] A) {
A.sort;
const N = cast(int)(A.length);
const cut = A.upperBound(K);
long ans;
foreach (i; 0 .. cut) {
ans += (i&1?-1:+1) * A[cut - 1 - i];
}
int sig = (cut&1?-1:+1);
foreach (i; cut .. N) if (!(A[i] & 1)) {
sig = -sig;
}
foreach (i; cut .. N) if (A[i] & 1) {
ans += sig * (-1);
sig = -sig;
}
return ans;
}
void main() {
debug {{
foreach (K; 1L .. 5L + 1) {
cache.clear;
writeln("K = ", K);
foreach (a; 1L .. 10L + 1) {
writeln([a], ": ", brute(K, [a]));
}
foreach (m; 1 .. 10) {
foreach (p; 0 .. 1 << (m-1)) {
long[] bs;
bs ~= 0;
foreach (j; 1 .. m) if (p >> (j-1) & 1) bs ~= j;
const N = cast(int)(bs.length);
bs ~= m;
auto A = new long[N];
foreach (i; 0 .. N) A[i] = bs[i + 1] - bs[i];
const brt = brute(K, A);
const slv = solve(K, A);
assert(brt == slv, format("%s %s: %s %s", K, A, brt, slv));
}
}
writeln;
}
}}
try {
for (; ; ) {
const N = readInt;
const K = readLong;
auto A = new long[N];
foreach (i; 0 .. N) {
A[i] = readLong;
}
const ans = solve(K, A);
writeln((A.sum + ans) / 2);
}
} catch (EOFException e) {
}
}