题解:AT_abc424_e [ABC424E] Cut in Half
Fail_Void_Vanish · · 题解
首先容易想到用合并果子的方法做,用优先队列辅助,时间复杂度为
于是考虑换种方法,我们用
一开始先压入
这能干嘛呢?
记当前二元组为
首先默认这里有弹出,另外实现时不要忘了。
如果
如果 break)即可。
做完
然后我们思考发现一开始压入
:::info[时间复杂度证明]{open}
先来看最后的输出过程,最坏时间复杂度为
显然通过上面的过程可得:对于一个值
由于需要弹出一次,因此每次对优先队列内元素的个数影响为增加
然后考虑
盲胡一个最坏情况是
| 那么时间复杂度为 |
|---|
:::success[实现]{open}
namespace lolcrying {
signed main () {
// int tot = 0;
n = read() ,k = read() ,x = read();
priority_queue < pair <double ,int> > q;
up(i ,1 ,n)
q.push({read() ,1});
while(1) {
double val = q.top().first ;
int appear = q.top().second;
q.pop();
// ++ tot;
if(appear > k) {
q.push({val / 2.0 ,k * 2});
q.push({val ,appear - k});
break;
}
q.push({val / 2.0 ,appear * 2});
k -= appear;
}
int rk = 0;
// writeln(tot);
while(1) {
double val = q.top().first ;
int appear = q.top().second;
q.pop();
rk += appear;
if(rk >= x) {
printf("%.10lf\n" ,val);
break;
}
}
return 0;
}
}
:::