Advertisement
paranid5

18.02 2

Feb 18th, 2022
1,255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 3.85 KB | None | 0 0
  1. use std::{
  2.     fmt::Debug,
  3.     fs::File,
  4.     io,
  5.     io::Read,
  6.     iter::FromIterator,
  7.     num::ParseIntError,
  8.     str::{FromStr, SplitWhitespace},
  9. };
  10.  
  11. #[inline]
  12. #[allow(dead_code)]
  13. fn input() -> String {
  14.     let mut input = String::new();
  15.     unsafe {
  16.         io::stdin().read_line(&mut input).unwrap_unchecked();
  17.     }
  18.     input
  19. }
  20.  
  21. #[inline]
  22. #[allow(dead_code)]
  23. fn input_parse<T: FromStr<Err = ParseIntError> + Debug>() -> T {
  24.     let mut input = String::new();
  25.     unsafe {
  26.         io::stdin().read_line(&mut input).unwrap_unchecked();
  27.         input.trim().parse().unwrap_unchecked()
  28.     }
  29. }
  30.  
  31. #[inline]
  32. #[allow(dead_code)]
  33. fn input_container<T, C>() -> C
  34. where
  35.     T: FromStr<Err = ParseIntError> + Debug,
  36.     C: FromIterator<T>,
  37. {
  38.     let mut input = String::new();
  39.     unsafe {
  40.         io::stdin().read_line(&mut input).unwrap_unchecked();
  41.     }
  42.  
  43.     input
  44.         .split_whitespace()
  45.         .map(|x| unsafe { x.parse::<T>().unwrap_unchecked() })
  46.         .collect::<C>()
  47. }
  48.  
  49. #[inline]
  50. #[allow(dead_code)]
  51. fn input_pair<F, S>() -> (F, S)
  52. where
  53.     F: FromStr<Err = ParseIntError> + Debug,
  54.     S: FromStr<Err = ParseIntError> + Debug,
  55. {
  56.     let mut input = String::new();
  57.     unsafe {
  58.         io::stdin().read_line(&mut input).unwrap_unchecked();
  59.     }
  60.  
  61.     let mut input = input.split_whitespace();
  62.  
  63.     unsafe {
  64.         (
  65.             input.next().unwrap().parse().unwrap_unchecked(),
  66.             input.next().unwrap().parse().unwrap_unchecked(),
  67.         )
  68.     }
  69. }
  70.  
  71. #[inline]
  72. #[allow(dead_code)]
  73. fn input_it(mut input: &mut String) -> SplitWhitespace<'_> {
  74.    unsafe {
  75.        io::stdin().read_line(&mut input).unwrap_unchecked();
  76.    }
  77.    input.split_whitespace()
  78. }
  79.  
  80. trait IterExt: std::iter::ExactSizeIterator {
  81.    #[inline]
  82.    fn filter_indexed(&mut self, mut f: impl FnMut(&Self::Item, usize) -> bool) -> Vec<Self::Item> {
  83.        let mut filter = Vec::with_capacity(self.len());
  84.  
  85.        for i in 0..self.len() {
  86.            let nxt = unsafe { self.next().unwrap_unchecked() };
  87.  
  88.            if f(&nxt, i) {
  89.                filter.push(nxt)
  90.            }
  91.        }
  92.  
  93.        filter.shrink_to_fit();
  94.        filter
  95.    }
  96. }
  97.  
  98. impl<T> IterExt for std::slice::Iter<'_, T> {}
  99.  
  100. fn main() {
  101.     let nums = {
  102.         let mut input = String::new();
  103.  
  104.         unsafe {
  105.             File::open("kek.txt")
  106.                 .unwrap_unchecked()
  107.                 .read_to_string(&mut input)
  108.                 .unwrap_unchecked();
  109.         }
  110.  
  111.         input
  112.     }
  113.     .trim()
  114.     .split('\n')
  115.     .skip(1)
  116.     .map(|i| unsafe {
  117.         let mut it = i.split(" ").map(|x| x.parse::<u32>().unwrap_unchecked());
  118.         (it.next().unwrap_unchecked(), it.next().unwrap_unchecked())
  119.     })
  120.     .collect::<Vec<_>>();
  121.  
  122.     let ans = nums
  123.         .iter()
  124.         .fold(0, |acc, (f, s)| acc + std::cmp::min(*f, *s));
  125.  
  126.     println!(
  127.         "{}",
  128.         ans + match ans % 3 {
  129.             0 => 0,
  130.  
  131.             _ => {
  132.                 let difs = nums
  133.                     .into_iter()
  134.                     .map(|(f, s)| f.abs_diff(s))
  135.                     .collect::<Vec<_>>();
  136.  
  137.                 let mut ost_one = difs.iter().filter(|&x| x % 3 == 1).collect::<Vec<_>>();
  138.                 let mut ost_two = difs.iter().filter(|&x| x % 3 == 2).collect::<Vec<_>>();
  139.  
  140.                 ost_one.sort();
  141.                 ost_two.sort();
  142.  
  143.                 match ans % 3 {
  144.                     1 => std::cmp::min(
  145.                         unsafe { **ost_two.first().unwrap_unchecked() },
  146.                         ost_one.into_iter().take(2).sum::<u32>(),
  147.                     ),
  148.  
  149.                     _ => std::cmp::min(
  150.                         unsafe { **ost_one.first().unwrap_unchecked() },
  151.                         ost_two.into_iter().take(2).sum::<u32>(),
  152.                     ),
  153.                 }
  154.             }
  155.         }
  156.     )
  157. }
  158.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement