paranid5

18.02 3

Feb 18th, 2022 (edited)
378
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 7.82 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. trait VecExt<T: Ord + Clone> {
  101.     fn sorted_self(self) -> Self;
  102.     fn sorted_cloned(&self) -> Self;
  103. }
  104.  
  105. impl<T: Ord + Clone> VecExt<T> for Vec<T> {
  106.     #[inline]
  107.     fn sorted_self(mut self) -> Self {
  108.         self.sort();
  109.         self
  110.     }
  111.  
  112.     #[inline]
  113.     fn sorted_cloned(&self) -> Self {
  114.         Vec::from(self.as_slice()).sorted_self()
  115.     }
  116. }
  117.  
  118. fn main() {
  119.     let nums = {
  120.         let mut input = String::new();
  121.  
  122.         unsafe {
  123.             File::open("kek.txt")
  124.                 .unwrap_unchecked()
  125.                 .read_to_string(&mut input)
  126.                 .unwrap_unchecked();
  127.         }
  128.  
  129.         input
  130.     }
  131.     .trim()
  132.     .split('\n')
  133.     .skip(1)
  134.     .map(|i| unsafe {
  135.         let mut it = i.split(" ").map(|x| x.parse::<u32>().unwrap_unchecked());
  136.         (it.next().unwrap_unchecked(), it.next().unwrap_unchecked())
  137.     })
  138.     .collect::<Vec<_>>();
  139.  
  140.     let ans = nums
  141.         .iter()
  142.         .fold(0, |acc, (f, s)| acc + std::cmp::min(*f, *s));
  143.  
  144.     println!(
  145.         "{}",
  146.         ans + match ans % 5 {
  147.             0 => 0,
  148.  
  149.             _ => {
  150.                 let osts = {
  151.                     let difs = nums
  152.                         .into_iter()
  153.                         .map(|(f, s)| f.abs_diff(s))
  154.                         .collect::<Vec<_>>();
  155.  
  156.                     (1..5)
  157.                         .map(|o| {
  158.                             difs.iter()
  159.                                 .filter(|&x| x % 5 == o)
  160.                                 .map(|&x| x)
  161.                                 .collect::<Vec<_>>()
  162.                                 .sorted_self()
  163.                         })
  164.                         .collect::<Vec<_>>()
  165.                 };
  166.  
  167.                 unsafe {
  168.                     match ans % 5 {
  169.                         /// 4:
  170.                         /// 4
  171.                         /// 3 + 1
  172.                         /// 3 + 3 + 3
  173.                         /// 2 + 2
  174.                         /// 2 + 1 + 1
  175.                         /// 1 + 1 + 1 + 1
  176.                         1 => vec![
  177.                             *osts.get_unchecked(3).first().unwrap_unchecked(),
  178.                             *osts.get_unchecked(2).first().unwrap_unchecked()
  179.                                 + *osts.get_unchecked(0).first().unwrap_unchecked(),
  180.                             osts.get_unchecked(2).iter().take(3).sum::<u32>(),
  181.                             osts.get_unchecked(1).iter().take(2).sum::<u32>(),
  182.                             osts.get_unchecked(0).iter().take(2).sum::<u32>()
  183.                                 + *osts.get_unchecked(1).first().unwrap_unchecked(),
  184.                             osts.get_unchecked(0).iter().take(4).sum::<u32>(),
  185.                         ],
  186.  
  187.                         /// 3:
  188.                         /// 4 + 4
  189.                         /// 4 + 2 + 2
  190.                         /// 3
  191.                         /// 2 + 2 + 2 + 2
  192.                         /// 2 + 1
  193.                         /// 1 + 1 + 1
  194.                         2 => vec![
  195.                             osts.get_unchecked(3).iter().take(2).sum::<u32>(),
  196.                             *osts.get_unchecked(3).first().unwrap_unchecked()
  197.                                 + osts.get_unchecked(1).iter().take(2).sum::<u32>(),
  198.                             *osts.get_unchecked(2).first().unwrap_unchecked(),
  199.                             osts.get_unchecked(1).iter().take(4).sum::<u32>(),
  200.                             *osts.get_unchecked(1).first().unwrap_unchecked()
  201.                                 + *osts.get_unchecked(0).first().unwrap_unchecked(),
  202.                             osts.get_unchecked(0).iter().take(3).sum::<u32>(),
  203.                         ],
  204.  
  205.                         /// 2:
  206.                         /// 4 + 4 + 4
  207.                         /// 4 + 3
  208.                         /// 3 + 3 + 1
  209.                         /// 2
  210.                         /// 1 + 1
  211.                         3 => vec![
  212.                             osts.get_unchecked(3).iter().take(3).sum::<u32>(),
  213.                             *osts.get_unchecked(3).first().unwrap_unchecked()
  214.                                 + *osts.get_unchecked(2).first().unwrap_unchecked(),
  215.                             osts.get_unchecked(2).iter().take(2).sum::<u32>()
  216.                                 + *osts.get_unchecked(0).first().unwrap_unchecked(),
  217.                             *osts.get_unchecked(1).first().unwrap_unchecked(),
  218.                             osts.get_unchecked(0).iter().take(2).sum::<u32>(),
  219.                         ],
  220.  
  221.                         /// 1:
  222.                         /// 4 + 2
  223.                         /// 4 + 4 + 3
  224.                         /// 4 + 4 + 4 + 4
  225.                         /// 3 + 3
  226.                         /// 2 + 2 + 2
  227.                         /// 1
  228.                         _ => vec![
  229.                             *osts.get_unchecked(3).first().unwrap_unchecked()
  230.                                 + *osts.get_unchecked(1).first().unwrap_unchecked(),
  231.                             osts.get_unchecked(3).iter().take(2).sum::<u32>()
  232.                                 + *osts.get_unchecked(2).first().unwrap_unchecked(),
  233.                             osts.get_unchecked(3).iter().take(4).sum::<u32>(),
  234.                             osts.get_unchecked(2).iter().take(2).sum::<u32>(),
  235.                             osts.get_unchecked(1).iter().take(3).sum::<u32>(),
  236.                             *osts.get_unchecked(0).first().unwrap_unchecked(),
  237.                         ],
  238.                     }
  239.                     .into_iter()
  240.                     .min()
  241.                     .unwrap_unchecked()
  242.                 }
  243.             }
  244.         }
  245.     )
  246. }
  247.  
Add Comment
Please, Sign In to add comment