141B - Hopscotch - CodeForces Solution


geometry math *1400

Please click on ads to support us..

Python Code:

from math import ceil

a, x, y = map(int, input().split())
l = y // a + ceil((y / a) % 1)
if ((l > 1 and l % 2 == 1) and (x == 0 or x >= a or x <= -a)) or (
        (l == 1 or l % 2 == 0) and (x >= a / 2 or x <= -a / 2)) or y % a == 0:
    print("-1")
    exit(0)

if l == 1:
    print("1")
    exit(0)

if l % 2 == 0:
    ans = 2 + (l / 2 - 1) * 3
else:
    ans = 2 + ((l - 1) / 2 - 1) * 3
    ans += 2 if x > 0 else 1

print(str(int(ans)))

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define yes cout<<"YES"<<endl;
#define no cout<<"NO"<<endl;
#define t int t; scanf("%d",&t); while(t--)
typedef long long ll;
const ll mod = 1e9+7;
const ll inf = 1LL<<30;
const int N = 2e5+10;
map<int,int> mp1;
map<int,int>mp2;
vector<int>v;
set<int>ss;
int b[N];
int c[N];
int a[N];
int dfs=1e9+7;
int main()
{
    int x,y,a,i=0;
    bool f=1;
    int cnt=0;
    i=0;
    f=1;
    cin>>a>>x>>y;
    if(y%a==0)
    {
        cout<<-1<<endl;
        return 0;
    }
    if(a>y)
    {
        if(abs(2*x)<a)
            cout<<1<<endl;
        else cout<<-1<<endl;
        return 0;
    }

 cnt=0;
 y-=a;
    while((i+a)<y)
    {
        i+=a;
        if(f==1) cnt++;
        else cnt+=2;
        f=!f;


    }
if(f==1) cnt++;
else cnt+=2;

    if(f==1&&abs(2*x)<a)
   cout<<cnt+1<<endl;
  else if(f==0&&abs(x)<a&&x!=0)
  cout<<cnt+1-(x<0)<<endl;
    else cout<<-1<<endl;





}


Comments

Submit
0 Comments
More Questions

791. Custom Sort String
787. Cheapest Flights Within K Stops
779. K-th Symbol in Grammar
701. Insert into a Binary Search Tree
429. N-ary Tree Level Order Traversal
739. Daily Temperatures
647. Palindromic Substrings
583. Delete Operation for Two Strings
518. Coin Change 2
516. Longest Palindromic Subsequence
468. Validate IP Address
450. Delete Node in a BST
445. Add Two Numbers II
442. Find All Duplicates in an Array
437. Path Sum III
436. Find Right Interval
435. Non-overlapping Intervals
406. Queue Reconstruction by Height
380. Insert Delete GetRandom O(1)
332. Reconstruct Itinerary
368. Largest Divisible Subset
377. Combination Sum IV
322. Coin Change
307. Range Sum Query - Mutable
287. Find the Duplicate Number
279. Perfect Squares
275. H-Index II
274. H-Index
260. Single Number III
240. Search a 2D Matrix II