Pagini recente » Cod sursa (job #1867961) | Cod sursa (job #2753130) | Cod sursa (job #1944551) | Cod sursa (job #1935281) | Cod sursa (job #3140768)
use std::{
fs::{File, OpenOptions},
io::{BufRead, BufReader, Write},
path::Path,
};
struct Solution {
i_sp_min: usize,
j: usize,
sum: i32,
}
fn main() {
//const N_MAX: u32 = 6_000_000;
const N_MAX: usize = 10;
//sol
let mut solution = Solution {
i_sp_min: 0,
j: 0,
sum: i32::MIN,
};
let mut in_array = Box::new(vec![0; N_MAX + 1]);
let mut sp_array = Box::new(vec![0; N_MAX + 1]);
let in_file = File::open(Path::new("ssm.in")).unwrap();
let mut out_file = OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.open(Path::new("ssm.out"))
.unwrap();
let buf_reader = BufReader::new(in_file);
let mut file_lines_iter = buf_reader.lines();
let first_line = file_lines_iter.next().unwrap().unwrap();
let n = first_line.parse::<usize>().unwrap();
let second_line = file_lines_iter.next().unwrap().unwrap();
let temp_array = second_line
.split_whitespace()
.take(n)
.map(|line| line.parse::<i32>().unwrap())
.collect::<Vec<i32>>();
// copy the input array into the in_array starting at index 1
in_array.splice(1.., temp_array);
// build the partial sums array
for i in 1..=n {
sp_array[i] = sp_array[i - 1] + in_array[i];
}
println!("n: {}", n);
println!("in_array: {:?}", in_array);
println!("sp_array: {:?}", sp_array);
let mut j = 1usize;
// for each j find the best subsequence
while j <= n {
//println!("j {}", j);
// find next i_sp_min
let mut i = solution.i_sp_min;
while i < j {
if sp_array[i] < sp_array[solution.i_sp_min] {
solution.i_sp_min = i;
}
i += 1;
}
// calculate the new sum
let new_sum = sp_array[j] - sp_array[solution.i_sp_min];
if new_sum > solution.sum {
solution.sum = new_sum;
solution.j = j;
}
j += 1;
}
out_file
.write_all(format!("{} {} {}", solution.sum, solution.i_sp_min + 1, solution.j).as_bytes())
.unwrap();
}