Skip to content

Instantly share code, notes, and snippets.

@recursivecurry
Created April 28, 2015 15:05
Show Gist options
  • Save recursivecurry/f6060d00ebd70722c66e to your computer and use it in GitHub Desktop.
Save recursivecurry/f6060d00ebd70722c66e to your computer and use it in GitHub Desktop.
google codejam round1a b - go and rust
package main
import (
"fmt"
"math"
)
func main() {
var T int
fmt.Scanf("%d", &T)
for i := 0; i < T; i++ {
var B, N int
fmt.Scanf("%d %d", &B, &N)
M := make([]int, B)
for j := 0; j < B; j++ {
fmt.Scanf("%d", &M[j])
}
//fmt.Printf("%v %v %v %v\n", B, N, M, len(M))
fmt.Printf("Case %v: %v\n", i+1, solve(M, B, N))
}
}
func solve(M []int, B int, N int) int {
var mid, num int64
top := int64(math.MaxInt64)
bottom := int64(0)
for bottom < top {
num = 0
mid = (top + bottom) / 2
for i := 0; i < B; i++ {
num += mid/int64(M[i]) + 1
}
if int64(N) <= num {
top = mid
} else {
bottom = mid + 1
}
}
num = 0
cand := make([]int, 0, len(M))
for i := 0; i < B; i++ {
num += top/int64(M[i]) + 1
if top%int64(M[i]) == 0 {
cand = append(cand, i)
}
}
return cand[len(cand)-1+N-int(num)] + 1
}
use std::io;
fn solve(m: &Vec<i64>, b: i64, n: i64, bottom: i64, top: i64) -> i64 {
if bottom < top {
let mid = (top + bottom) / 2;
let num = m.iter().fold(0, |acc, &x| {acc+mid/x+1});
if n <= num {
solve(m, b, n, bottom, mid)
} else {
solve(m, b, n, mid+1, top)
}
} else {
let ans = m.iter().zip(0..b).fold((0, vec![]), |mut acc, x| (acc.0 + top / x.0 + 1, if top % x.0 == 0 {acc.1.push(x.1); acc.1} else {acc.1}));
ans.1[ans.1.len()-1+n as usize-ans.0 as usize]+1
}
}
fn main() {
let t;
let mut stdin = io::stdin();
let mut input = String::new();
stdin.read_line(&mut input).unwrap();
t = input.trim().parse::<i64>().unwrap();
for i in 0..t {
input.clear();
stdin.read_line(&mut input).unwrap();
let inputs: Vec<i64> = input.split(' ').map(|x| x.trim().parse::<i64>().unwrap()).collect();
let b = inputs[0];
let n = inputs[1];
input.clear();
stdin.read_line(&mut input).unwrap();
let m = input.split(' ').map(|x| x.trim().parse::<i64>().unwrap()).collect();
println!("Case {}: {}\n", i+1, solve(&m, b, n, 0, std::i64::MAX));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment