Submission details
Task:Bittijono
Sender:JuusoH
Submission time:2026-01-17 16:51:02 +0200
Language:Rust (2021)
Status:READY
Result:0
Feedback
groupverdictscore
#10
#20
#30
Test results
testverdicttimegroup
#10.00 s1, 3details
#20.00 s1, 2, 3details
#30.00 s1, 3details
#40.00 s1, 3details
#50.00 s1, 3details
#6--3details
#7--2, 3details
#8--3details
#9--2, 3details
#10--2, 3details
#11--3details
#12--2, 3details
#13--3details
#14--3details
#15--3details
#160.00 s1, 2, 3details
#170.00 s1, 3details
#180.00 s1, 3details
#19--3details
#200.00 s3details

Compiler report

warning: variable does not need to be mutable
  --> input/code.rs:13:9
   |
13 |     let mut nums = input.trim().chars();
   |         ----^^^^
   |         |
   |         help: remove this `mut`
   |
   = note: `#[warn(unused_mut)]` on by default

warning: variable does not need to be mutable
  --> input/code.rs:14:9
   |
14 |     let mut list_a = nums.map(|c| c == '1').collect::<Vec<_>>();
   |         ----^^^^^^
   |         |
   |         help: remove this `mut`

warning: variable does not need to be mutable
  --> input/code.rs:18:9
   |
18 |     let mut nums = input.trim().chars();
   |         ----^^^^
   |         |
   |         help: remove this `mut`

warning: variable does not need to be mutable
  --> input/code.rs:19:9
   |
19 |     let mut list_b = nums.map(|c| c == '1').collect::<Vec<_>>();
   |         ----^^^^^^
   |         |
   |         help: remove this `mut`

warning: unused variable: `a`
  --> input/code.rs:41:41
   |
41 |     fn new(amount: usize, change: bool, a:...

Code

use std::io::stdin;
fn main() {
    let stdin = stdin();
    let mut input = String::new();
    _ = stdin.read_line(&mut input);
    let mut nums = input.split_whitespace();
    let n = nums.next().unwrap().parse::<usize>().unwrap();
    let a = nums.next().unwrap().parse::<usize>().unwrap();
    let b = nums.next().unwrap().parse::<usize>().unwrap();

    input.clear();
    _ = stdin.read_line(&mut input);
    let mut nums = input.trim().chars();
    let mut list_a = nums.map(|c| c == '1').collect::<Vec<_>>();

    input.clear();
    _ = stdin.read_line(&mut input);
    let mut nums = input.trim().chars();
    let mut list_b = nums.map(|c| c == '1').collect::<Vec<_>>();

    let mut lines = vec![];

    for i in 0..n {
        let val = list_a[i] ^ list_b[i];
        Line::list_lines_add(&mut lines, val, a, b);
    }

    println!("{lines:?}");

    let cost = Line::start_cost_eval(lines, a, b);

    println!("{cost}");
}

#[derive(Clone, Copy, Debug)]
struct Line {
    amount: usize,
    change: bool,
}
impl Line {
    fn new(amount: usize, change: bool, a: usize, b: usize) -> Self {
        //let c = Self::flip_cost(amount, a, b);
        Self { amount, change }
    }
    fn list_lines_add(list: &mut Vec<Line>, change: bool, a: usize, b: usize) {
        if list.len() > 0 {
            let last = list.last_mut().unwrap();
            let amount = last.amount;
            if last.change == change {
                last.amount += 1;
            } else {
                list.push(Line::new(1, change, a, b));
            }
        } else {
            list.push(Line::new(1, change, a, b));
        }
    }
    fn start_cost_eval(mut list: Vec<Line>, a: usize, b: usize) -> usize {
        if !list[0].change {
            list.remove(0);
        }
        if let Some(l) = list.last() {
            if !l.change {
                list.remove(list.len() - 1);
            }
        }
        Self::list_lines_cost(&list, a, b)
    }
    fn list_lines_cost(list: &[Line], a: usize, b: usize) -> usize {
        let seperate_cost = list
            .iter()
            .fold(0, |acc, o| if o.change { acc + o.cost(a, b) } else { acc });
        let total_amount = list.iter().fold(0, |acc, a| acc + a.amount);
        let all_flip_cost = Self::flip_cost(total_amount, a, b);
        if list.len() < 3 {
            return seperate_cost;
        }
        if list.len() == 3 {
            if all_flip_cost + list[1].cost(a, b) < seperate_cost {
                return all_flip_cost + list[1].cost(a, b);
            }
            return seperate_cost;
        }
        let pairs = (list.len() - 1) / 2;
        let mut best: Option<usize> = None;
        for i in 0..pairs {
            let start_i = i * 2;

            for l in 0..pairs - i {
                let end_i = start_i + 2 * (l + 1);
                let t_cost = Self::list_lines_cost(&list[start_i..end_i], a, b);
                let mut excluded_cost = 0;
                excluded_cost += Self::list_lines_cost(&list[..start_i], a, b);
                excluded_cost += Self::list_lines_cost(&list[end_i..], a, b);
                if let Some(b) = best.as_mut() {
                    *b = (*b).min(t_cost + excluded_cost);
                } else {
                    best = Some(t_cost + excluded_cost);
                }
                println!("best: {best:?}");
                println!("curr:  i: {start_i} l: {end_i}");
            }
        }
        return best.unwrap();
    }
    fn cost(&self, a: usize, b: usize) -> usize {
        Self::flip_cost(self.amount, a, b)
    }
    fn flip_cost(amount: usize, a: usize, b: usize) -> usize {
        let use_b_if_more = b / a;
        if amount > use_b_if_more {
            return b;
        }
        amount * a
    }
}

Test details

Test 1 (public)

Group: 1, 3

Verdict:

input
8 3 5
10110001
01101000

correct output
11

user output
[Line { amount: 2, change: tru...

Feedback: Output is longer than expected

Test 2

Group: 1, 2, 3

Verdict:

input
10 644 644
0111000100
0000010111

correct output
1932

user output
[Line { amount: 1, change: fal...

Feedback: Output is longer than expected

Test 3

Group: 1, 3

Verdict:

input
10 493 986
0001110000
0001100001

correct output
986

user output
[Line { amount: 5, change: fal...

Feedback: Output is longer than expected

Test 4

Group: 1, 3

Verdict:

input
10 240 720
1011001110
1000000001

correct output
1200

user output
[Line { amount: 2, change: fal...

Feedback: Output is longer than expected

Test 5

Group: 1, 3

Verdict:

input
10 3 7
1110111111
0010010101

correct output
15

user output
[Line { amount: 2, change: tru...

Feedback: Output is longer than expected

Test 6

Group: 3

Verdict:

input
100000 1 1000000000
001100110010101001010111000110...

correct output
50252

user output
(empty)

Test 7

Group: 2, 3

Verdict:

input
100000 1000000000 1
110010000110110100110110101011...

correct output
25055

user output
(empty)

Test 8

Group: 3

Verdict:

input
100000 1000 1000000000
001001101010100000011110000101...

correct output
50001000

user output
(empty)

Test 9

Group: 2, 3

Verdict:

input
100000 1000000000 1000
101010110001010011011011101110...

correct output
24939000

user output
(empty)

Test 10

Group: 2, 3

Verdict:

input
100000 1000000000 1000000000
001000000001000000000010110111...

correct output
25023000000000

user output
(empty)

Test 11

Group: 3

Verdict:

input
100000 123456789 987654321
100010110100011000001111001110...

correct output
5475678967593

user output
(empty)

Test 12

Group: 2, 3

Verdict:

input
100000 987654321 123456789
000100110000010110111101111101...

correct output
3071481453531

user output
(empty)

Test 13

Group: 3

Verdict:

input
100000 1000000 1000000000
001100110010100011000111101100...

correct output
49916000000

user output
(empty)

Test 14

Group: 3

Verdict:

input
100000 10000000 1000000000
110111101101111110100101011000...

correct output
494930000000

user output
(empty)

Test 15

Group: 3

Verdict:

input
100000 100000000 1000000000
111110000010100011011100110010...

correct output
4547300000000

user output
(empty)

Test 16

Group: 1, 2, 3

Verdict:

input
1 1 1
1
1

correct output
0

user output
[Line { amount: 1, change: fal...

Feedback: Output is longer than expected

Test 17

Group: 1, 3

Verdict:

input
10 600 800
0000000000
1110111111

correct output
1400

user output
[Line { amount: 3, change: tru...

Feedback: Output is longer than expected

Test 18

Group: 1, 3

Verdict:

input
10 300 599
1101001010
0011010110

correct output
1198

user output
[Line { amount: 3, change: tru...

Feedback: Output is longer than expected

Test 19

Group: 3

Verdict:

input
100000 300000000 500000000
010011101001001010010101101101...

correct output
10000000000000

user output
(empty)

Test 20

Group: 3

Verdict:

input
100000 60000 1000000000
110110111011010100001000011011...

correct output
3000000000

user output
[Line { amount: 16668, change:...

Feedback: Output is longer than expected