Advertisement
paranid5

07.02 3

Feb 7th, 2022
2,101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 4.72 KB | None | 0 0
  1. use std::{
  2.     collections::VecDeque,
  3.     fmt::Debug,
  4.     fs::File,
  5.     io,
  6.     io::Read,
  7.     iter::FromIterator,
  8.     num::ParseIntError,
  9.     str::{FromStr, SplitWhitespace},
  10. };
  11.  
  12. #[inline]
  13. #[allow(dead_code)]
  14. fn input() -> String {
  15.     let mut input = String::new();
  16.     unsafe {
  17.         io::stdin().read_line(&mut input).unwrap_unchecked();
  18.     }
  19.     input
  20. }
  21.  
  22. #[inline]
  23. #[allow(dead_code)]
  24. fn input_parse<T: FromStr<Err = ParseIntError> + Debug>() -> T {
  25.     let mut input = String::new();
  26.     unsafe {
  27.         io::stdin().read_line(&mut input).unwrap_unchecked();
  28.         input.trim().parse().unwrap_unchecked()
  29.     }
  30. }
  31.  
  32. #[inline]
  33. #[allow(dead_code)]
  34. fn input_container<T, C>() -> C
  35. where
  36.     T: FromStr<Err = ParseIntError> + Debug,
  37.     C: FromIterator<T>,
  38. {
  39.     let mut input = String::new();
  40.     unsafe {
  41.         io::stdin().read_line(&mut input).unwrap_unchecked();
  42.     }
  43.  
  44.     input
  45.         .split_whitespace()
  46.         .map(|x| unsafe { x.parse::<T>().unwrap_unchecked() })
  47.         .collect::<C>()
  48. }
  49.  
  50. #[inline]
  51. #[allow(dead_code)]
  52. fn input_pair<F, S>() -> (F, S)
  53. where
  54.     F: FromStr<Err = ParseIntError> + Debug,
  55.     S: FromStr<Err = ParseIntError> + Debug,
  56. {
  57.     let mut input = String::new();
  58.     unsafe {
  59.         io::stdin().read_line(&mut input).unwrap_unchecked();
  60.     }
  61.  
  62.     let mut input = input.split_whitespace();
  63.  
  64.     unsafe {
  65.         (
  66.             input.next().unwrap().parse().unwrap_unchecked(),
  67.             input.next().unwrap().parse().unwrap_unchecked(),
  68.         )
  69.     }
  70. }
  71.  
  72. #[inline]
  73. #[allow(dead_code)]
  74. fn input_it(mut input: &mut String) -> SplitWhitespace<'_> {
  75.    unsafe {
  76.        io::stdin().read_line(&mut input).unwrap_unchecked();
  77.    }
  78.    input.split_whitespace()
  79. }
  80.  
  81. pub trait IterExt: std::iter::ExactSizeIterator {
  82.    #[inline]
  83.    fn filter_indexed(&mut self, mut f: impl FnMut(&Self::Item, usize) -> bool) -> Vec<Self::Item> {
  84.        let mut filter = Vec::with_capacity(self.len());
  85.  
  86.        for i in 0..self.len() {
  87.            let nxt = unsafe { self.next().unwrap_unchecked() };
  88.  
  89.            if f(&nxt, i) {
  90.                filter.push(nxt)
  91.            }
  92.        }
  93.  
  94.        filter.shrink_to_fit();
  95.        filter
  96.    }
  97. }
  98.  
  99. impl<T> IterExt for std::slice::Iter<'_, T> {}
  100.  
  101. fn main() {
  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.     let input = input.split('\n').collect::<Vec<_>>();
  112.  
  113.     let m = unsafe {
  114.         input
  115.             .get_unchecked(0)
  116.             .split(' ')
  117.             .skip(1)
  118.             .next()
  119.             .unwrap_unchecked()
  120.             .parse::<u32>()
  121.             .unwrap_unchecked()
  122.     };
  123.  
  124.     let mut products = input
  125.         .iter()
  126.         .skip(1)
  127.         .map(|x| unsafe {
  128.             let mut it = x.split(' ');
  129.             (
  130.                 it.next()
  131.                     .unwrap_unchecked()
  132.                     .parse::<u32>()
  133.                     .unwrap_unchecked(),
  134.                 it.next().unwrap_unchecked(),
  135.             )
  136.         })
  137.         .collect::<Vec<_>>();
  138.     products.sort();
  139.  
  140.     let mut a_products = products
  141.         .iter()
  142.         .filter(|(_, x)| *x == "A")
  143.         .collect::<Vec<_>>();
  144.  
  145.     let mut other_products = products
  146.         .iter()
  147.         .filter(|(_, x)| *x != "A")
  148.         .collect::<VecDeque<_>>();
  149.  
  150.     let mut sum = 0;
  151.  
  152.     let max_len = products
  153.         .iter()
  154.         .take_while(|(x, _)| {
  155.             if sum + x > m {
  156.                 false
  157.             } else {
  158.                 sum += x;
  159.                 true
  160.             }
  161.         })
  162.         .count();
  163.  
  164.     sum = 0;
  165.  
  166.     let mut cur_a_products = a_products
  167.         .iter()
  168.         .take_while(|(x, _)| {
  169.             if sum + x > m {
  170.                 false
  171.             } else {
  172.                 sum += x;
  173.                 true
  174.             }
  175.         })
  176.         .collect::<Vec<_>>();
  177.  
  178.     let mut cur_other_products_len = 0;
  179.  
  180.     loop {
  181.         if sum + unsafe { other_products.front().unwrap_unchecked().0 } > m {
  182.             break;
  183.         }
  184.  
  185.         cur_other_products_len += 1;
  186.         sum += unsafe { other_products.pop_front().unwrap_unchecked().0 };
  187.     }
  188.  
  189.     while cur_a_products.len() + cur_other_products_len < max_len {
  190.         sum -= unsafe { cur_a_products.pop().unwrap_unchecked().0 };
  191.  
  192.         loop {
  193.             if sum + unsafe { other_products.front().unwrap_unchecked().0 } > m {
  194.                 break;
  195.             }
  196.  
  197.             cur_other_products_len += 1;
  198.             sum += unsafe { other_products.pop_front().unwrap_unchecked().0 };
  199.         }
  200.     }
  201.  
  202.     println!("{} {}", cur_a_products.len(), m - sum);
  203. }
  204.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement