题意
题目链接
Sol
主人公的最优决策一定是经过熊->返回到某个位置->收集经过的钻石
那么可以直接设(f[i])表示收集完了前(i)个位置的钻石的最小时间,转移的时候枚举下最后收集的位置
[f[i] =min(f[j], p[i] - p[j 1] max(T, 2 * (p[i] - p[j 1])))]
至于为啥对(T)取个max,是因为我可以先返回,然后等到可以捡的时候再走,这样走的时候的贡献就抵消掉了
这时候我们可以直接二分 线段树就行了
但是考虑这个式子各个变量的单调性,(f[i])是单调递增的,(p[i])是单调递增的。
也就是说对于某个前缀是从(2 * (p[i] - p[j 1]))转移而来,对于剩下的是从(T)转移而来,可以直接记录一下转移的位置,以及前缀最小值就行了
复杂度:(O(n))
代码语言:javascript复制#include<bits/stdc .h>
#define Pair pair<int, int>
#define MP(x, y) make_pair(x, y)
#define fi first
#define se second
//#define int long long
#define LL long long
#define Fin(x) {freopen(#x".in","r",stdin);}
#define Fout(x) {freopen(#x".out","w",stdout);}
//#define getchar() (p1 == p2 && (p2 = (p1 = buf) fread(buf, 1, 1<<22, stdin), p1 == p2) ? EOF : *p1 )
char buf[(1 << 22)], *p1 = buf, *p2 = buf;
using namespace std;
const int MAXN = 2e5 10, mod = 998244353, INF = 1e9 10;
const double eps = 1e-9;
template <typename A, typename B> inline bool chmin(A &a, B b){if(a > b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline bool chmax(A &a, B b){if(a < b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline LL add(A x, B y) {if(x y < 0) return x y mod; return x y >= mod ? x y - mod : x y;}
template <typename A, typename B> inline void add2(A &x, B y) {if(x y < 0) x = x y mod; else x = (x y >= mod ? x y - mod : x y);}
template <typename A, typename B> inline LL mul(A x, B y) {return 1ll * x * y % mod;}
template <typename A, typename B> inline void mul2(A &x, B y) {x = (1ll * x * y % mod mod) % mod;}
template <typename A> inline void debug(A a){cout << a << 'n';}
template <typename A> inline LL sqr(A x){return 1ll * x * x;}
inline int read() {
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 c - '0', c = getchar();
return x * f;
}
LL N, E, T, a[MAXN], f[MAXN];
int main() {
N = read(); E = read(); T = read();
memset(f, 0x3f, sizeof(f));
for(int i = 1; i <= N; i ) a[i] = read();
f[0] = 0; f[1] = T;
LL mn = 1e18, j = 0;
for(int i = 2; i <= N; i ) {
while(T <= 2 * (a[i] - a[j 1]) && j < i) chmin(mn, f[j] - 2 * a[j 1]), j ;
chmin(f[i], mn 2 * a[i]);
chmin(f[i], f[j] T);
}
cout << f[N] E;
return 0;
}
/*
3 9 23333
1 3 8
*/