Doubt

Правка en1, от harshkankhar1, 2024-12-23 12:47:57

** Hello I tried to solve question 1949B - Charming Meals , first I used brute force and solved it , later i tried optimising it using binary search but I am getting a wrong answer on 14 testcase , can someone explain me what can be the edge case or whats wrong in my approach **


#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> // #include <atcoder/all> #define ordered_set tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag, tree_order_statistics_node_update> #define int long long const int mod = 1e9 + 7; const int N = 1e5 + 9; const int inf = 1e18; using namespace __gnu_pbds; using namespace std; // using namespace atcoder; void solve() { int n; cin >> n; vector<int> a(n), b(n); for(int i = 0; i < n; i++) { cin >> a[i]; } for(int i = 0; i < n; i++) { cin >> b[i]; } sort(a.begin(), a.end()); sort(b.begin(), b.end()); int ans = INT_MIN; int l = 0, r = n; while(l<r) { int mid = (l+r)/2; int val1 = INT_MAX; int val2 = INT_MAX; for(int i = 0; i < n; i++) { val1 = min(val1, abs(a[i]-b[(i+mid-1)%n])); val2 = min(val2, abs(a[i]-b[(i+mid)%n])); } if(val2 >= val1) { l = mid + 1; ans = max({ans,val1,val2}); } else { r = mid; } } // edge case removing attempt for(int i=l-10;i<l+10;i++) { int val = INT_MAX; for(int j=0;j<n;j++) { val = min(val,abs(a[j]-b[abs((j+i))%n])); } ans = max(ans,val); } cout << ans << endl; } signed main() { ios::sync_with_stdio(0); cin.tie(0); int t = 1; cin >> t; while (t--) { solve(); } return 0; }

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en1 Английский harshkankhar1 2024-12-23 12:47:57 1979 Initial revision (published)