切蛋糕(招行笔试)
題意:
把蛋糕切整齊,每次切的體積不超過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; }總結
- 上一篇: 云原生安全构筑下一代企业安全架构
- 下一篇: 自己用服务器搭建网盘,三步轻松搞定,还包