結果

問題 No.281 門松と魔法(1)
ユーザー itezpace
提出日時 2016-10-24 09:44:08
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
WA  
実行時間 -
コード長 1,436 bytes
コンパイル時間 421 ms
コンパイル使用メモリ 54,492 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-11-24 02:40:52
合計ジャッジ時間 1,953 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 47 WA * 10
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
using namespace std;
bool kadomatu(int a,int b,int c){
  if(b>a && b>c && a!=b && b!=c && c!=a){
    return true;
  } else if(b<a && b<c && a!=b && b!=c && c!=a){
    return true;
  } else {
    return false;
  }
}
int high(int a,int b,int c,int d){
  int ret=-1;
  if(kadomatu(a,b,c)){
    ret=0;
  } else {
    int x=0;
    if(a-d>=0 && a==c){
      a-=d;
      x+=1;
    }
    int g=a;
    if(a>b){
      int e=a-b;
      int f=e/d+1;
      if(a-d*f>0){
        g=a-d*f;
        x+=f;
      }
    }
    int h=c;
    if(c>b){
      int e=c-b;
      int f=e/d+1;
      if(c-d*f>0){
        h=c-d*f;
        x+=f;
      }
    }
    if(kadomatu(g,b,h)){
      ret=x;
    }
  }
  return ret;
}
int low(int a,int b,int c,int d){
  int ret=-1;
  if(kadomatu(a,b,c)){
    ret=0;
  } else {
    int x=0;
    if(a-d>=0 && a==c){
      a-=d;
      x+=1;
    }
    int e=a;
    if(c<a) e=a;
    int f=b-e;
    int g=f/d+1;
    int h=b;
    if(b-d*g>0){
      h=b-d*g;
      x+=g;
    }
    if(kadomatu(a,h,c)){
      ret=x;
    }
  }
  return ret;
}
int main(){
  int d=0;cin>>d;
  int a=0,b=0,c=0;cin>>a>>b>>c;
  int ans=-1;
  if(d==0){
    if(kadomatu(a,b,c)){
      ans=0;
    }
  } else {
    int h=high(a,b,c,d);
    int l=low(a,b,c,d);
    if(h==-1){
      ans=l;
    } else if(l==-1){
      ans=h;
    } else {
      if(h<=l){
        ans=h;
      } else if(l<=h){
        ans=l;
      }
    }
  }
  cout<<ans<<endl;
}
0