結果
問題 | No.1495 パンの仕入れ |
ユーザー | akakimidori |
提出日時 | 2021-03-31 12:58:46 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 55 ms / 2,000 ms |
コード長 | 2,991 bytes |
コンパイル時間 | 16,271 ms |
コンパイル使用メモリ | 379,484 KB |
実行使用メモリ | 11,260 KB |
最終ジャッジ日時 | 2024-05-09 15:29:01 |
合計ジャッジ時間 | 15,641 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,248 KB |
testcase_02 | AC | 39 ms
7,552 KB |
testcase_03 | AC | 37 ms
7,680 KB |
testcase_04 | AC | 38 ms
7,424 KB |
testcase_05 | AC | 38 ms
7,424 KB |
testcase_06 | AC | 39 ms
7,552 KB |
testcase_07 | AC | 39 ms
7,552 KB |
testcase_08 | AC | 39 ms
7,552 KB |
testcase_09 | AC | 38 ms
7,680 KB |
testcase_10 | AC | 39 ms
7,680 KB |
testcase_11 | AC | 39 ms
7,552 KB |
testcase_12 | AC | 19 ms
7,168 KB |
testcase_13 | AC | 19 ms
7,296 KB |
testcase_14 | AC | 18 ms
7,296 KB |
testcase_15 | AC | 19 ms
7,168 KB |
testcase_16 | AC | 19 ms
7,296 KB |
testcase_17 | AC | 18 ms
7,296 KB |
testcase_18 | AC | 19 ms
7,040 KB |
testcase_19 | AC | 19 ms
7,168 KB |
testcase_20 | AC | 17 ms
7,168 KB |
testcase_21 | AC | 19 ms
7,168 KB |
testcase_22 | AC | 51 ms
11,252 KB |
testcase_23 | AC | 55 ms
11,260 KB |
testcase_24 | AC | 16 ms
6,944 KB |
testcase_25 | AC | 17 ms
6,944 KB |
testcase_26 | AC | 17 ms
6,940 KB |
testcase_27 | AC | 16 ms
6,912 KB |
testcase_28 | AC | 47 ms
11,252 KB |
testcase_29 | AC | 46 ms
11,252 KB |
testcase_30 | AC | 13 ms
6,272 KB |
testcase_31 | AC | 13 ms
6,144 KB |
testcase_32 | AC | 13 ms
6,144 KB |
testcase_33 | AC | 13 ms
6,272 KB |
testcase_34 | AC | 13 ms
6,272 KB |
testcase_35 | AC | 18 ms
7,168 KB |
testcase_36 | AC | 18 ms
7,168 KB |
testcase_37 | AC | 18 ms
7,168 KB |
testcase_38 | AC | 18 ms
7,168 KB |
testcase_39 | AC | 18 ms
7,296 KB |
testcase_40 | AC | 15 ms
6,016 KB |
testcase_41 | AC | 15 ms
6,144 KB |
testcase_42 | AC | 15 ms
6,272 KB |
testcase_43 | AC | 16 ms
6,144 KB |
testcase_44 | AC | 15 ms
6,144 KB |
testcase_45 | AC | 1 ms
5,376 KB |
testcase_46 | AC | 1 ms
5,376 KB |
testcase_47 | AC | 1 ms
5,376 KB |
ソースコード
const MAX_T: usize = 100; const MAX_N: usize = 200_000; const MAX_M: usize = 200_000; const MAX_K: i64 = 1_000_000_000; const MAX_Y: i64 = 1_000_000_000; fn read() -> Vec<(usize, i64, Vec<(usize, i64)>)> { let mut s = String::new(); use std::io::Read; std::io::stdin().read_to_string(&mut s).unwrap(); let mut it = s.trim().split_whitespace(); let mut next = || it.next().unwrap().parse::<i64>().unwrap(); let t = next() as usize; assert!(1 <= t && t <= MAX_T); let mut test = vec![]; for _ in 0..t { let n = next() as usize; assert!(1 <= n && n <= MAX_N); let m = next() as usize; assert!(1 <= m && m <= MAX_M); let k = next(); assert!(1 <= k && k <= MAX_K); let mut p = vec![(0usize, 0i64); m]; for p in p.iter_mut() { p.0 = next() as usize; assert!(1 <= p.0 && p.0 <= n); p.0 -= 1; p.1 = next(); assert!(1 <= p.1 && p.1 <= MAX_Y); } assert!( m.saturating_mul(k.max(p.iter().map(|p| p.1).max().unwrap()) as usize) <= 10usize.pow(18) ); test.push((n, k, p)); } test } fn solve(n: usize, k: i64, p: &[(usize, i64)]) -> i64 { let mut dp = vec![(0, 0, 0); n]; for &(x, y) in p.iter() { let po = &mut dp[x]; po.0 += y * y; po.1 -= 2 * y; po.2 += 1; } let eval = |m: i64| -> (i64, i64) { if m >= 0 { let mut cnt = 0; let mut cost = 0; let mut zero = false; for &(c, b, a) in dp.iter() { cost += c; zero |= a == 0; if a > 0 && 0 >= b - a { let x = (0 - (b - a)) / (2 * a); cnt += x; cost += 2 * a * (x + 1) * x / 2 + (b - a) * x; } } if zero { cnt += k; } if cnt >= k { return (cnt, cost); } } let mut cnt = 0; let mut cost = 0; let mut p = vec![0; n]; for (p, &(c, b, a)) in p.iter_mut().zip(dp.iter()) { cost += c; if a > 0 && m >= b - a { let x = (m - (b - a)) / (2 * a); cnt += x; *p = x; cost += 2 * a * (x + 1) * x / 2 + (b - a) * x; } } (cnt, cost) }; let mut ng = dp.iter().map(|&(_, b, a)| 2 * a + b - a - 1).min().unwrap(); let mut ok = dp.iter().map(|&(_, b, a)| 2 * a * k + b - a).min().unwrap(); while ok - ng > 1 { let mid = (ok + ng) / 2; if eval(mid).0 >= k { ok = mid; } else { ng = mid; } } let (x, y) = eval(ok); let ans = y - ok * (x - k); ans } fn main() { let test = read(); for (n, k, p) in test { let ans = solve(n, k, &p); println!("{}", ans); } }