We'd like to thank you all for participating in the contest, and hope you enjoyed it. Hope to see you again soon!
Author: 0x4F5F6F
Tutorial
#include<bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
return 0;
}
Author: _bLIC_
Lets first sort all three arrays $$$a$$$, $$$b$$$, and $$$c$$$ in increasing order of array $$$a$$$.
Lets call subarray $$$a_{l, r}$$$, $$$l\leq r$$$, good if $$$a_r-a_l\leq x$$$. For a good subarray, we can choose any $$$i$$$ and any $$$j$$$ in the subarray $$$[l, r]$$$, maintaining the given condition $$$|a_i - a_j| \leq x$$$. It is always optimal to choose $$$i$$$ with the maximum $$$b$$$ and $$$j$$$ with the maximum $$$c$$$ from good subarray.
Now, for each $$$r = 1, 2, ,..., n$$$,
- we find the minimum left index $$$l$$$ for which $$$a_{l, r}$$$ is good. This can be done by two pointers or binary search.
- Update the answer with the sum of the maximum of $$$b$$$ and the maximum of $$$c$$$ in the subarray $$$[l, r]$$$.
The maximum in the subarray can be found using a segment tree. A multiset can also be used to keep track of elements in the subarrays of $$$b$$$ and $$$c$$$ separately.
The overall time complexity will be $$$O(n\log n)$$$.
#include <bits/stdc++.h>
using namespace std;
// Source: https://codeforces.me/blog/entry/18051
template <class T> struct SegTree{
int n; T ID;
std::vector<T> tree;
T oper(T a, T b){return max(a, b);} // update this operation function according to need
SegTree(int _n, T id){
n = 1;ID = id;
while(n<_n) n<<=1;
tree.resize(2*n, ID);
}
void add(int i, T val){tree[i+n] = val;}
void build(){
for (int i = n-1;i>0;--i) tree[i] = oper(tree[i<<1], tree[i<<1|1]);
}
void update(int ind, T value){
for (tree[ind+=n] = value;ind>1;ind>>=1) tree[ind>>1] = (ind&1)?oper(tree[ind^1], tree[ind]):oper(tree[ind], tree[ind^1]);
}
T query(int l, int r){// indexing is from 0 to n-1, gives answer to [l, r)
T left = ID, right = ID; // left and right for non associative operation
for (l+=n, r+=n;l<r;l>>=1, r>>=1){
if (l&1) left = oper(left, tree[l++]);
if (r&1) right = oper(tree[--r], right);
}
return oper(left, right);
}
};
void solve(){
int n, x;cin>>n>>x;
array<int, 3> abc[n];
for (int i = 0;i<n;i++) cin>>abc[i][0];
for (int i = 0;i<n;i++) cin>>abc[i][1];
for (int i = 0;i<n;i++) cin>>abc[i][2];
sort(abc, abc+n);
SegTree<int> c(n, 0), b(n, 0);
for (int i = 0; i<n;i++){
b.add(i, abc[i][1]);
c.add(i, abc[i][2]);
}
b.build();
c.build();
int ans = 0;
int l = 0;
for (int r = 0;r<n;r++){
while(abc[r][0]-abc[l][0]>x){
l++;
}
ans = max(ans, b.query(l, r+1) + c.query(l, r+1));
}
cout<<ans;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int t;cin>>t;
while(t--){
solve();
cout<<'\n';
}
}
Author: BitWizz
Let's try to formulate this problem in a mathematical way:
Firstly, if $$$d = 0$$$, then they will meet. If $$$x1$$$ and $$$x2$$$ both are $$$0$$$, then neither BitWizz nor Kalosmi can make a move. In such a case if $$$d \neq 0$$$, then its not possible for them to meet.
We can observe that the distance between BitWizz and Kalosmi changes by $$$\pm(x1 + x2)$$$ or $$$\pm(x1 - x2)$$$. Hence, we just need to check if there is a solution to the equation :
Replace $$$(x1 + x2)$$$ with $$$t$$$ and $$$(x1 - x2)$$$ with $$$u$$$
Now, the question reduces in checking if this $$$Diophantine$$$ $$$Equation$$$ formed has a solution or not, which is equivalent to checking if $$$d$$$ is divisible by $$$gcd(t,u)$$$ i.e if $$$d$$$ is divisible by $$$gcd(x1 + x2, abs(x1 - x2))$$$ or not.
#include "bits/stdc++.h"
using namespace std;
#define int long long
#define endl "\n"
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int tc;
cin >> tc;
while (tc--) {
int x1, x2, d;
cin >> x1 >> x2 >> d;
if (x1 == 0 && x2 == 0) {
if(d == 0) cout << "YES" << endl;
else cout << "NO" << endl;
} else {
if (d % __gcd(x1 + x2, abs(x1 - x2)) == 0) cout << "YES" << endl;
else cout << "NO" << endl;
}
}
return 0;
}



