0
0

yukicoder No.80 四角形を描こう 解説

Last updated at Posted at 2024-08-10

問題文

解説

まず、縦の長さを$i$と決めうつ。すると、残りの書ける長さは$D-2\times i$となる。
横の長さは大きくすればする方がいいので、横の長さの最大値は$\lfloor \frac{D-2\times i}{2}\rfloor$($\lfloor x\rfloor$とは、$x$以下の最大の整数のこと)。
よって、縦の長さを$i$にしたときの答えは$i\times \lfloor \frac{D-2\times i}{2}\rfloor$となる。
$i$として成り立つ数は$1\leqq i\leqq \lfloor D\rfloor$なので、

\max_{0\leqq i\leqq D}i\times \lfloor \frac{D-2\times i}{2}\rfloor\\

が答え。
普通に$i$を全探索すると、実行時間は$O(D)$なので十分高速。
また、数学的に考えると$O(1)$で解けるらしい(割愛)。

C++での解答例

#include <bits/stdc++.h>
using namespace std;

int main(){
  int D,ans=0;cin>>D;
  for(int i=1;i<=D/2;i++)
    ans=max(ans,i*((D-i*2)/2));
  cout<<ans<<endl;
}
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