実施日
2025/09/07
得たこと
- 実行時間を考える必要がある
- ループ処理は極力排除する
- 入力の条件に気を付ける
解けた問題
A/B/C
A問題
- 1回目
use proconio::input;
fn main() {
input! {
s: [String;3],
}
let mut world = s[0].parse::<i32>().unwrap();
let mut stage = s[2].parse::<i32>().unwrap();
if stage == 8 {
world += 1;
stage = 1;
} else {
stage += 1;
}
println!("{}-{}", world, stage);
}
課題:入力は1 - 1ではなく1-1だったためs[0]にしか情報が入っておらず、RE
解決:入力をs[String;3]からStringに変更し、その後-でわけて配列化する
- 2回目
use proconio::input;
fn main() {
input! {
s: String,
}
let s: Vec<&str> = s.split('-').collect();
let mut world = s[0].parse::<i32>().unwrap();
let mut stage = s[1].parse::<i32>().unwrap();
if stage == 8 {
world += 1;
stage = 1;
} else {
stage += 1;
}
println!("{}-{}", world, stage);
}
結果:371Byte,1ms,2072KiB,16:40
B問題
- 1回目
h×w行列にして"#"があったら上下左右を見てflagを操作で実装
use proconio::input;
fn main() {
input! {
h: usize,
w: usize,
s: [String; h],
}
let grid: Vec<Vec<char>> = s.iter().map(|row| row.chars().collect()).collect();
let mut flag = true;
for i in 0..h {
for j in 0..w {
if grid[i][j] == '#' {
let mut checker = 0;
let directions = [(-1, 0), (1, 0), (0, -1), (0, 1)];
for (di, dj) in directions.iter() {
let ni = i as isize + di;
let nj = j as isize + dj;
if ni >= 0 && ni < h as isize && nj >= 0 && nj < w as isize {
if grid[ni as usize][nj as usize] == '#' {
checker += 1;
}
}
}
if checker == 0 || checker == 1 || checker == 3 {
flag = false;
break;
}
}
}
if !flag {
break;
}
}
if flag {
println!("Yes");
} else {
println!("No");
}
}
課題:もっといいアルゴリズムがあったかもしれない
解決:考え中
結果:1153Byte,1ms,2076KiB,28:20
C問題
- 1回目
use proconio::input;
fn main() {
input! {
t: usize,
test_cases: [[usize; 3]; t],
}
for i in 0..t {
let mut a = test_cases[i][0];
let mut b = test_cases[i][1];
let mut c = test_cases[i][2];
let mut flag = true;
let mut count = 0;
while flag {
if a > 0 && c > 0 {
if b > 0 {
count += 1;
a -= 1;
b -= 1;
c -= 1;
} else if b == 0 {
a -= 1;
c -= 1;
if a == 0 && c == 0 {
flag = false;
} else if a > c {
count += 1;
a -= 1;
} else if c > a {
count += 1;
c -= 1;
}
}
} else {
flag = false;
}
}
println!("{}", count);
}
}
問題点:実行時間が長すぎる
課題:ループ処理の排除
解決:まとめて処理できるところは処理したい
- 2回目
use proconio::input;
fn main() {
input! {
t: usize,
test_cases: [[usize; 3]; t],
}
for i in 0..t {
let mut a = test_cases[i][0];
let mut b = test_cases[i][1];
let mut c = test_cases[i][2];
let mut flag = true;
let mut count = 0;
let mut temp = a.min(b).min(c);
count += temp;
a -= temp;
b -= temp;
c -= temp;
while flag {
if a > 0 && c > 0 {
let min_ac = a.min(c);
let temp_a = a - min_ac;
let temp_c = c - min_ac;
if temp_a > 0 && temp_a >= min_ac {
count += min_ac;
a -= min_ac;
c -= min_ac;
} else if temp_c > 0 && temp_c >= min_ac {
count += min_ac;
a -= min_ac;
c -= min_ac;
} else if temp_a < min_ac && temp_a > 0 {
count += temp_a;
a -= temp_a * 2;
c -= temp_a;
}else if temp_c < min_ac && temp_c > 0 {
count += temp_c;
a -= temp_c * 2;
c -= temp_c;
} else if temp_a == 0 && temp_c == 0 {
count += min_ac / 2;
a -= min_ac / 2 * 2;
c -= min_ac / 2;
} else {
flag = false;
}
} else {
flag = false;
}
}
println!("{}", count);
}
}
問題点:やはり処理が長い
課題:ACが大量に残った時にループ処理がある
解決:AとCの数の和を取って3で割れば取り出せる回数わかるのでは?
- 3回目
CE - 4回目
use proconio::input;
fn main() {
input! {
t: usize,
test_cases: [[usize; 3]; t],
}
for i in 0..t {
let mut a = test_cases[i][0];
let mut b = test_cases[i][1];
let mut c = test_cases[i][2];
let mut flag = true;
let mut count = 0;
let mut temp = a.min(b).min(c);
count += temp;
a -= temp;
b -= temp;
c -= temp;
if a > 0 && c > 0 {
let mut temp_ac = a + c;
let mut min_ac = a.min(c);
if (temp_ac / 3) <= min_ac {
count += temp_ac / 3;
} else {
count += min_ac;
}
}
println!("{}", count);
}
}
我ながらよく思いついたなと…
結果:746Byte,201ms,18056KiB,96:37