Wannafly挑战赛13-D

本文解析了一种关于切蛋糕的问题,通过优先队列记录每次切割的情况,并更新最小蛋糕尺寸,直至满足比例要求。最终输出所需的最少切割次数。

题解:看到大佬的写的结构体,骤然醒悟,一块蛋糕切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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值