1. 程式人生 > 實用技巧 >codeforces 1336 B 思維+二分

codeforces 1336 B 思維+二分

碎點

題意:給三個陣列,讓你從每個陣列中各挑一個數滿足兩兩之差的平方和最小。

思路:若想滿足兩兩之和的平方差最小,則滿足確定一個數t,其餘兩個數各是其陣列中大於等於t和小於等於t的數。

  用lower和--upper即可

#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <string>
#include <map>
#include <iomanip>
#include <algorithm>
#include 
<queue> #include <stack> #include <set> #include <vector> // #include <bits/stdc++.h> #define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); #define sp ' ' #define endl '\n' #define inf 0x3f3f3f3f; #define FOR(i,a,b) for( int i = a;i <= b;++i) #define bug cout<<"--------------"<<endl #define
P pair<int, int> #define fi first #define se second #define pb(x) push_back(x) #define ppb() pop_back() #define mp(a,b) make_pair(a,b) #define ms(v,x) memset(v,x,sizeof(v)) #define rep(i,a,b) for(int i=a;i<=b;i++) #define repd(i,a,b) for(int i=a;i>=b;i--) #define sca3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c)) #define
sca2(a,b) scanf("%d %d",&(a),&(b)) #define sca(a) scanf("%d",&(a)); #define sca3ll(a,b,c) scanf("%lld %lld %lld",&(a),&(b),&(c)) #define sca2ll(a,b) scanf("%lld %lld",&(a),&(b)) #define scall(a) scanf("%lld",&(a)); using namespace std; typedef long long ll; ll gcd(ll a,ll b){return b?gcd(b,a%b):a;} ll lcm(ll a,ll b){return a/gcd(a,b)*b;} ll powmod(ll a, ll b, ll mod){ll sum = 1;while (b) {if (b & 1) {sum = (sum * a) % mod;b--;}b /= 2;a = a * a % mod;}return sum;} const double Pi = acos(-1.0); const double epsilon = Pi/180.0; const int maxn = 2e5+10; ll ans; ll pf(ll x) { return x * x; } void solve(vector<int>a,vector<int>b,vector<int>c) { for(int x : a){ auto y = lower_bound(b.begin(),b.end(),x); auto z = upper_bound(c.begin(),c.end(),x); if(y == b.end() || z == c.begin()) continue; z--; ll tmp = pf(x-*y) + pf(x-*z) + pf(*y-*z); ans = min(ans,tmp); } } int main() { //freopen("input.txt", "r", stdin); int _; scanf("%d",&_); while(_--) { ans = 9e18; int nr,ng,nb; cin>>nr>>ng>>nb; std::vector<int>r(nr),g(ng),b(nb); rep(i,0,nr-1){ cin>>r[i]; } rep(i,0,ng-1){ cin>>g[i]; } rep(i,0,nb-1){ cin>>b[i]; } sort(r.begin(), r.end()); sort(g.begin(), g.end()); sort(b.begin(), b.end()); solve(r,g,b); solve(r,b,g); solve(g,r,b); solve(g,b,r); solve(b,r,g); solve(b,g,r); cout<<ans<<endl; } }