luogu P1663 山

题目链接

luogu P1663 山

题解

只需要求出下凸包的最低点就好了
显然是由两个斜率相反的直线相交来的
盼下最低点为直线的情况

代码

#include<cstdio> 
#include<iostream> 
#include<algorithm> 
inline int read() { 
    int x = 0,f = 1; 
    char c = getchar(); 
    while(c < '0' || c > '9')c = getchar(); 
    while(c <= '9' && c >= '0') x = x * 10 + c - '0',c = getchar(); 
    return x * f; 
} 
const int maxn = 10007;  
int n; 
double x[maxn],y[maxn]; 
double k[maxn],b[maxn]; 
int main() { 
    n = read(); 
    scanf("%lf%lf",&x[1],&y[1]); 
    for(int i = 2;i <= n;++ i) { 
        scanf("%lf%lf",x + i,y + i); 
        k[i] = (y[i] - y[i - 1]) / (x[i] - x[i - 1]); 
        b[i] = y[i] - k[i] * x[i]; 
    } 
    double ans = 0.0; 
    for(int i = 1;i <= n;++ i) 
        for(int j = i + 1;j <= n;++ j) {
            if((k[j] > 0 && k[i] < 0) || (k[i] > 0 && k[j] < 0))  { 
                double X = (b[i] - b[j]) / (k[j] - k[i]); 
                if(X * k[i] + b[i] > ans) ans = X * k[i] + b[i]; 
            }  
            else if(!k[j] && !k[i] && b[i] == b[j])  
                if(ans < b[i]) ans = b[i]; 
        } 
    printf("%.7lf",ans); 
    return 0; 
} 

posted @ 2018-08-10 07:11  zzzzx  阅读(134)  评论(0编辑  收藏  举报