Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 109 additions & 0 deletions 201902769/1006.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
use std::io;

// 원이 아닌 평면일 경우
fn circulate(n: usize, w: i32, circle: &Vec<Vec<i32>>) -> i32 {
// dp[i] = circle[0][i] 묶지 않는 i까지 배열의 최대 수
let mut dp_1 = vec![0;n];
// dp[i] = circle[1][i] 묶지 않는 i까지 배열의 최대 수
let mut dp_2 = vec![0;n];
// dp[i] = i까지 배열의 최대 수
let mut dp_3 = vec![0;n];

if circle[0][0] + circle[1][0] <= w {
dp_3[0] = 1;
}

// dp_1[i]의 경우 : max(dp_3[i-1], dp_2[i-1] + 1)
// dp_2[i]의 경우 : max(dp_3[i-1], dp_1[i-1] + 1)
// dp_3[i]의 경우 : max(dp_1[i], dp_2[i], dp_3[i-1] + 1(위아래 경우), dp_3[i-2] + 2(좌우 2개))
for i in 1..n {
if i > 1 {
dp_3[i] = dp_3[i-2];
}

if circle[0][i] + circle[0][i-1] <= w {
dp_2[i] = i32::max(dp_3[i-1], dp_1[i-1] + 1);
dp_3[i] += 1;
}
else {
dp_2[i] = dp_3[i-1];
}

if circle[1][i] + circle[1][i-1] <= w {
dp_1[i] = i32::max(dp_3[i-1], dp_2[i-1] + 1);
dp_3[i] += 1;
}
else {
dp_1[i] = dp_3[i-1];
}

if circle[0][i] + circle[1][i] <= w {
dp_3[i] = i32::max(dp_3[i], dp_3[i-1] + 1);
}
dp_3[i] = i32::max(dp_3[i], dp_3[i-1]);
dp_3[i] = i32::max(dp_3[i], dp_2[i]);
dp_3[i] = i32::max(dp_3[i], dp_1[i]);

}

// println!("{:?} {:?} {:?} {:?}", circle, dp_1, dp_2, dp_3);
i32::max(i32::max(dp_1[n-1], dp_2[n-1]), dp_3[n-1])
}

fn main() {
let stdin = io::stdin();
let mut input = String::new();

stdin.read_line(&mut input).unwrap();
let t: usize = input.trim().parse().unwrap();

for _ in 0..t {
input.clear();
stdin.read_line(&mut input).unwrap();

let v: Vec<i32> = input.split_whitespace().map(|x| x.parse().unwrap()).collect();
let n = v[0] as usize;
let w = v[1];

let circle: Vec<Vec<i32>> = (0..2).map(|_| {
input.clear();
stdin.read_line(&mut input).unwrap();
input.split_whitespace().map(|x| x.parse().unwrap()).collect()
}).collect();

let mut merge_count = 0;
//원일 경우는 총 4가지.
// 안묶일 경우
merge_count = circulate(n, w, &circle);

if n == 1 {
println!("{:?}", n as i32 * 2 - merge_count );
continue;
}
//circle[0][0] & circle[0][-1] 이 묶일 경우
if circle[0][0] + circle[0][n-1] <= w {
let mut cloned_circle = circle.clone();
cloned_circle[0][0] = w + 1; // 사용하지 못하게 잠금
cloned_circle[0][n-1] = w + 1; // 사용하지 못하게 잠금
merge_count = i32::max(merge_count, circulate(n, w, &cloned_circle) + 1);
}
//circle[1][0] & circle[1][-1] 이 묶일 경우
if circle[1][0] + circle[1][n-1] <= w {
let mut cloned_circle = circle.clone();
cloned_circle[1][0] = w + 1; // 사용하지 못하게 잠금
cloned_circle[1][n-1] = w + 1; // 사용하지 못하게 잠금
merge_count = i32::max(merge_count, circulate(n, w, &cloned_circle) + 1);
}
//둘 다 묶일 경우
if circle[0][0] + circle[0][n-1] <= w && circle[1][0] + circle[1][n-1] <= w {
let mut cloned_circle = circle.clone();
cloned_circle[0][0] = w + 1; // 사용하지 못하게 잠금
cloned_circle[0][n-1] = w + 1; // 사용하지 못하게 잠금
cloned_circle[1][0] = w + 1; // 사용하지 못하게 잠금
cloned_circle[1][n-1] = w + 1; // 사용하지 못하게 잠금
merge_count = i32::max(merge_count, circulate(n, w, &cloned_circle) + 2);
}
println!("{:?}", n as i32 * 2 - merge_count );
}

}
76 changes: 76 additions & 0 deletions 201902769/1019.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use std::io;

fn main() {
let mut buf = String::new();
io::stdin().read_line(&mut buf).unwrap();

let n: i128 = match buf.trim().parse() {
Ok(num) => num,
Err(_) => {
eprintln!("Invalid input. Please enter a valid number.");
return;
}
};

// 0 ~ 10^n-1 까지의 count dp
let mut a = [0 as i128; 12]; // a[n] = a[n-1] * 10 + 10^n-1

// 이거 안썼네.. ㅠㅠ
let mut b = [0 as i128; 11]; // b[n] = a[n] + 1 - 111111111(n의 자리 1111)

let mut k = n;
let mut table = [0; 10];

// a와 b를 계산
for i in 1..11 {
if k == 0 {
break;
}
table[(k % 10) as usize] += 1;
k = k / 10;
a[i] = a[i - 1] * 10 + 10_i128.pow((i - 1).try_into().unwrap());
// b[i] = a[i] + 1;
// for j in 1..=i {
// b[i] -= 10_i128.pow((j - 1).try_into().unwrap());
// }
}


let mut count = table.clone();
let mut k = n;

for i in 1..11 {
if k == 0 {
// 0의 자리
for j in 1..i {
count[0] -= 10_i128.pow((j - 1).try_into().unwrap());
}
break;
}
table[k as usize %10] -= 1;
// i번째 수
for j in 0..k%10 {
count[j as usize] += 10_i128.pow(i-1);

}
// i번째 수 기준 오른쪽
for j in 0..10 {
count[j] += a[i as usize -1]*(k%10);
}
// i번째 수 기준 왼쪽
for j in 0..10 {
count[j as usize] += 10_i128.pow(i-1)*(k%10)*table[j];
}
k = k / 10;
}

// n == 1000000000 인 경우 pow 연산에서 count[0]에 오류 발생하여 예외 케이스 지정
if n == 1000000000 {
count[0] = 788888898;
}
// 출력
for i in 0..10 {
print!("{} ", count[i]);
}
}

54 changes: 54 additions & 0 deletions 201902769/1091.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
fn is_sorted(n: usize, cards: &[usize], p: &[usize]) -> bool {
cards.iter().enumerate().all(|(idx, &card)| card % 3 == p[idx])
}

fn shuffle(n: usize, cards: &mut [usize], s: &[usize]) {
let mut new_cards = vec![0; n];
for i in 0..n {
new_cards[i] = cards[s[i]];
}
cards.copy_from_slice(&new_cards);
}

fn check_cycle(n: usize, init_state: &[usize], cards: &[usize], s: &[usize]) -> bool {
init_state.iter().enumerate().all(|(i, &val)| val == cards[s[i]])
}

fn minimum_shuffles(n: usize, p: &[usize], s: &[usize]) -> i32 {
let mut cards: Vec<usize> = (0..n).collect();
let init_state = cards.clone();
let mut count = 0;

while !is_sorted(n, &cards, p) {
if count > 0 && check_cycle(n, &init_state, &cards, s) {
return -1;
}

shuffle(n, &mut cards, s);
count += 1;
}

count
}

fn main() {
let mut n = String::new();
let mut p = String::new();
let mut s = String::new();

std::io::stdin().read_line(&mut n).unwrap();
let n: usize = n.trim().parse().unwrap();

std::io::stdin().read_line(&mut p).unwrap();
let p: Vec<usize> = p.trim().split_whitespace()
.map(|x| x.parse().unwrap())
.collect();

std::io::stdin().read_line(&mut s).unwrap();
let s: Vec<usize> = s.trim().split_whitespace()
.map(|x| x.parse().unwrap())
.collect();

let result = minimum_shuffles(n, &p, &s);
println!("{}", result);
}
109 changes: 109 additions & 0 deletions 201902769/12100.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
use std::io;

fn main() {
let mut input = String::new();
let mut matrix: Vec<Vec<i32>> = Vec::new();

io::stdin().read_line(&mut input).unwrap();
let n: usize = input.trim().parse().unwrap();

for _ in 0..n {
input.clear();
io::stdin().read_line(&mut input).unwrap();
let row: Vec<i32> = input.trim().split_whitespace()
.map(|x| x.parse().unwrap())
.collect();
matrix.push(row);
}

let max_value = simulate(&mut matrix, 0);
println!("{}", max_value);
}

fn simulate(matrix: &mut Vec<Vec<i32>>, depth: usize) -> i32 {
if depth == 5 {
return max_matrix(matrix.to_vec());
}

let mut max_val = 0;
let dirs = vec![(0, 1), (0, -1), (1, 0), (-1, 0)]; // right, left, down, up

for dir in dirs {
let mut temp_matrix = matrix.clone();
move_matrix(&mut temp_matrix, dir);
max_val = i32::max(max_val, simulate(&mut temp_matrix, depth + 1));
}

max_val
}

fn max_matrix(matrix: Vec<Vec<i32>>) -> i32 {
let mut max_value = 0;
for i in 0..matrix.len() {
for j in 0..matrix.len() {
max_value = i32::max(max_value, matrix[i][j]);
}
}
max_value
}

fn move_matrix(matrix: &mut Vec<Vec<i32>>, direction: (i32, i32)) {
let n = matrix.len();
match direction {
(0, 1) => {
for row in matrix.iter_mut() {
row.reverse();
compress_and_merge(row);
row.reverse();
}
}
(0, -1) => {
for row in matrix.iter_mut() {
compress_and_merge(row);
}
}
(1,0) => {
for j in 0..n {
let mut col: Vec<i32> = (0..n).map(|i| matrix[i][j]).collect();
col.reverse();
compress_and_merge(&mut col);
col.reverse();
for i in 0..n {
matrix[i][j] = col[i];
}
}
}
(-1,0) => {
for j in 0..n {
let mut col: Vec<i32> = (0..n).map(|i| matrix[i][j]).collect();
compress_and_merge(&mut col);
for i in 0..n {
matrix[i][j] = col[i];
}
}
}
_ => panic!()
}
}

fn compress_and_merge(row: &mut Vec<i32>) {
let mut stack: Vec<i32> = Vec::new();
let mut isMerged = false;
for &val in row.iter() {
if val != 0 {
if let Some(last) = stack.last_mut() {
if *last == val && !isMerged {
*last *= 2;
isMerged = true;
continue;
}
}
stack.push(val);
isMerged = false;
}
}
row.fill(0);
for (i, &val) in stack.iter().enumerate() {
row[i] = val;
}
}
Loading