1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

codejam用 rustテンプレート

Posted at

標準入力から問題を受け取りアクターで並列に答え解いて標準出力に吐き出す用
RustかわいいよRust

template.rs
use std;

import core::io;
import io::reader;
import io::reader_util;
import std::sort;

impl read_util for reader{  
  fn read_valid_char() -> char {
    let mut c;
    do {
      c = self.read_char();
    } while c == '\n' || c == ' ';
    ret c;
  }

  fn read_valid_line() -> str{
    let mut line;
    do {
      line = self.read_line();
    } while line == "";
    ret line;
  }

  fn read_uint() -> uint{
    let raw_uint = self.read_valid_line();
    ret option::get(uint::from_str(raw_uint));
  }
  fn read_int() -> int{
    ret option::get(int::from_str(self.read_line()));
  }
  fn read_uints() -> [uint]{
    fn to_uint(&&s: str) -> uint{
      ret option::get(uint::from_str(s));
    }
    let line = self.read_line();
    ret vec::map(str::split_char(line, ' '), to_uint);
  }
  fn read_ints() -> [int]{
    fn to_int(&&s: str) -> int{
      ret option::get(int::from_str(s));
    }
    let line = self.read_valid_line();
    ret vec::map(str::split_char(line, ' '), to_int);
  }
}

#[doc="input data type"]
type input = {
  mut index: uint,
  w: uint, 
  h: uint, 
  board: [str]
};


#[doc="build input data from stdin"]
fn fetch(stdin: io::reader) -> input {
  let s = stdin.read_uint();
  let v1 = stdin.read_ints();
  let v2 = stdin.read_ints();

  ret {
    mut index: 0u, 
    s : s, 
    v1: v1, 
    v2: v2
  };
}

#[doc="output status"]
enum out_status {
  ready,
  start, 
  solved((uint, str))
}


fn solve(in: input) -> str {
  log(error, #fmt("idx.%u start solved", in.index));
  
  let mut v1 = in.v1;
  let mut v2 = in.v2;
  fn le(&&a: int, &&b: int) -> bool { int::le(a, b) }
  fn ge(&&a: int, &&b: int) -> bool { int::ge(a, b) }
  
  v1 = sort::merge_sort(le, v1);
  v2 = sort::merge_sort(ge, v2);

  fn times(&&x: int, &&y: int) -> int { ret x * y; }
  fn sum(&&accum: int, &&a: int) -> int { ret accum+a; }
  ret int::to_str(vec::foldl(0, vec::map2(v1, v2, times), sum), 10u);

}

fn main() {
  let stdin = io::stdin();
  let n = stdin.read_uint();
  let mut inputs : [input] = [];
  let mut outputs = vec::to_mut(vec::from_elem(n, ""));

  let port = comm::port();
  let chan = comm::chan(port);

  log(error, #fmt("n: %u", n));
  uint::range(0u, n) {|i| 
    let in = fetch(stdin);
    in.index = i;
    inputs += [in];
    
    task::spawn{||
      build_actor(in, chan);
    }
  }
  
  iter::repeat(n) {||
    alt check comm::recv(port){
     solved((idx, res)) {
      outputs[idx] = res;
     }
     _ {
      log(error, "what?");
     }
    }
  }

  vec::iteri(outputs){|i, res|
    io::println(#fmt("Case #%u: %s", i+1u, res));
  }
}

fn build_actor(in: input, chan: comm::chan<out_status>) {
  let res = solve(in);
  comm::send(chan, solved((in.index, res)));
}

↑で想定してる問題は2008 Round1A Aの

2
3
1 3 -5
-2 4 1
5
1 2 3 4 5
1 0 1 0 1

というような形式のもの

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?