harshkankhar1's blog

By harshkankhar1, history, 14 hours ago, In English

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

Full text and comments »

  • Vote: I like it
  • -26
  • Vote: I do not like it

By harshkankhar1, history, 13 days ago, In English

1982D - Beauty of the mountains


#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 = 1e9; using namespace __gnu_pbds; using namespace std; // using namespace atcoder; void solve() { int n, m, k; cin >> n >> m >> k; vector<vector<int>> mountain(n, vector<int>(m)); vector<string> mountain_type(n); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> mountain[i][j]; } } for (int i = 0; i < n; i++) { cin >> mountain_type[i]; } int snowy = 0, not_snowy = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (mountain_type[i][j] == '0') { not_snowy += mountain[i][j]; } else { snowy += mountain[i][j]; } } } int diff = abs(snowy - not_snowy); vector<vector<int>> prefix(n + 1, vector<int>(m + 1, 0)); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { prefix[i][j] = (mountain_type[i - 1][j - 1] - '0') + prefix[i - 1][j] + prefix[i][j - 1] - prefix[i - 1][j - 1]; } } set<int> factors; for (int i = k; i <= n; i++) { for (int j = k; j <= m; j++) { int total_snowy = prefix[i][j] - prefix[i - k][j] - prefix[i][j - k] + prefix[i - k][j - k]; factors.insert(abs(k * k - 2 * total_snowy)); } } int tot_gcd = (factors.size()==0) ? 0 : *factors.begin(); if(factors.size()==0) { if(diff == 0) { cout << "YES" << endl; } else { cout << "NO" << endl; } return; } for (auto x : factors) { if (x == 0) { continue; } tot_gcd = __gcd(tot_gcd, x); } if (diff % tot_gcd == 0) { cout << "YES" << endl; } else { cout << "NO" << endl; } } signed main() { ios::sync_with_stdio(0); cin.tie(0); int t = 1; cin >> t; while (t--) { solve(); } return 0; }

Full text and comments »

  • Vote: I like it
  • -7
  • Vote: I do not like it