日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

切蛋糕(招行笔试)

發布時間:2023/12/16 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 切蛋糕(招行笔试) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題意:
把蛋糕切整齊,每次切的體積不超過k.

參考:
https://www.nowcoder.com/discuss/164334?type=1

每次下降一個高度,計算當前累積的cut。當大于k時,跳出循環,cuth+1,切掉所有大于cuth的蛋糕。

#include<iostream> #include<vector> #include<algorithm> using namespace std;int main() {int n,k;cin>>n>>k;vector<int>h(n);for (int i=0;i<n;i++){cin>>h[i];}int hmax = *max_element(h.begin(),h.end());int hmin = *min_element(h.begin(),h.end());sort(h.begin(),h.end());for (int i=0;i<h.size();i++){cout<<h[i]<<" ";}cout<<endl;int cuth = hmax;int cut = 0;int res = 0;cout<<"begin: "<< cuth<<endl;vector<bool>v(n,0);while (cuth>hmin){while(true){cuth--;cut = 0; // eath time, go down a height, and then compute the sum of cut;for (int i=0;i<n;i++)v[i] = 0;cout<<"in cuth: "<<cuth<<endl;for (int i=n-1;i>=0;i--){if (h[i]>cuth){cut += h[i]-cuth;v[i] = 1;}else break; }if (cut>k)break;}cuth++;res++;cout<<"out cuth: "<<cuth<<endl;cout<<"res: "<<endl;cout<<"--------------"<<endl;for (int i=n-1;i>=0;i--){if (h[i]>cuth){h[i] = cuth;}else break;}}cout<<res<<endl;return 0; }

總結

以上是生活随笔為你收集整理的切蛋糕(招行笔试)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。