P8244 [COCI2013-2014#3] KOLINJE 题解
UPD:
2022/03/29:修复了题目链接(手急打错了),完善了一些语言。
原题链接
P8244 [COCI2013-2014#3] KOLINJE
思路
我们设
对于不等式:
我们将
接下来就有三种情况:
Case 1: b_i > b_{i+1}
最后判断一下,如果
Code:
实现上的一些细节:
- 注意
x 本来还有一个[0,10^7] 的范围限制。 - 要输出多几位小数,要不然精度不够就 WA 了。
- 我的代码在计算过程中采用分数避免精度问题,如果是用小数实现的要注意细节上的处理。
然后我们就可以愉快地写出以下的代码了:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
struct node{
ll fz, fm;
node (ll x, ll y){
if (y < 0) y = -y, x = -x; // 把负号存在分子
fz = x / __gcd(abs(x), y);
fm = y / __gcd(abs(x), y); // 不约分其实也可以
}
bool operator <(const node &a) const {
return (fz * a.fm < fm * a.fz);
}
bool operator >(const node &a) const {
return (fz * a.fm > fm * a.fz);
}
};
int n, a[1003], b[1003];
node xmin = node(0, 1), xmax = node(1e7, 1); // x 最开始的上下界
ll S = 0;
int main(){
scanf ("%d", &n);
for (int i = 1;i <= n;++i){
scanf ("%d%d", a+i, b+i);
S += b[i];
}
for (int i = 1;i < n;++i){
if (b[i] > b[i+1]) xmin = max(xmin, node(S * (a[i+1] - a[i]), b[i] - b[i+1])); // Case 1
else if (b[i] < b[i+1]) xmax = min(xmax, node(S * (a[i+1] - a[i]), b[i] - b[i+1])); // Case 2
else if (a[i+1] - a[i] > 0) { // Case 3
printf("-1");
return 0;
}
}
if (xmin > xmax) {
printf("-1");
return 0;
}
printf ("%.10f", (double)xmin.fz / xmin.fm);
return 0;
}