#include <bits/stdc++.h> #define int long long using namespace std; int32_t main() { int…
- User
#include <bits/stdc++.h> #define int long long
using namespace std;
int32_t main() { int t; cin >> t; while (t--) { int n; cin >> n; vector a(2 * n), b(2 * n);
for (int i = 0; i < 2 * n; i++) cin >> a[i]; for (int i = 0; i < 2 * n; i++) cin >> b[i];
vector<pair<int, int>> pairs;
for (int i = 0; i < 2 * n; i++) { pairs.emplace_back(a[i], b[i]); }
sort(pairs.begin(), pairs.end(), [](const pair<int, int> &x, const pair<int, int> &y) { return x.first + y.second < y.first + x.second; });
int t = 0;
for (int i = 0; i < n; i++) t += pairs[i].first;
for (int i = n; i < 2 * n; i++) t += pairs[i].second;
cout << t << endl;
} return 0; } convert to rust
- Assistant
Certainly! Below is the Rust version of the given C++ code:
use std::cmp::Ordering; fn main() { let mut input = String::new(); std::io::stdin().read_line(&mut input).unwrap(); let t: usize = input.trim().parse().unwrap(); for _ in 0..t { input.clear(); std::io::stdin().read_line(&mut input).unwrap(); let n: usize = input.trim().parse().unwrap(); let mut a: Vec<i64> = Vec::new(); let mut b: Vec<i64> = Vec::new(); input.clear(); std::io::stdin().read_line(&mut input).unwrap(); a.extend(input.split_whitespace().map(|s| s.parse().unwrap())); input.clear(); std::io::stdin().read_line(&mut input).unwrap(); b.extend(input.split_whitespace().map(|s| s.parse().unwrap())); let mut pairs: Vec<(i64, i64)> = Vec::new(); for i in 0..2 * n { pairs.push((a[i], b[i])); } pairs.sort_by(|x, y| { (x.0 + y.1).cmp(&(y.0 + x.1)) }); let mut t: i64 = 0; for i in 0..n { t += pairs[i].0; } for i in n..2 * n { t += pairs[i].1; } println!("{}", t); } }