LoginSignup
0
0

More than 3 years have passed since last update.

【PHP】初中級者が解くべき過去問精選 100 問を解いてみた【5問目/100】

Posted at

アルゴリズムのアウトプット

ということで、
レッドコーダーが教える、競プロ・AtCoder上達のガイドライン【中級編:目指せ水色コーダー!】@e869120さん

AtCoder で水色コーダー、つまりレーティング 1200 を少ない問題数で達成するために、茶色コーダー・緑コーダーにとって適切な教育的良問を 100 問集めました。

こちらの記事の初中級者が解くべき過去問精選 100 問
をPHPで解いていきます。

<?php

$s = trim(fgets(STDIN));
$s = str_replace(array("\r\n","\r","\n"), '', $s);
$s = explode(" ", $s);

$priceA = $s[0];
$priceB = $s[1];
$priceC = $s[2];
$needA = $s[3];
$needB = $s[4];

$priceList = [];

//AとBの少ない方の数分をABで買って足りないを買い足す場合
$buyA = 0;
$buyB = 0;
$buyC = min($needA, $needB) * 2;
if ($needA <= $needB) {
    $buyB = $needB - $buyC / 2;
} else {
    $buyA = $needA - $buyC / 2;
}
$priceList[] = $buyA * $priceA + $buyB * $priceB + $buyC * $priceC;

//Cを買わない場合
$priceList[] = $priceA * $needA + $priceB * $needB;

//Cのみ買う場合
$priceList[] = $priceC * 2 * max($needA, $needB);

echo min($priceList) . "\n";
}

全列挙するとオーバーフローするので、条件別に分けて計算量を減らす問題

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0