题解:看到大佬的写的结构体,骤然醒悟,一块蛋糕切k次,这(k+1)块蛋糕是均匀的。所以要保存切的次数,及其对应的蛋糕大小(切了几次后的蛋糕大小),初始蛋糕的大小。
感受:以前理解的是切蛋糕是均分的,切k次每块大小为Cake/(2^k)。正解是切k次每块大小为Cake/k。
1 #pragma warning(disable:4996) 2 #include<queue> 3 #include<cstdio> 4 #include<string> 5 #include<cstring> 6 #include<iostream> 7 #include<algorithm> 8 #define ll long long 9 using namespace std; 10 11 const double INF = 1e9 + 7; 12 13 struct node { 14 double a, b; 15 int num; 16 node() {}; 17 node(double a, double b, int num) :a(a), b(b), num(num) {} 18 bool operator<(const node& i) const { 19 return b < i.b; 20 } 21 }; 22 23 int n; 24 double ratio; 25 26 int main() 27 { 28 while (cin >> ratio >> n) { 29 priority_queue<node> p; 30 31 double Min = INF; 32 for (int i = 1; i <= n; i++) { 33 double tp; 34 cin >> tp; 35 Min = min(Min, tp); 36 p.push(node(tp, tp, 1)); 37 } 38 39 int ans = 0; 40 while (true) { 41 node now = p.top(); p.pop(); 42 43 if (Min / now.b >= ratio) break; 44 45 ans++; 46 now.num++; 47 48 now.b = now.a / now.num; 49 p.push(now); 50 51 Min = min(Min, now.b); 52 } 53 54 cout << ans << endl; 55 56 } 57 return 0; 58 }
原文:https://www.cnblogs.com/zgglj-com/p/8824133.html