LuoGu P1731 生日蛋糕

2022-05-31 03:06:18 字數 1486 閱讀 3805

\(\color}\)

7月17日是 \(mr.w\) 的生日,\(acm-thu\) 為此要製作乙個體積為的 \(m\) 層生日蛋糕,每層都是乙個圓柱體。設從下往上數第 \(i\)

\((1 \leq i \leq m)\) 層蛋糕是半徑為 \(r_i\) , 高度為 \(h_i\) 的圓柱。當 \(i時,要求 \(r_i>r_\) 且 \(h_i>h_\) 由於要在蛋糕上抹忌廉,為盡可能節約經費,我們希望蛋糕外表面(最下一層的下底面除外)的面積 \(q\) 最小。令 \(q = s\pi\) ,請程式設計對給出的 \(n\) 和 \(m\) ,找出蛋糕的製作方案(適當的 \(r_i\) 和 \(h_i\) 的值),使 \(s\) 最小。(除 \(q\) 外,以上所有資料皆為正整數)

\(\color}\)

有兩行,第一行為 \(n\) ,表示待製作的蛋糕的體積為 \(n\pi\);第二行為 \(m\),表示蛋糕的層數為 \(m\)。

\(\color}\)

僅一行,是乙個正整數 \(s\)(若無解則 \(s=0\))。

\(\color}\)

\(1 \leq n \leq 20000 , 1 \leq m \leq 15\).

\(\color}\)

圓柱相關公式:\(v=\pi r^2h\)體積 ;側面積 \(s'=2 \pi rh\);底面積 \(s=\pi r^2\) 。

\(\color}\)

首先不難想到的是搜尋,但直接搜尋會很慢,考慮到要加一些剪枝:

最優性剪枝:

1、當前面積加上下一層的最小側面積比記錄的 \(ans\) 大,那麼該種方案定然不是最優的;

2、如果剩餘體積對應的下一層側面積加上當前面積大於等於 \(ans\) ,該方案不是最優的(必須是大於等於因為下一層的半徑取不到當前的 \(r\) )

乙個小技巧是預處理出每一層的最小體積以及最小面積

\(\color}\)

#include #define ll long long

#define reg register

using namespace std;

const int kmax = 21;

const int inf = 0x7f7f7f7f;

int mins[kmax], minv[kmax];

int n, m, ans = inf;

void dfs(int v, int s, int p, int r, int h)

if (v + minv[p - 1] > n) return ; // 可行性剪枝

if (s + mins[p - 1] > ans) return ; // 最優性剪枝

if (s + (n - v) / r * 2 >= ans) return ; //最優性剪枝

for (reg int i = r - 1; i >= p; --i)

}int main()

\(\color}\)

\(noi\ 1999\)

Luogu P1731 生日蛋糕 dfs剪枝

include include using namespace std int n,m,r 55 h 55 minn 0x7ffffff inline int min int x,int y inline void dfs int now,int rest,int s,int z if rest 0...

P1731 生日蛋糕

原題鏈結 dfs 注意剪枝 這個剪枝還挺麻煩的 首先是最優性剪枝 如果當前的面積 之後可行的最小面積仍然大於現在的最優解 那麼捨去 可行性剪枝 如果當前的體積 之後可行的最大體積仍小於規定體積 捨去從大到小列舉是為了方便確定範圍 include include include include inc...

洛谷 P1731 生日蛋糕

7月17日是mr.w的生日,acm thu為此要製作乙個體積為n 的m層 生日蛋糕,每層都是乙個圓柱體。設從下往上數第i 1 i m 層蛋糕是半徑為ri,高度為hi的圓柱。當i include include include include using namespace std const int...