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; } }