CP Lang
31 lineschecks// IOI 2019 "Vision Program": build an AND/OR/NOT circuit over an h x w image
// with exactly two black pixels that outputs whether they are at distance k.
// Two pixels are at distance exactly k when some pair of diagonals k apart
// (in either direction) both hold a black pixel and no pair k + 1 apart does.
extern fn add_and(ins: List<Int>) -> Int
extern fn add_or(ins: List<Int>) -> Int
extern fn add_not(x: Int) -> Int
@grader
fn construct_network(h: Int, w: Int, k: Int):
domain R = [h]
domain C = [w]
fn wire(r: R, c: C) -> Int = Int(r) * w + Int(c)
fn any(ws: List<Int>) -> Int = if ws.is_empty: add_and([0, add_not(0)]) else: add_or(ws)
fn lines(key: (R, C) -> Int) -> List<Int>:
let cells = [(r, c) for r in R for c in C]
let by_line = cells.group(by: rc => key(rc.0, rc.1))
[add_or(by_line[d].map(rc => wire(rc.0, rc.1)).collect()) for d in by_line.keys.collect().sorted()]
fn gap_exactly(ls: List<Int>, d: Int) -> Int =
any([add_and([ls[i], ls[i + d]]) for i in 0..ls.len - d])
fn gap_at_least(ls: List<Int>, d: Int) -> Int:
let seen = ls.scan((a, b) => add_or([a, b]))
any([add_and([seen.get(i), ls[i + d]]) for i in 0..ls.len - d])
let diag = [lines((r, c) => Int(r) + Int(c)), lines((r, c) => Int(r) - Int(c))]
let hit = add_or(diag.map(ls => gap_exactly(ls, k)).collect())
let over = add_or(diag.map(ls => gap_at_least(ls, k + 1)).collect())
add_and([hit, add_not(over)])
Accepted C++
112 lines · 0.78 s on the judge#include "bits/extc++.h"
using namespace std;
template <typename T, typename... U>
void dbgh(const T& t, const U&... u) {
cerr << t;
((cerr << " | " << u), ...);
cerr << endl;
}
#ifdef DEBUG
#define dbg(...) \
cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]: "; \
dbgh(__VA_ARGS__)
#else
#define dbg(...)
#define cerr \
if (false) \
cerr
#endif
#define endl "\n"
#define long int64_t
#define sz(x) int(std::size(x))
int add_and(vector<int> Ns);
int add_or(vector<int> Ns);
int add_xor(vector<int> Ns);
int add_not(int N);
void construct_network(int n, int m, int kv) {
auto id_arr = [&](int x, int y) -> int { return x * m + y; };
int b_true = -1, b_false = -1;
auto g_true = [&]() -> int {
if (b_true == -1) {
vector<int> cq;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cq.push_back(id_arr(i, j));
}
}
b_true = add_or(cq);
}
return b_true;
};
auto g_false = [&]() -> int {
if (b_false == -1) {
b_false = add_not(g_true());
}
return b_false;
};
auto go_cell = [&](auto cb) -> vector<int> {
map<int, vector<int>> mp;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
mp[cb(i, j)].push_back(id_arr(i, j));
}
}
vector<int> ans;
for (auto& [_k, v] : mp) {
ans.push_back(add_or(v));
}
return ans;
};
auto has_k_diff = [&](const vector<int>& arr, int kv) -> int {
int n = sz(arr);
if (kv >= n) {
return g_false();
}
vector<int> cq;
for (int i = 0; i + kv < n; i++) {
cq.push_back(add_and({arr[i], arr[i + kv]}));
}
return add_or(cq);
};
auto has_ge_k_diff = [&](const vector<int>& arr, int kv) -> int {
int n = sz(arr);
if (kv >= n) {
return g_false();
}
vector<int> cq;
int suff = arr[0];
for (int i = kv; i < n; i++) {
suff = add_or({suff, arr[i - kv]});
cq.push_back(add_and({arr[i], suff}));
}
return add_or(cq);
};
auto c1 = go_cell([&](int x, int y) -> int { return x + y; }),
c2 = go_cell([&](int x, int y) -> int { return x - y; });
int has_k = add_or({has_k_diff(c1, kv), has_k_diff(c2, kv)}),
has_gt_k =
add_or({has_ge_k_diff(c1, kv + 1), has_ge_k_diff(c2, kv + 1)});
add_and({has_k, add_not(has_gt_k)});
}