二维空间中存在若干条连续的折线,折线方向从左下到右上或者右下到左上。一个人沿着这个折线走从下方走到上方。每条折线上可能会有可以直达上方最近折线的线,这种线只能通往最近上方折线,而且走这条线只是y坐标增加。走折线需要一定时间,走直上的线也需要一定的时间。求最少时间到达折线y坐标最大的点。
求第K大。。偷懒并没有写平衡树又写了二分+树状数组。。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define FOR(i,j,k) for(i=j;i<=k;++i)
#define rep(i,j,k) for(i=j;i<k;++i)
const int N = 300005;
typedef long long ll;
struct Data {
int x, y, v, t;
Data() { }
Data(int a, int b, int c, int d) : x(a), y(b), v(c), t(d) { }
} p[N];
int c[N], ax[N], x[N], id[N], n;
ll ans[N];
int sum(int i) {
int s = 0;
for (; i; i -= i & -i) s += c[i];
return s;
}
void add(int i, int x) {
for (; i <= n; i += i & -i) c[i] += x;
}
int binary(int k) {
int l = 0, r = n, ans = 0;
while (l <= r) {
int mid = l + r >> 1;
if (sum(mid) < k) l = mid + 1;
else r = mid - 1, ans = l;
}
return ans;
}
int main() {
int i, j, k, pos, cnt, v, y, xx, t, l, r, g, gg; ll pre, mx;
while (scanf("%d", &n) != EOF) {
FOR(i,0,n) scanf("%d", &x[i]);
cnt = y = 0;
rep(i,0,n) {
scanf("%d%d", &v, &j);
p[cnt++] = Data(x[i], ++y, v, -1);
while (j--) {
scanf("%d%d", &xx, &t);
p[cnt++] = Data(xx, ++y, v, t);
}
}
p[cnt++] = Data(x[n], ++y, 0, -1);
n = 0;
rep(i,0,cnt) {
if (p[i].t != -1 && p[i + 1].t == -1 && p[i].x == p[i + 1].x)
swap(p[i], p[i + 1]);
ax[++n] = p[i].x;
}
sort(ax + 1, ax + n + 1);
n = unique(ax + 1, ax + n + 1) - (ax + 1);
pre = 0;
memset(c, 0, sizeof c);
rep(i,0,cnt-1) {
ans[i] = pre;
gg = l = lower_bound(ax + 1, ax + n + 1, p[i].x) - ax;
g = r = lower_bound(ax + 1, ax + n + 1, p[i + 1].x) - ax;
if (l > r) swap(l, r);
mx = pre + (ll) abs(ax[r] - ax[l]) * p[i].v;
while (sum(r) - sum(l - 1)) {
k = binary(sum(l - 1) + 1);
add(k, -1);
pos = ax[k];
k = id[k];
mx = min(mx, ans[k] + p[k].t + (ll)abs(ax[g] - pos) * p[i].v);
}
if (p[i].t != -1)
add(gg, 1), id[gg] = i;
pre = mx;
}
printf("%I64d\n", pre);
}
return 0;
}
Cableways
Description
Vova bought a new wide-angle lens for his camera a few days ago. Now he wants to take a panoramic photo with this lens, and that is the reason why he decided to climb the nearest hill.
There is a single narrow serpentine path leading from base of the hill to its top. Vova introduced coordinate axes in such a way that
OY
is directed along the hill side from base to top and
OX
is orthogonal to it. The path is represented as a polyline with
n
segments, vertices of which follow in the order of strictly increasing
Moreover, Vova can use cableways. They start at some point of the path, run strictly up the hill parallel to
Help Vova climb the hill before it gets dark! Note that Vova is so determined that he will refuse to go down the hill even if this action helps him to climb the hill faster.
Input
The first line contains a number of segments in the path
n(1≤n≤105)
. The next line contains
n+1
integers which are x-coordinates of polyline vertices in the order from base to top. No two consecutive vertices have the same
x
-coordinate. The
Output
Output the minimal time required to climb the hill.
Sample
input
4
0 5 15 10 0
1 1 1 100
1 1 7 1
1 0
1 0
output
15
input
3
0 10 0 10
10 0
10 1 10 1
10 0
output
101