Hey, I am having an issue with this problem: https://codeforces.com/contest/1117/problem/C .
I am getting testcase number 7 wrong for this problem in this submission: https://codeforces.com/contest/1117/submission/104940838
My idea for this problem is to basically calculate the prefix sums for the x and y directions(where U and R are positive and L and D are negative). I also calculate the distance between the final and starting Xs and Ys. Then I binary search through the answer and check whether the specific day works. I think my issue has to do with the check function since the binary search function is copied from USACO Guide. Here is my code for this problem:
Edit: I checked the official CF solution, and I am pretty sure that my check function actually has the same logic as the solution. However, I could be wrong
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MXN = 1e5+2;
ll totx, toty, n;
string s;
//Prefix sums
ll px[MXN], py[MXN];
bool check(ll days){
ll x = totx;
ll y = toty;
//Compute how much the wind is going to shave off from the distance we need to travel
x-= (days/n) * px[n] + px[days%n];
y-= (days/n) * py[n] + py[days%n];
//return false if the distance left to travel after the computation of the wind
// is more than the number of days we allotted
return abs(x+y) <=days;
}
//Binary search
ll fstTrue(ll lo, ll hi) {
// returns hi+1 if no x satisfies f
for (hi ++; lo < hi; ) {
ll mid = (lo+hi)/2;
check(mid) ? hi = mid : lo = mid+1;
}
return lo;
}
int main() {
ll x1, y1, x2, y2;
cin>>x1>>y1>>x2>>y2;
totx = x2 - x1, toty = y2 - y1;
cin>>n>>s;
// Calculate prefix sums
px[0] = 0, py[0] = 0;
for(ll i=1; i<=n; i++){
px[i] = px[i-1], py[i] = py[i-1];
if(s[i-1]=='U') py[i]++;
else if(s[i-1]=='D') py[i]--;
else if(s[i-1]=='R') px[i]++;
else if(s[i-1]=='L') px[i]--;
}
ll ans = fstTrue(1, 1e18);
if(ans==1000000000000000001) cout<<-1;
else cout<<ans;
}
Thank you