跳到正文

题解

题解:AT_arc001_4 [ARC001D] レースゲーム

yes_NT约 4 分钟
原文
文章信息与版本

文章信息

收录
2026年3月15日
更新
2026年3月15日
篇幅
约 4 分钟

版本

  1. v2当前
  2. v1

省流:严格 O(n)O(n) 。

考虑维护最左能走到的位置和最右能走到的位置形成的凸包。

(图片来自官方题解)

考虑每次怎么更新凸包。

如果左右两边没有交点,直接更新即可。

如图,如果更新右凸包的时候产生了交点,说明右边的最优策略被挡住了,这时候我们需要沿着左边一直走,直到两部分没有交点。

这样到最后一定会在终点部分形成一条线。直接统计即可。

复杂度 O(n)O(n) 。

C++
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<ctime>
using namespace std;
bool MED;
template<typename T>inline void read(T&x){
  x=0;bool f=0;char c=getchar();
  while(c<'0'||c>'9'){if(c=='-')f=1;c=getchar();}
  while(c>='0'&&c<='9')x=(x<<1)+(x<<3)+(c^48),c=getchar();
  if(f)x=~x+1;
}
#define ld long double
const int N=2e5+5;
struct point{int x,y;}f[N],g[N];
inline ld dis(const point&a,const point&b){
  return sqrtl(1ll*(a.x-b.x)*(a.x-b.x)+1ll*(a.y-b.y)*(a.y-b.y));
}
int hdf=1,hdg=1,tlf,tlg;
int l[N],r[N],n,st,ed;
ld ans;
bool MST;
int main(){
  fprintf(stderr,"%.3lf MB\n",(&MED-&MST)/1048576.0);
  read(n);read(st);read(ed);
  for(int i=0;i<=n;++i)read(l[i]),read(r[i]);
  l[0]=st,r[0]=st,l[n]=ed,r[n]=ed;

  ans=0;
  point now=(point){0,st};
  f[++tlf]=now,g[++tlg]=now;
  for(int i=1;i<=n;++i){
    while(tlf>hdf&&1ll*(l[i]-f[tlf].y)*(f[tlf].x-f[tlf-1].x)>=1ll*(f[tlf].y-f[tlf-1].y)*(i-f[tlf].x))tlf--;
    f[++tlf]=(point){i,l[i]};

    if(tlf>hdf&&tlg>hdg){
      while(hdg<tlg&&1ll*(f[hdf+1].y-g[hdg].y)*(g[hdg+1].x-g[hdg].x)>1ll*(g[hdg+1].y-g[hdg].y)*(f[hdf+1].x-g[hdg].x))
        ans+=dis(g[hdg],g[hdg+1]),hdg++;
      f[hdf]=g[hdg];
    }

    while(tlg>hdg&&1ll*(r[i]-g[tlg].y)*(g[tlg].x-g[tlg-1].x)<=1ll*(g[tlg].y-g[tlg-1].y)*(i-g[tlg].x))tlg--;
    g[++tlg]=(point){i,r[i]};

    if(tlf>hdf&&tlg>hdg){
      while(hdf<tlf&&1ll*(f[hdf+1].y-f[hdf].y)*(g[hdg+1].x-f[hdf].x)>1ll*(g[hdg+1].y-f[hdf].y)*(f[hdf+1].x-f[hdf].x))
        ans+=dis(f[hdf],f[hdf+1]),hdf++;
      g[hdg]=f[hdf];
    }

  }
  for(int i=hdf;i<tlf;++i)
    ans+=dis(f[i],f[i+1]);
  printf("%.11Lf\n",ans);
  return 0;
}
原文