Last active
January 13, 2018 16:09
-
-
Save farma11/aef144472dd95e6e57fe5f0e9e00c251 to your computer and use it in GitHub Desktop.
AtCoder Beginner Contest #013C – 節制
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <algorithm> | |
using namespace std; | |
#define LL long long int | |
LL n, h; | |
LL a, b, c, d, e; | |
// 質素な食事の最低限の回数が求められる | |
inline LL minCheapFood(double x){ | |
return ((n - x)*e - h - b*x) / (d + e) + 1; | |
} | |
int main(){ | |
cin >> n >> h; | |
cin >> a >> b >> c >> d >> e; | |
LL cost = 1e14; | |
for (LL x = 0; x <= n; x++){ | |
LL y = max(0LL, minCheapFood(x)); | |
if (x + y <= n){ | |
cost = min(cost, (LL)(a*x + c*y)); | |
} | |
} | |
cout << cost << endl; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <algorithm> | |
using namespace std; | |
#define LL long long int | |
LL n, h; | |
LL a, b, c, d, e; | |
// 普通の食事の日数: x, 質素な食事の日数: y | |
inline bool isNotHunger(LL x, LL y){ | |
return h + b*x + d*y - e*(n - x - y) > 0; | |
} | |
int main(){ | |
cin >> n >> h; | |
cin >> a >> b >> c >> d >> e; | |
LL cost = 1e14; | |
LL x = 0, y = 0; // 普通の食事の日数: x, 質素な食事の日数: y | |
// 普通の食事だけの場合、必要となる回数を求める | |
while (n > x && !isNotHunger(x, 0)) x++; | |
while (1){ | |
//条件を満たすなら最低値を更新 | |
if (isNotHunger(x, y)) cost = min(cost, a*x + c*y); | |
else break; | |
//条件を満たさなくなるまでxを減らす(減らせないなら終了) | |
if (x == 0) break; | |
while (isNotHunger(x, y)) x--; | |
//条件を満たすまでyを増やす(増やせないなら終了) | |
if (x + y == n) break; | |
while (!isNotHunger(x, y)) y++; | |
} | |
cout << cost << endl; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <algorithm> | |
using namespace std; | |
long n, h; | |
int a, b, c, d, e; | |
// 普通の食事の日数: x, 質素な食事の日数: y | |
inline bool isNotHunger(int x, int y){ | |
return h + b*x + d*y - e*(n - x - y) > 0; | |
} | |
int main(){ | |
cin >> n >> h; | |
cin >> a >> b >> c >> d >> e; | |
long cost = 1e10; | |
for (int x = 0; x <= n; x++){ | |
for (int y = 0; y <= n - x; y++){ | |
if(isNotHunger(x, y)){ | |
cost = min(cost, long(a*x + c*y)); | |
} | |
} | |
} | |
cout << cost << endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment