CFgym:Bonuses and Teleports(贪心)

xiaoxiao2021-02-28  116

思路:先预处理每个金币最快到达传送门的时间,然后对于金币i,要么从金币i-1位置走过来,要么在金币i-1先回传送门再从传送门走到金币i的位置,选择时间短的即可。

//Reference: Travel_poorly # include <iostream> # include <cstdio> # include <cstring> # include <cstdlib> # include <algorithm> using namespace std; const int maxn = 2e5+3; const int INF = 0x7fffffff; typedef long long LL; int a[maxn], b[maxn], c[maxn], d[maxn]; int main() { int n, m; scanf("%d%d",&n,&m); a[n+1] = INF; for(int i=1; i<=n; ++i) scanf("%d",&a[i]); for(int i=1; i<=m; ++i) scanf("%d",&b[i]); int j=0; for(int i=1; i<=m; ++i) { while(a[j+1] <= b[i]) ++j; if(j) c[i] = b[i]-a[j]; else c[i] = INF; if(j<n) d[i] = a[j+1]-b[i]; else d[i] = INF; c[i] = min(c[i], d[i]); } LL ans = 0; b[0] = b[m+1] = a[1]; for(int i=1; i<=m+1; ++i) { LL tmp = abs((LL)b[i-1] - b[i]); tmp = min(tmp, LL(c[i-1])+c[i]); ans += tmp; } printf("%lld\n",ans); return 0; }

转载请注明原文地址: https://www.6miu.com/read-66064.html

最新回复(0)