1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
| struct Node { x: i32, w: i32, d: i32, id: i32, }
fn main() { let mut input = String::new(); std::io::stdin().read_line(&mut input).unwrap(); let mut s = input.trim().split(' '); let n: usize = s.next().unwrap().trim().parse().unwrap(); let c: i32 = s.next().unwrap().trim().parse().unwrap();
let mut a = Vec::new(); for _ in 0..n { input.clear(); std::io::stdin().read_line(&mut input).unwrap(); while input.trim().len() <= 0 { std::io::stdin().read_line(&mut input).unwrap(); }
let mut s1 = input.trim().split(' '); let x: i32 = s1.next().unwrap().trim().parse().unwrap(); let w: i32 = s1.next().unwrap().trim().parse().unwrap(); let d: i32 = s1.next().unwrap().trim().parse().unwrap(); let id: i32 = s1.next().unwrap().trim().parse().unwrap();
a.push(Node { x, w, d, id }); }
a.sort_by(|a, b| { if a.x < b.x { return std::cmp::Ordering::Less; } else if a.x == b.x { return std::cmp::Ordering::Equal; } else { return std::cmp::Ordering::Greater; } });
let mut f : Vec<Vec<i64>> = vec![vec![0; n]; n]; let mut g : Vec<Vec<i64>> = vec![vec![0; n]; n]; let mut sum : Vec<i64> = vec![0; n];
sum[0] = a[0].w as i64; for i in 1..n { sum[i] = sum[i - 1] + a[i].w as i64; }
for i in 0..n { let tmp = sum[n - 1] * a[i].d as i64; f[i][i] = tmp; g[i][i] = tmp; }
for len in 1..n { for i in 0..n { let j = (i + len) % n; f[i][j] = min( f[(i + 1) % n][j] + dis(i, (i + 1) % n, c, &a) as i64 * enemy((i + 1) % n, j, n, &sum), g[(i + 1) % n][j] + dis(i, j, c, &a) as i64 * enemy((i + 1) % n, j, n, &sum), ); g[i][j] = min( g[i][(j + n - 1) % n] + dis((j + n - 1) % n, j, c, &a) as i64 * enemy(i, (j + n - 1) % n, n, &sum), f[i][(j + n - 1) % n] + dis(i, j, c, &a) as i64 * enemy(i, (j + n - 1) % n, n, &sum), ); } }
let mut ans = min(f[0][n - 1], g[0][n - 1]); let mut l = 0; let mut r = n - 1; for i in 1..n { let t = min(f[i][i - 1], g[i][i - 1]); if t < ans { ans = t; l = i; r = i - 1; } }
println!("{}", ans);
if f[l][r] < g[l][r] { outputf(l, r, n, &a, c, &sum, &f, &g); } else { outputg(l, r, n, &a, c, &sum, &f, &g); }
println!(""); }
fn enemy(i: usize, j: usize, n: usize, sum: &[i64]) -> i64 { if i > j { return sum[i - 1] - sum[j]; } else if i == 0 { return sum[n - 1] - sum[j]; } else { return sum[n - 1] - sum[j] + sum[i - 1]; } }
fn min(a: i64, b: i64) -> i64 { if a < b { a } else { b } } fn abs(x: i32) -> i32 { if x < 0 { -x } else { x } } fn dis(i: usize, j: usize, c: i32, a: &[Node]) -> i32 { if i <= j { abs(a[i].x - a[j].x) } else { c - abs(a[i].x - a[j].x) } } fn outputf( l: usize, r: usize, n: usize, a: &[Node], c: i32, sum: &Vec<i64>, f: &Vec<Vec<i64>>, g: &Vec<Vec<i64>>, ) { if r == l { print!("{} ", a[l].id); return; } else if f[(l + 1) % n][r] + dis(l, (l + 1) % n, c, a) as i64 * enemy((l + 1) % n, r, n, sum) < g[(l + 1) % n][r] + dis(l, r, c, a) as i64 * enemy((l + 1) % n, r, n, sum) { outputf((l + 1) % n, r, n, a, c, sum, f, g); } else { outputg((l + 1) % n, r, n, a, c, sum, f, g); } print!("{} ", a[l].id); } fn outputg( l: usize, r: usize, n: usize, a: &[Node], c: i32, sum: &Vec<i64>, f: &Vec<Vec<i64>>, g: &Vec<Vec<i64>>, ) { if r == l { print!("{} ", a[l].id); return; } else if g[l][(r + n - 1) % n] + dis((r + n - 1) % n, r, c, a) as i64 * enemy(l, (r + n - 1) % n, n, sum) < f[l][(r + n - 1) % n] + dis(l, r, c, a) as i64 * enemy(l, (r + n - 1) % n, n, sum) { outputg(l, (r + n - 1) % n, n, a, c, sum, f, g); } else { outputf(l, (r + n - 1) % n, n, a, c, sum, f, g); } print!("{} ", a[r].id); }
|