864C - Bus - CodeForces Solution


greedy implementation math *1500

Please click on ads to support us..

Python Code:

l=[int(x) for x in input().split()]
a=[]
b=l[1]
f=0
c=0
 
a.append(0)
 
for i in range(l[3]):
    if(i%2==0):
        a.append(i*l[0]+l[2])
    else:
        a.append((i+1)*l[0]-l[2])
a.append(l[0]*l[3])
 
for j in range(1,l[3]+2):
    
    if(b>=(a[j]-a[j-1])):
        b=b-a[j]
        b=b+a[j-1]
        
    else:
        
        b=l[1]
        if(b>=a[j]-a[j-1]):
            f=f+1
            b=b-(a[j]-a[j-1])
        else:
            c=-1 
            break
if(c==-1):
    print(c)
else:
    print(f)

C++ Code:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int a,b,f,k;
int main(){
	cin>>a>>b>>f>>k;
	int t=0,b1=b,i=0,ans=0;
	while(k--){
		if(i==0){
			t=f;
			b1=b1-f;
			if(b1<0){
				cout<<-1;
				return 0;
			}
			if(b1<2*(a-f)&&k!=0){
				b1=b;
				ans++;
			}
			else if(k==0&&b1<a-f){
				b1=b;
				ans++;
			}
			t=a;
			b1=b1-(a-f);
			
			if(b1<0){
				cout<<-1;
				return 0;
			}
			i=1;
		}
		else if(i==1){
			t=f;
			b1=b1-(a-f);
			if(b1<0){
				cout<<-1;
				return 0;
			}
			if(b1<2*f&&k!=0){
				b1=b;
				ans++;
			}
			else if(k==0&&b1<f){
				b1=b;
				ans++;
			}
			t=0;
			b1=b1-f;
			if(b1<0){
				cout<<-1;
				return 0;
			}
			i=0;
		}
//		cout<<b1<<" "<<ans<<endl;
	}
	cout<<ans;
	return 0;
}
 				   	  		 	 	 	  			   	 	


Comments

Submit
0 Comments
More Questions

53A - Autocomplete
1729G - Cut Substrings
805B - 3-palindrome
805C - Find Amir
676C - Vasya and String
1042B - Vitamins
1729F - Kirei and the Linear Function
25D - Roads not only in Berland
1694A - Creep
659F - Polycarp and Hay
1040A - Palindrome Dance
372A - Counting Kangaroos is Fun
1396B - Stoned Game
16A - Flag
1056A - Determine Line
670B - Game of Robots
1418C - Mortal Kombat Tower
1382B - Sequential Nim
1272C - Yet Another Broken Keyboard
808A - Lucky Year
1245A - Good ol' Numbers Coloring
58B - Coins
1041C - Coffee Break
507A - Amr and Music
1041D - Glider
1486A - Shifting Stacks
1389B - Array Walk
71B - Progress Bar
701A - Cards
545A - Toy Cars