結果

問題 No.602 隠されていたゲーム2
ユーザー yosupot
提出日時 2017-12-02 00:22:56
言語 D
(dmd 2.109.1)
結果
AC  
実行時間 30 ms / 2,000 ms
コード長 5,325 bytes
コンパイル時間 783 ms
コンパイル使用メモリ 108,624 KB
実行使用メモリ 5,936 KB
最終ジャッジ日時 2024-06-12 22:45:20
合計ジャッジ時間 1,722 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

/+ dub.sdl:
name "A"
dependency "dcomp" version=">=0.7.4"
+/
import std.stdio, std.conv, std.algorithm, std.range;
import std.math;
// import dcomp.scanner;
int solve() {
auto sc = new Scanner(stdin);
int n;
long[] _di;
sc.read(n, _di);
typeof(_di.sort!"a<b")[2] di;
di[0] = _di.filter!"a%2==0".array.sort!"a<b";
di[1] = _di.filter!"a%2==1".array.sort!"a<b";
long x, y;
sc.read(x, y);
if (x == 0 && y == 0) {
return 0;
}
long dist = abs(x) + abs(y);
if (di[0].contains(dist) || di[1].contains(dist)) {
return 1;
}
foreach (d; _di) {
long mi = 10L^^18;
long ma = -(10L^^18);
void ck(long a, long b) {
long c = abs(a) + abs(b);
mi = min(mi, c);
ma = max(ma, c);
}
ck(x-d, y);
ck(x+d, y);
ck(x, y-d);
ck(x, y+d);
if (y-x-d <= 0 && 0 <= y-x+d) {
long z = (y-x+d)/2;
ck(x-d+z, y-z);
}
if (y+x-d <= 0 && 0 <= y+x+d) {
long z = (y+x+d)/2;
ck(x+d-z, y-z);
}
auto rng = di[(mi%2+2)%2].upperBound(mi-1);
if (!rng.empty && rng.front <= ma) {
return 2;
}
}
return -1;
}
void main() {
writeln(solve());
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */
// module dcomp.scanner;
// import dcomp.container.stack;
class Scanner {
import std.stdio : File;
import std.conv : to;
import std.range : front, popFront, array, ElementType;
import std.array : split;
import std.traits : isSomeChar, isStaticArray, isArray;
import std.algorithm : map;
File f;
this(File f) {
this.f = f;
}
char[512] lineBuf;
char[] line;
private bool succW() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (!line.empty && line.front.isWhite) {
line.popFront;
}
return !line.empty;
}
private bool succ() {
import std.range.primitives : empty, front, popFront;
import std.ascii : isWhite;
while (true) {
while (!line.empty && line.front.isWhite) {
line.popFront;
}
if (!line.empty) break;
line = lineBuf[];
f.readln(line);
if (!line.length) return false;
}
return true;
}
private bool readSingle(T)(ref T x) {
import std.algorithm : findSplitBefore;
import std.string : strip;
import std.conv : parse;
if (!succ()) return false;
static if (isArray!T) {
alias E = ElementType!T;
static if (isSomeChar!E) {
auto r = line.findSplitBefore(" ");
x = r[0].strip.dup;
line = r[1];
} else static if (isStaticArray!T) {
foreach (i; 0..T.length) {
bool f = succW();
assert(f);
x[i] = line.parse!E;
}
} else {
StackPayload!E buf;
while (succW()) {
buf ~= line.parse!E;
}
x = buf.data;
}
} else {
x = line.parse!T;
}
return true;
}
int read(T, Args...)(ref T x, auto ref Args args) {
if (!readSingle(x)) return 0;
static if (args.length == 0) {
return 1;
} else {
return 1 + read(args);
}
}
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/container/stack.d */
// module dcomp.container.stack;
struct StackPayload(T, size_t MIN = 4) {
import std.algorithm : max;
import std.conv;
import std.range.primitives : ElementEncodingType;
import core.stdc.string : memcpy;
private T* _data;
private uint len, cap;
bool empty() const { return len == 0; }
@property size_t length() const { return len; }
alias opDollar = length;
ref inout(T) opIndex(size_t i) inout { return _data[i]; }
ref inout(T) front() inout { return _data[0]; }
ref inout(T) back() inout { assert(len); return _data[len-1]; }
void reserve(size_t nlen) {
import core.memory : GC;
if (nlen <= cap) return;
void* nx = GC.malloc(nlen * T.sizeof);
cap = nlen.to!uint;
if (len) memcpy(nx, _data, len * T.sizeof);
_data = cast(T*)(nx);
}
void free() {
import core.memory : GC;
GC.free(_data);
}
void opOpAssign(string op : "~")(T item) {
if (len == cap) {
reserve(max(MIN, cap*2));
}
_data[len++] = item;
}
void insertBack(T item) {
this ~= item;
}
void removeBack() {
len--;
}
void clear() {
len = 0;
}
T[] data() {
return (_data) ? _data[0..len] : null;
}
}
/*
This source code generated by dcomp and include dcomp's source code.
dcomp's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dcomp)
dcomp's License: MIT License(https://github.com/yosupo06/dcomp/blob/master/LICENSE.txt)
*/
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0