跳到正文

题解

返回基地

qiaochu约 2 分钟
原文
文章信息与版本

文章信息

发布
2026年5月16日
收录
2026年5月17日
更新
2026年11月13日
篇幅
约 2 分钟

版本

  1. v1当前

考虑贪心

定义斜率: 第 ii 座山,高为 hih_i。 第 jj 座山,高为 hjh_j。 ii小于jj

这两座山之间的斜率为 (hj−hi)÷(j−i)(h_j-h_i)\div(j-i)

每次飞往后的山里面斜率最大的山即可。

几何原理:

  • 斜率最大的点,就是视线最陡的点
  • 这个点会挡住后面所有点
  • 所以它就是能直接飞到的最远点

显然可得:

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,h[1000010];
double ans=0;
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin>>n;
    for(int i=1;i<=n;i++)cin>>h[i];
    int tmp=1;
    while(tmp!=n)
    {
        double maxx=-1e10;
        int maxt;
        for(int i=tmp+1;i<=n;i++)
        {
            double cc=(h[i]-h[tmp])*1.0/(i-tmp);//斜率
            if(cc>=maxx)maxx=cc,maxt=i;
        }
        double a=h[maxt]-h[tmp],b=abs(tmp-maxt);
        tmp=maxt;
        ans+=sqrt(a*a+b*b);
    }
    double a=ans-(int)ans;
    if(a>=0.5)cout<<(int)ans+1;
    else cout<<(int)ans;
}
原文
存档评论1 条抓取于