P5951

· · 题解

写题解是因为另一篇有误。

Problem

有个水缸,里面东西都不会碰撞,放入 n 个立方体,给出每个立方体的棱长 l_i 和密度 \rho_i,求最后缸中水的高度。

Solution

显然,这是一道初中物理题。

对物体分类讨论:

  1. $\because F_\texttt浮=\rho_\texttt水\cdot g\cdot V_\texttt{排}

    又 \because F_\texttt{浮}=G=l_i^3\cdot\rho_i\cdot g

    \therefore V_\texttt{排}=\dfrac{l_i^3\cdot\rho_i}{\rho_\texttt{水}} \therefore$ 水面上升 $\dfrac{V_\texttt{排}}{s}

小剪枝:当高度超过 h 时可以直接输出高度 h。(另一篇题解写成了体积)

Code

#include<bits/stdc++.h>
using namespace std;
double ans,l,p;
int n,s,h,v;
int main(){
    scanf("%d%d%d%d",&s,&h,&v,&n),ans=1.*v/s;
    for(int i=1;i<=n;i++){
        scanf("%lf%lf",&l,&p);
        if(p>=1)ans+=l*l*l/s;
        else ans+=l*l*l*p/s;
        if(ans>=h)return printf("%d",h),0;
    }
    printf("%.7f",ans);
    return 0;
}