Transmission Door
Greedy... Konjac Konjac proves not ...
Each time to find the largest, find the largest, the series will be divided into left and right sides, left with the STL priority queue maintenance, the tree-like array to maintain.
(The segment tree timed out ....) )
Code
#include <queue> #include <cstdio> #include <iostream> #define N 100001#define ls now << 1#define RS now << 1 | 1#define Max (x, y) (P[X].A * 2 + p[x].b > P[Y].A * 2 + p[y].b?) (x): (y)) int n, last, now, ans, m[n];std::p riority_queue <int> q; struct Node{int A, b;} P[n];inline int read () {int x = 0, f = 1;char ch = getchar (); for (;!isdigit (ch); ch = getchar ()) if (ch = = '-') F = -1;for (; IsDigit (CH); ch = getchar ()) x = (x << 1) + (x << 3) + CH-' 0 '; return x * f;} inline void Add (int x, int. D) {for (; x; x = x & x) m[x] = max (m[x], d);} inline int query (int x) {int ret = 0;for (; x <= n; x + = x & x) ret = max (ret, m[x]); return ret;} int main () {int I, j, x;n = Read (); for (i = 1; I <= n; i++) p[i].a = Read (); for (i = 1; I <= n; i++) p[i].b = Read (); fo R (i = 1; I <= n; i++) Add (i, i), last = 0;for (i = 1; I <= n; i++) {now = query (last + 1); if (Q.empty () | | | (Q.top () < (P[NOW].A-P[LAST].A) * 2 + p[now].b)) {for (j = last + 1; J &Lt Now J + +) Q.push (p[j].b); ans + = (p[now].a-p[last].a) * 2 + P[now].b;last = Now;} Else{ans + = Q.top (); Q.pop ();} printf ("%d\n", ans);} return 0;}
[luoguP2672] Salesman (greedy + tree-like array + priority queue)