题解:P5951 [BalticOI 2003] Barrel
写在前面
参照该讨论,其他题解的做法都是有问题的,因为立方体可能因为在下沉的过程中提前触底而被迫中断下沉的过程,这一点是不能忽略的。
本题解共参考 2 篇讨论的内容,系统性地总结了解题过程中可能遇到的问题及解决方案。
正解
对于已知水位高度
情况一:d \ge 1
此时如果水位足够高,立方体排开水的体积就是整个立方体的体积
而如果水位没有立方体高,即
综上,情况一的公式就是
情况二:d \lt 1
此时如果水位足够高,立方体理应有
而如果水位没有立方体高,即
综上,情况二的公式就是
我们把以上两种情况综合一下,就能够得到最终公式:
现在对于一个确定的
我们可以把这个总和与水面下理应有的体积总和
如果该总和较
我们可以对答案
关于输入格式
由于这道题第一个测试点的输入格式有问题,如果你是写 C++ 的,n 的类型一定要设为 int 而非 double,而其他语言如果没有像 C++ 这样的格式化输入,最好自己写一个输入流。
那么输入格式的问题是什么呢?
举个例子:
假如标准输入里本应有像这样的一个整数和一个实数:
3 0.4
而这道题的输入就会变成这样:
3.4
假如有这样的一份 C++ 代码:
#include<stdio.h>
int n;
double a;
int main(){
scanf("%d%lf",&n,&a);
printf("%d %lf\n",n,a);
return 0;
}
它就可以正确地把 3.4 读成 3 0.4。
而假如你的代码像这样:
#include<stdio.h>
double n,a;
int main(){
scanf("%lf%lf",&n,&a);
printf("%lf %lf\n",n,a);
return 0;
}
它就会错误地把 n 读成 3.4,并引发后面读数的连锁错误。
这也就是为什么这位同学的代码会爆第一个点。
这里给一个 Python 代码,作为一个其他语言自行完成输入流的参考。
class input_stream:
'''提供字符串解析的输入流,不要和其他输入流(如 input、sys.stdin、os.read、ctypes 中的各种输入函数、与 REPL 有关的输入函数等)混用'''
class EOF:
'''有多种用途的占位符,不可用于字符串的拼接'''
isdigit=isprintable=lambda:False
def _read():
while True:
try:
yield from input()
yield '\n'
except EOFError:
yield input_stream.EOF
_stream=_read()
_gc=EOF
def getint():
'''读入一个整数,类似 "%d" 的格式化输入'''
while not (input_stream._gc.isdigit() or input_stream._gc=='-'):
input_stream._gc=next(input_stream._stream)
if input_stream._gc=='-':
ans='-'
input_stream._gc=next(input_stream._stream)
else:
ans=''
while input_stream._gc.isdigit():
ans+=input_stream._gc
input_stream._gc=next(input_stream._stream)
return int(ans)
def getfloat():
'''读入一个浮点数,类似 "%f" 的格式化输入'''
while not (input_stream._gc.isdigit() or input_stream._gc in ('-','.')):
input_stream._gc=next(input_stream._stream)
if input_stream._gc=='-':
ans='-'
input_stream._gc=next(input_stream._stream)
else:
ans=''
while True:
if input_stream._gc=='.':
ans+='.'
input_stream._gc=next(input_stream._stream)
while input_stream._gc.isdigit():
ans+=input_stream._gc
input_stream._gc=next(input_stream._stream)
return float(ans)
elif input_stream._gc.isdigit():
ans+=input_stream._gc
input_stream._gc=next(input_stream._stream)
else:
return float(ans)
def getstring():
'''读入一个可见字符串,类似 "%s" 的格式化输入,但比 "%s" 更智能'''
while not input_stream._gc.isprintable() or input_stream._gc==' ':
input_stream._gc=next(input_stream._stream)
ans=''
while input_stream._gc.isprintable() and input_stream._gc!=' ':
ans+=input_stream._gc
input_stream._gc=next(input_stream._stream)
return ans
def getchar():
'''读入一个字符'''
try:
if input_stream._gc==input_stream.EOF:
input_stream._gc=next(input_stream._stream)
return input_stream._gc
finally:
input_stream._gc=input_stream.EOF
def getline(seps):
'''以指定分隔符为界,读入一个字符串;
seps 参数可以是任何容器或迭代器,用来指定一个或多个分隔符;
字符串头部的分隔符会被忽略;
读完后字符串末尾的分隔符不会被消耗掉,可以使用 input_stream.getchar() 手动消耗'''
assert (seps:=frozenset(seps)) and all(len(i)==1 and isinstance(i,str) for i in seps),'seps 参数有误'
if input_stream._gc==input_stream.EOF:
input_stream._gc=next(input_stream._stream)
while input_stream._gc in seps:
input_stream._gc=next(input_stream._stream)
ans=''
while not input_stream._gc in seps:
ans+=input_stream._gc
input_stream._gc=next(input_stream._stream)
return ans
然后就是主程序部分:
eps=1e-12
s=input_stream.getint()
h=input_stream.getint()
v=input_stream.getint()
l=v/s
r=h
cubs=tuple((input_stream.getfloat(),input_stream.getfloat()) for _ in range(input_stream.getint()))
while l+eps<r:
m=(l+r)/2
if sum(a*a*min(min(d,1)*a,m) for a,d in cubs)+v>s*m:
l=m
else:
r=m
print('%.7f'%r)
这里给各位写 Py 的家人们提个醒,有很多题目的实际输入格式和它描述的输入格式是有差别的,如果你的代码莫名奇妙地 RE,就可以把这个 class input_stream 糊它脸上!