Codeforces Round #436 C

Bus

思路:一定经过k次f点,那么从f点开始模拟,需要k-1次,第一次旅行0-f 和 最后一次 f-a(或f-0)分别在最前和最后算,所以中间模拟k-1次经过f点的情况

AC代码:

#include "iostream"
#include "iomanip"
#include "string.h"
#include "stack"
#include "queue"
#include "string"
#include "vector"
#include "set"
#include "map"
#include "algorithm"
#include "stdio.h"
#include "math.h"
#pragma comment(linker, "/STACK:102400000,102400000")
#define bug(x) cout<<x<<" "<<"UUUUU"<<endl;
#define mem(a,x) memset(a,x,sizeof(a))
#define step(x) fixed<< setprecision(x)<<
#define mp(x,y) make_pair(x,y)
#define pb(x) push_back(x)
#define ll long long
#define endl ("\n")
#define ft first
#define sd second
#define lrt (rt<<1)
#define rrt (rt<<1|1)
using namespace std;
const ll mod=1e9+7;
const ll INF = 1e18+1LL;
const int inf = 1e9+1e8;
const double PI=acos(-1.0);
const int N=1e5+100;

int a,b,f,k,ans;
int main(){
    cin>>a>>b>>f>>k;
    if(k==1){
        if(f>b || a-f>b){
            cout<<-1<<endl;
            return 0;
        }
        else if(b>=a) cout<<0<<endl;
        else cout<<1<<endl;
        return 0;
    }
    int now=b, pos=0, t=a-f , q=1;
    if(b<2*t){
        cout<<-1<<endl;
        return 0;
    }
    if(k>2 && b<2*f){
        cout<<-1<<endl;
        return 0;
    }
    now-=f;
    for(int i=2; i<=k; ++i){
        if(q){
            if(now>=2*t){
                 now-=2*t;
            }
            else{
                ans++, now=b;
                now-=2*t;
            }
            q=0;
        }
        else{
            if(now>=2*f){
                 now-=2*f;
            }
            else{
                ans++, now=b;
                now-=2*f;
            }
            q=1;
        }
    }//cout<<ans<<" "<<now<<" "<<q<<" "<<f<<" "<<t<<endl;
    if(q && now<t) ans++;
    if(!q && now<f) ans++;
    cout<<ans<<endl;
    return 0;
}

 

posted on 2017-09-25 21:39  lazzzy  阅读(131)  评论(0编辑  收藏  举报

导航