Codeforces 875E Delivery Club dp
Posted cjlhy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces 875E Delivery Club dp相关的知识,希望对你有一定的参考价值。
首先二分答案, 我们考虑dp去check
我们可以发现所有状态要从两个点在 i 和 i - 1转移过来。
所以我们令dp[ i ] 表示 能否到达两个快递员在分别 i 号点 和 i - 1号点的状态。
转移就是把在 i 号位置的快递员不断地往后移, 如果能移到 j && abs(a[ j + 1 ] - a[ j ]) <= d 则 dp[ j + 1 ] = true;
#include<bits/stdc++.h> #define LL long long #define LD long double #define ull unsigned long long #define fi first #define se second #define mk make_pair #define PLL pair<LL, LL> #define PLI pair<LL, int> #define PII pair<int, int> #define SZ(x) ((int)x.size()) #define ALL(x) (x).begin(), (x).end() #define fio ios::sync_with_stdio(false); cin.tie(0); using namespace std; const int N = 1e5 + 7; const int inf = 0x3f3f3f3f; const LL INF = 0x3f3f3f3f3f3f3f3f; const int mod = 1e9 + 7; const double eps = 1e-8; const double PI = acos(-1); template<class T, class S> inline void add(T& a, S b) {a += b; if(a >= mod) a -= mod;} template<class T, class S> inline void sub(T& a, S b) {a -= b; if(a < 0) a += mod;} template<class T, class S> inline bool chkmax(T& a, S b) {return a < b ? a = b, true : false;} template<class T, class S> inline bool chkmin(T& a, S b) {return a > b ? a = b, true : false;} int Log[N]; struct ST { int dp[N][20]; int ty; void build(int n, int b[], int _ty) { ty = _ty; for(int i = -(Log[0]=-1); i < N; i++) Log[i] = Log[i - 1] + ((i & (i - 1)) == 0); for(int i = 1; i <= n; i++) dp[i][0] = ty * b[i]; for(int j = 1; j <= Log[n]; j++) for(int i = 1; i+(1<<j)-1 <= n; i++) dp[i][j] = max(dp[i][j-1], dp[i+(1<<(j-1))][j-1]); } inline int query(int x, int y) { int k = Log[y - x + 1]; return ty * max(dp[x][k], dp[y-(1<<k)+1][k]); } }; int n, s1, s2; int a[N]; int dp[N]; ST rmqmx, rmqmn; bool check(int d) { memset(dp, 0, sizeof(dp)); dp[2]++; dp[3]--; for(int i = 2; i <= n; i++) { dp[i] += dp[i - 1]; if(!dp[i] || abs(a[i] - a[i - 1]) > d) continue; if(i == n) return true; int p1 = i, p2 = i; int low = i, high = n, mid; while(low <= high) { mid = low + high >> 1; if(rmqmx.query(i, mid) <= a[i - 1] + d) p1 = mid, low = mid + 1; else high = mid - 1; } low = i, high = n; while(low <= high) { mid = low + high >> 1; if(rmqmn.query(i, mid) >= a[i - 1] - d) p2 = mid, low = mid + 1; else high = mid - 1; } int p = min(p1, p2); if(p == n) return true; dp[i + 1]++; dp[p + 2]--; } return false; } int main() { scanf("%d%d%d", &n, &s1, &s2); n += 2; for(int i = 3; i <= n; i++) scanf("%d", &a[i]); a[1] = s1; a[2] = s2; rmqmx.build(n, a, 1); rmqmn.build(n, a, -1); int low = abs(s1 - s2), high = 1000000000, ans = -1; while(low <= high) { int mid = low + high >> 1; if(check(mid)) ans = mid, high = mid - 1; else low = mid + 1; } printf("%d\n", ans); return 0; } /* */
以上是关于Codeforces 875E Delivery Club dp的主要内容,如果未能解决你的问题,请参考以下文章
Codeforces 1166 F. Vicky's Delivery Service 并查集+set
cocoalibspotify:music_delivery下播放回调时崩溃