My code compiles quickly on my old laptop using the same version of Rust as
the competition environment, however, I had to renamed my cache variable to
_cache to avoid a file locking error:
>> time cargo build --release
Blocking waiting for file lock on package cache
However, after fixing this error by renaming to _cache, I resubmitted,
however now I always get CTLE. I'm wondering if this is a locking issue due
to GCJ trying to run cargo build twice at the same time. Or, is my code
simply compiling too slowly? Here is my code for what it's worth:
fn main() {
let mut sz = [[0usize;100]; 100];
let mut _cache: [[Option<usize>;100]; 100] = [[None;100]; 100];
let mut multiset = [[[0usize;100];100]; 100];
let (r, w) = (std::io::stdin(), std::io::stdout());
let mut sc = IO::new(r.lock(), w.lock());
let t: usize = sc.read();
for case_num in 1..=t {
let (e, w): (usize, usize) = (sc.read(), sc.read());
for i in 0..e {
let weight_counts: Vec<usize> = sc.vec(w);
sz[i][i] = weight_counts.iter().sum::<usize>();
for k in i..e {
_cache[i][k] = None;
}
for j in 0..w {
multiset[i][i][j] = weight_counts[j];
}
}
for i in 0..e {
for j in i+1..e {
sz[i][j] = 0;
for k in 0..w {
multiset[i][j][k] = multiset[i][j-1][k].min(multiset[j][j][k]);
sz[i][j] += multiset[i][j][k];
assert!(sz[i][j-1] >= sz[i][j]);
}
}
}
let ans = 2*sz[0][e-1] + m(0, e-1, &sz, &mut _cache);
sc.write(
format!("Case #{}: {}", case_num, ans)
);
sc.write("\n");
}
}
fn m(l: usize, r: usize, sz: &[[usize;100]; 100], _cache: &mut
[[Option<usize>;100]; 100]) -> usize {
if l == r {
return 0;
}
if let Some(cached) = _cache[l][r] {
return cached;
}
let mut ans = 1_000_000_000;
for x in l..r {
let left = m(l, x, sz, _cache) + 2*sz[l][x] - 2*sz[l][r];
let right = m(x+1, r, sz, _cache) + 2*sz[x+1][r] - 2*sz[l][r];
ans = ans.min(left+right);
}
_cache[l][r] = Some(ans);
ans
}
pub struct IO<R, W: std::io::Write>(R, std::io::BufWriter<W>);
impl<R: std::io::Read, W: std::io::Write> IO<R, W> {
pub fn new(r: R, w: W) -> IO<R, W> {
IO(r, std::io::BufWriter::new(w))
}
pub fn write<S: ToString>(&mut self, s: S) {
use std::io::Write;
self.1.write_all(s.to_string().as_bytes()).unwrap();
}
pub fn read<T: std::str::FromStr>(&mut self) -> T {
use std::io::Read;
let buf = self
.0
.by_ref()
.bytes()
.map(|b| b.unwrap())
.skip_while(|&b| b == b' ' || b == b'\n' || b == b'\r' || b == b'\t')
.take_while(|&b| b != b' ' && b != b'\n' && b != b'\r' && b != b'\t')
.collect::<Vec<_>>();
unsafe { std::str::from_utf8_unchecked(&buf) }
.parse()
.ok()
.expect("Parse error.")
}
pub fn vec<T: std::str::FromStr>(&mut self, n: usize) -> Vec<T> {
(0..n).map(|_| self.read()).collect()
}
}
--
-- You received this message because you are subscribed to the Google Groups
Code Jam group. To post to this group, send email to
[email protected]. To unsubscribe from this group, send email to
[email protected]. For more options, visit this group at
https://groups.google.com/d/forum/google-code?hl=en
---
You received this message because you are subscribed to the Google Groups
"Google Code Jam" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/google-code/c3f834aa-f313-402c-8a60-20e193fda0d2n%40googlegroups.com.