Skip to content

Instantly share code, notes, and snippets.

@ctylim
Created September 19, 2015 11:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ctylim/bc484408512faf037e06 to your computer and use it in GitHub Desktop.
Save ctylim/bc484408512faf037e06 to your computer and use it in GitHub Desktop.
yukicoder No.281
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cmath>
#include <queue>
#include <stack>
#define repd(i,a,b) for (int i=(a);i<(b);i++)
#define rep(i,n) repd(i,0,n)
typedef long long ll;
const int max_int = 2147483647;
using namespace std;
int inputValue(){
int a;
cin >> a;
return a;
};
void inputArray(int * p, int a){
rep(i, a){
cin >> p[i];
}
};
void inputVector(vector<int> * p, int a){
rep(i, a){
int input;
cin >> input;
p -> push_back(input);
}
}
template <typename T>
void output(T a, int precision) {
if(precision > 0){
cout << setprecision(precision) << a << "\n";
}
else{
cout << a << "\n";
}
}
bool isTake(int a, int b, int c){
if (a < 0 || b < 0 || c < 0){
return false;
}
if ((a < b && b > c && a != c) || (a > b && b < c && a != c)) {
return true;
}
return false;
}
bool isTakev(vector<int> V){
return isTake(V[0], V[1], V[2]);
}
int main(int argc, const char * argv[]) {
// source code
int d = inputValue();
vector<int> H(3);
rep(i, 3){
H[i] = inputValue();
}
// case -1
if (isTake(H[0], H[1], H[2])) {
output(0, 0);
return 0;
}
if (!isTake(H[0], H[1], H[2]) && d == 0) {
output(-1, 0);
return 0;
}
// 真ん中を最小にする場合と最大にする場合両方調べる
int seedC = 0;
int seedE = 0;
if (H[0] == H[2]) {
H[0] = max(0, H[0] - d);
if(H[2] != 0) {
// 両端が0で同じだとどうしようもないので排除
seedC += 1;
seedE += 1;
}
}
vector<int> newHC = H;
// 真ん中を最小にしたい場合 (真ん中に魔法をかける)
if (H[0] <= H[1] && H[0] <= H[2]) {
newHC[1] = H[1] - ((H[1] - H[0]) / d + 1) * d;
if (newHC[1] < 0) newHC[1] = 0;
seedC += (H[1] - H[0]) / d + 1;
}
if (H[0] <= H[1] && H[0] >= H[2]) {
newHC[1] = H[1] - ((H[1] - H[2]) / d + 1) * d;
if (newHC[1] < 0) newHC[1] = 0;
seedC += (H[1] - H[2]) / d + 1;
}
vector<int> newHE = H;
// 真ん中を最大にしたい場合 (両端に魔法をかける)
if (H[0] >= H[1]){
newHE[0] = H[0] - ((H[0] - H[1]) / d + 1) * d;
if (newHE[0] < 0) newHE[0] = 0;
seedE += (H[0] - H[1]) / d + 1;
}
if (H[2] >= H[1]){
newHE[2] = H[2] - ((H[2] - H[1]) / d + 1) * d;
if (newHE[0] < 0) newHE[0] = 0;
seedE += (H[2] - H[1]) / d + 1;
}
if (newHE[0] == newHE[2]){
newHE[0] = max(0, newHE[0] - d);
if(newHE[2] != 0) {
// 両端が0で同じだとどうしようもないので排除
seedE += 1;
}
}
if (!isTakev(newHC)) {
seedC = max_int;
}
if (!isTakev(newHE)) {
seedE = max_int;
}
if (seedC == max_int && seedE == max_int) {
output(-1, 0);
return 0;
}
else{
output(min(seedC, seedE), 0);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment