Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //credits: Benq
- #include <bits/stdc++.h>
- using namespace std;
- using ll = long long;
- using db = long double; // or double, if TL is tight
- using str = string; // yay python!
- using pi = pair<int, int>;
- using pl = pair<ll, ll>;
- using pd = pair<db, db>;
- using vi = vector<int>;
- using vb = vector<bool>;
- using vl = vector<ll>;
- using vd = vector<db>;
- using vs = vector<str>;
- using vpi = vector<pi>;
- using vpl = vector<pl>;
- using vpd = vector<pd>;
- #define tcT template<class T
- #define tcTU tcT, class U
- // ^ lol this makes everything look weird but I'll try it
- tcT> using V = vector<T>;
- tcT, size_t SZ > using AR = array<T, SZ>;
- tcT > using PR = pair<T, T>;
- // pairs
- #define mp make_pair
- #define f first
- #define s second
- // vectors
- // oops size(x), rbegin(x), rend(x) need C++17
- #define sz(x) int((x).size())
- #define bg(x) begin(x)
- #define all(x) bg(x), end(x)
- #define rall(x) x.rbegin(), x.rend()
- #define sor(x) sort(all(x))
- #define rsz resize
- #define ins insert
- #define ft front()
- #define bk back()
- #define pb push_back
- #define eb emplace_back
- #define pf push_front
- #define rtn return
- #define lb lower_bound
- #define ub upper_bound
- tcT > int lwb(V<T>& a, const T& b) { return int(lb(all(a), b) - bg(a)); }
- // loops
- #define FOR(i,a,b) for (int i = (a); i < (b); ++i)
- #define F0R(i,a) FOR(i,0,a)
- #define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
- #define R0F(i,a) ROF(i,0,a)
- #define rep(a) F0R(_,a)
- #define each(a,x) for (auto& a: x)
- //optimization & debug
- #define francesco_bernoulli ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
- #define DBG(x) cerr << #x << " = " << x << endl;
- const int MOD = 1e9 + 7; // 998244353;
- const int MX = 2e5 + 5;
- const ll INF = 1e18; // not too close to LLONG_MAX
- const db PI = acos((db)-1);
- const int dx[4] = { 1,0,-1,0 }, dy[4] = { 0,1,0,-1 }; // for every grid problem!!
- mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());
- template<class T> using pqg = priority_queue<T, vector<T>, greater<T>>;
- // bitwise ops
- // also see https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html
- constexpr int pct(int x) { return __builtin_popcount(x); } // # of bits set
- constexpr int bits(int x) { // assert(x >= 0); // make C++11 compatible until USACO updates ...
- return x == 0 ? 0 : 31 - __builtin_clz(x);
- } // floor(log2(x))
- constexpr int p2(int x) { return 1 << x; }
- constexpr int msk2(int x) { return p2(x) - 1; }
- ll cdiv(ll a, ll b) { return a / b + ((a ^ b) > 0 && a % b); } // divide a by b rounded up
- ll fdiv(ll a, ll b) { return a / b - ((a ^ b) < 0 && a % b); } // divide a by b rounded down
- tcT > bool ckmin(T& a, const T& b) {
- return b < a ? a = b, 1 : 0;
- } // set a = min(a,b)
- tcT > bool ckmax(T& a, const T& b) {
- return a < b ? a = b, 1 : 0;
- }
- tcT > void remDup(vector<T>& v) { // sort and remove duplicates
- sort(all(v)); v.erase(unique(all(v)), end(v));
- }
- long double distance(long double a, long double b, long double c, long double d) {
- long double x = a - c;
- long double y = b - d;
- return sqrt((y * y) + (x * x));
- }
- void solve() {
- int n;
- cin >> n;
- //x = 0 so miner
- //y = 0 so diamond
- vector<pair<long double, long double>> miners;
- vector<pair<long double, long double>> diamonds;
- int x, y;
- F0R(i, 2 * n) {
- cin >> x >> y;
- if (!x) {
- y = abs(y);
- long double x1 = static_cast<long double>(x), y1 = static_cast<long double>(y);
- miners.push_back({ x1, y1 });
- }
- else {
- x = abs(x);
- long double x1 = static_cast<long double>(x), y1 = static_cast<long double>(y);
- diamonds.push_back({ y1, x1 });
- }
- }
- sort(rall(miners));
- sort(rall(diamonds));
- long double ans = 1000000000.0, mini = 0;
- F0R(i, n) {
- // cout << i+1 << ". \n";
- cout << miners[i].first << " " << diamonds[i].second << "\n";
- cout << miners[i].second << " " << diamonds[i].first << "\n";
- // cout << "\n";
- mini += distance(miners[i].first, diamonds[i].second, miners[i].second, diamonds[i].first);
- }
- ans = min(ans, mini);
- sort(all(miners));
- sort(rall(diamonds));
- mini = 0;
- F0R(i, n) {
- // cout << i+1 << ". \n";
- // cout << miners[i].first << " " << diamonds[i].second << "\n";
- // cout << miners[i].second << " " << diamonds[i].first << "\n";
- // cout << "\n";
- mini += distance(miners[i].first, diamonds[i].second, miners[i].second, diamonds[i].first);
- }
- ans = min(ans, mini);
- cout.precision(15);
- cout << fixed << ans << "\n";
- }
- int main() {
- francesco_bernoulli;
- int t = 1;
- cin >> t;
- while (t--)
- solve();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement