Skip to content

Commit

Permalink
abc079 a, b, c, d
Browse files Browse the repository at this point in the history
  • Loading branch information
bouzuya committed Jan 6, 2023
1 parent f90943f commit 75b7d26
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 77 deletions.
88 changes: 86 additions & 2 deletions abc079/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion abc079/Cargo.toml
@@ -1,11 +1,11 @@
[package]
name = "abc079"
version = "0.1.0"
authors = ["bouzuya <m@bouzuya.net>"]
edition = "2018"

# dependencies added to new project
[dependencies]
num = "=0.2.1"
proconio = { version = "=0.3.6", features = ["derive"] }
superslice = "=1.0.0"

Expand Down
5 changes: 2 additions & 3 deletions abc079/src/bin/a.rs
@@ -1,10 +1,9 @@
use proconio::input;
use proconio::marker::Chars;
use proconio::{input, marker::Chars};

fn main() {
input! {
n: Chars,
};
let ans = (n[0] == n[1] && n[0] == n[2]) || (n[1] == n[2] && n[1] == n[3]);
let ans = (n[0] == n[1] && n[1] == n[2]) || (n[1] == n[2] && n[2] == n[3]);
println!("{}", if ans { "Yes" } else { "No" });
}
13 changes: 6 additions & 7 deletions abc079/src/bin/b.rs
Expand Up @@ -2,14 +2,13 @@ use proconio::input;

fn main() {
input! {
n: usize
n: usize,
};
let mut l = vec![0_usize; n + 1];
l[0] = 2;
l[1] = 1;
for i in 2..=n {
l[i] = l[i - 1] + l[i - 2];
let mut x = (2_usize, 1_usize);
for _ in 2..=n {
let (l_i_2, l_i_1) = x;
x = (l_i_1, l_i_1 + l_i_2);
}
let ans = l[n];
let ans = x.1;
println!("{}", ans);
}
63 changes: 23 additions & 40 deletions abc079/src/bin/c.rs
@@ -1,48 +1,31 @@
use proconio::input;
use proconio::marker::Chars;

fn dfs(d: &Vec<i64>, o: &mut Vec<bool>) -> Option<Vec<bool>> {
if o.len() == d.len() - 1 {
let mut r = d[0];
for (&o_i, &d_i) in o.iter().zip(d.iter().skip(1)) {
if o_i {
r += d_i;
} else {
r -= d_i;
}
}
return if r == 7 { Some(o.to_vec()) } else { None };
}

o.push(true);
if let Some(o) = dfs(d, o) {
return Some(o);
}
o.pop();
o.push(false);
if let Some(o) = dfs(d, o) {
return Some(o);
}
o.pop();
None
}
use proconio::{input, marker::Chars};

fn main() {
input! {
abcd: Chars
abcd: Chars,
};
let d = abcd
.iter()
.map(|&c| c.to_digit(10).unwrap() as i64)
.collect::<Vec<i64>>();
match dfs(&d, &mut vec![]) {
None => unreachable!(),
Some(o) => {
print!("{}", d[0]);
for (&o_i, &d_i) in o.iter().zip(d.iter().skip(1)) {
print!("{}{}", if o_i { "+" } else { "-" }, d_i);
let a = (abcd[0] as u8 - b'0') as i64;
let b = (abcd[1] as u8 - b'0') as i64;
let c = (abcd[2] as u8 - b'0') as i64;
let d = (abcd[3] as u8 - b'0') as i64;

for op_1 in vec![1, -1] {
for op_2 in vec![1, -1] {
for op_3 in vec![1, -1] {
if (((a + op_1 * b) + op_2 * c) + op_3 * d) == 7 {
println!(
"{}{}{}{}{}{}{}=7",
a,
if op_1 == 1 { '+' } else { '-' },
b,
if op_2 == 1 { '+' } else { '-' },
c,
if op_3 == 1 { '+' } else { '-' },
d
);
return;
}
}
println!("=7");
}
}
}
49 changes: 25 additions & 24 deletions abc079/src/bin/d.rs
@@ -1,19 +1,20 @@
use proconio::input;

fn dijkstra(n: usize, inf: u64, e: &Vec<Vec<(usize, u64)>>, s: usize) -> Vec<u64> {
fn dijkstra(n: usize, inf: usize, e: &[Vec<(usize, usize)>], s: usize) -> Vec<usize> {
use std::{cmp::Reverse, collections::BinaryHeap};
let mut d = vec![inf; n];
let mut pq = std::collections::BinaryHeap::new();
let mut pq = BinaryHeap::new();
d[s] = 0;
pq.push(std::cmp::Reverse((0, s)));
while let Some(std::cmp::Reverse((w_u, u))) = pq.pop() {
pq.push(Reverse((0, s)));
while let Some(Reverse((w_u, u))) = pq.pop() {
if w_u > d[u] {
continue;
}
for &(v, w_v) in e[u].iter() {
for (v, w_v) in e[u].iter().copied() {
let w = w_u + w_v;
if w < d[v] {
d[v] = w;
pq.push(std::cmp::Reverse((w, v)));
pq.push(Reverse((w, v)));
}
}
}
Expand All @@ -24,32 +25,32 @@ fn main() {
input! {
h: usize,
w: usize,
c: [[u64; 10]; 10],
c: [[usize; 10]; 10],
a: [[i64; w]; h],
};
let mut e = vec![vec![]; 10];
for (i, c_i) in c.iter().enumerate() {
for (j, &c_ij) in c_i.iter().enumerate() {
if i == j {
continue;
}
e[i].push((j, c_ij));

let mut edges = vec![vec![]; 10];
for i in 0..10 {
for j in 0..10 {
edges[i].push((j, c[i][j]));
}
}
let inf = 1_000_000_u64;
let mut d = vec![];

let mut dists = vec![];
for i in 0..10 {
d.push(dijkstra(10, inf, &e, i));
let d = dijkstra(10, 1 << 60, &edges, i);
dists.push(d[1]);
}
let mut ans = 0;
for a_i in a.iter() {
for &a_ij in a_i.iter() {
match a_ij {
-1 | 1 => {} // do nothing
0 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 => ans += d[a_ij as usize][1],
_ => unreachable!(),

let mut ans = 0_usize;
for i in 0..h {
for j in 0..w {
if a[i][j] == -1 {
continue;
}
ans += dists[a[i][j] as usize];
}
}

println!("{}", ans);
}

0 comments on commit 75b7d26

Please sign in to comment.