7 solutions
-
0
二分,找最接近他的数字
#include<iostream> #include<algorithm> using namespace std; #define ll long long const int N = 1e5 + 5; ll m, n, ans; ll a[N], b[N]; ll Next(ll l) { ll x = a[l]; for (ll i = l + 1; i <= m; ++i) { if (a[i] != x)return a[i]; } return x; } ll lbreach(ll x) { ll l = 0, r = m, mid; while (l < r) { mid = (l + r) >> 1; if (a[mid] >= x)r = mid; else l = mid + 1;//就是本坐标 } if (a[l] == x) return 0; if (l == 1) return a[1] - x; ll Numnext = Next(l - 1); return min(abs(Numnext - x), abs(a[l - 1] - x)); } int main() { scanf("%lld%lld", &m, &n); for (ll i = 1; i <= m; ++i)scanf("%lld", &a[i]); for (ll i = 1; i <= n; ++i)scanf("%lld", &b[i]); sort(a + 1, a + 1 + m); for (ll i = 1; i <= n; ++i) { ans += lbreach(b[i]); } printf("%lld\n", ans); return 0; }
Information
- ID
- 280
- Time
- 1000ms
- Memory
- 256MiB
- Difficulty
- 8
- Tags
- # Submissions
- 266
- Accepted
- 51
- Uploaded By