Skip to content

Instantly share code, notes, and snippets.

@farma11 farma11/ABC013C_01.cpp
Last active Jan 13, 2018

Embed
What would you like to do?
AtCoder Beginner Contest #013C – 節制
#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;
}
#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;
}
#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
You can’t perform that action at this time.