【NOIp 2017 普及组】 跳房子

裸的单调队列优化dp+二分

我居然还调了挺久

日常审题错误

 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 typedef long long ll;
 6 
 7 const ll inf = 1e9, MAXN = 5e5 + 5;
 8 
 9 ll f[MAXN], x[MAXN], c[MAXN], d, n;
10 
11 struct mque {
12     deque <int> q;
13 
14     void push(int x) {
15         while(!q.empty() && f[q.back()] <= f[x]) q.pop_back();
16         q.push_back(x);
17     }
18     void pop_front() {q.pop_front();}
19     int front() {return q.front();}
20     bool empty() {return q.empty();}
21     void clear() {q.clear();}
22 
23 } que;
24 
25 ll dp(ll g) {
26     memset(f, ~0x3f, sizeof(f));
27     ll ans = 0, now = 0;
28     que.clear();
29     f[0] = 0;
30     for(int i = 1; i <= n; i++) {
31         for(; now < i && x[i] - x[now] >= d - g; now++) que.push(now);
32         while(!que.empty() && x[i] - x[que.front()] > d + g) que.pop_front();
33         if(que.empty()) continue;
34         f[i] = f[que.front()] + c[i];
35         ans = max(f[i], ans);
36     }
37     return ans;
38 }
39 
40 int main() {
41     ios::sync_with_stdio(false);
42     ll k, l = 0, r = inf;
43     cin >> n >> d >> k;
44     for(int i = 1; i <= n; i++)
45         cin >> x[i] >> c[i];
46     while(l < r) {
47         ll mid = (l + r) / 2;
48         if(dp(mid) < k) l = mid + 1;
49         else r = mid;
50     }
51     cout << l << endl;
52     return 0;
53 }
View Code

猜你喜欢

转载自www.cnblogs.com/zhylj/p/9503919.html