当前位置: 移动技术网 > IT编程>开发语言>C/C++ > agc007D - Shik and Game(dp 单调性)

agc007D - Shik and Game(dp 单调性)

2019年01月28日  | 移动技术网IT编程  | 我要评论

姜玉阳官网,活该我爱你,丑小鸭

题意

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)\)

#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
*/

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网