結果
問題 | No.1012 荷物収集 |
ユーザー |
|
提出日時 | 2022-12-22 23:13:31 |
言語 | Rust (1.83.0 + proconio) |
結果 |
AC
|
実行時間 | 140 ms / 2,000 ms |
コード長 | 1,692 bytes |
コンパイル時間 | 10,794 ms |
コンパイル使用メモリ | 378,272 KB |
実行使用メモリ | 7,296 KB |
最終ジャッジ日時 | 2024-11-18 03:38:27 |
合計ジャッジ時間 | 17,666 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 41 |
ソースコード
fn main() { let mut nq = String::new(); std::io::stdin().read_line(&mut nq).ok(); let nq: Vec<usize> = nq.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); let n = nq[0]; let q = nq[1]; let mut items = (0..n).map(|_| { let mut temp = String::new(); std::io::stdin().read_line(&mut temp).ok(); let temp: Vec<isize> = temp.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); (temp[0], temp[1]) }) .collect::<Vec<_>>(); let mut x = String::new(); std::io::stdin().read_line(&mut x).ok(); let x: Vec<isize> = x.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); let mut result = vec![0isize; q]; let mut x = (0..q).map(|i| (x[i], i)).collect::<Vec<_>>(); x.sort(); items.sort(); let mut lidx = 0usize; let mut lweights = 0isize; let total_weights = items.iter().map(|&(_, w)| w).sum::<isize>(); while lidx < n && items[lidx].0 < x[0].0 { lweights += items[lidx].1; lidx += 1; } result[x[0].1] = items.iter().map(|&(pos, w)| (pos - x[0].0).abs() * w).sum::<isize>(); let mut summary = result[x[0].1]; for i in 1..q { let (px, _) = x[i-1]; let (cx, qidx) = x[i]; let diff = cx - px; summary += lweights * diff; summary -= (total_weights - lweights) * diff; while lidx < n && items[lidx].0 < cx { let (ix, iw) = items[lidx]; summary += 2 * (cx - ix) * iw; lweights += iw; lidx += 1; } result[qidx] = summary; } for &v in result.iter() { println!("{}", v); } }