★[Dynamic Planning] [line segment tree] Base Station Site Selection

Source: Internet
Author: User
[Problem description] There are n villages located in a straight line, and the distance between the I (I> 1) villages and the 1st villages is di. No more than k Communication Base stations need to be established in these villages. The cost of building a base station in the village I is CI. If a communication base station is established within the distance of the village I, it will be overwritten. If village I is not covered, you need to compensate them for the cost of WI. The problem is that the base station location is selected to minimize the total cost. [Input format] the first line of the base. In input file contains two integers, N, and K. The meaning is described above. The second line contains N-1 integers, representing D2, D3 ,..., DN, this number of N-1 is increasing. The third row contains N integers, indicating C1, C2 ,... CN. The fourth row contains N integers, indicating S1, S2 ,..., Sn. The fifth line contains N integers, indicating W1, W2 ,..., Wn. Output Format: The base. Out output file contains only one integer, indicating the minimum total cost. [Input sample] 3 21 22 3 21 1 010 20 30 [output sample] 4 [data scale] n <= 40%; 500 of data, k <= n, k <= 100, n <= 20,000, di <= 1000000000, CI <= 10000, Si <= 1000000000, wi <= 10000.

This topic describes the dynamic planning of line tree optimization.

First, it is not difficult to think of a simple method:
Status: F [k] [I] indicates the minimum cost of building the K base station in the first I Village of the I Village.
F [k] [I] = min (F [k-1] [p] + cost (P, I) + C [I].

In this case, the time complexity is O (n ^ 3 · K), which obviously times out.

One idea is to optimize the computation of cost (P, I) to reduce the enumeration volume, but the complexity is still high.

For further consideration, the scope of non-compensation for village I is [L [I], R [I]. We can see that, with the incremental enumeration of K and I (K in the outer loop), If I build a base station at a certain position, P in the village on the left of I needs to be compensated, then after I ', P must also be compensated.

Therefore, you can open an array to maintain the current cost of each location, and find the minimum cost each time, plus C [I.

However, the time efficiency is still low.

Speaking of the interval, it is not difficult to think of a line segment tree, right! It can optimize the time complexity to (O (K · nlog n )).
Sort the villages in the ascending order of R. For each enumeration, I first set the cost of each vertex to the corresponding value in F [k-1, then enumerate each P (R [p] <D [I]) and add all W [p] to the corresponding range [0, L [p, then, the minimum value maintained in the line segment tree is used to calculate the current F [k] [I], that is, the minimum value is added with C [I].

When updating the ANS value, add the remaining P after enumeration I to the line segment tree in sequence according to the above rules. Then, update the ANS with the minimum value maintained by the line segment tree.
Accode:

# Include <cstdio> # include <cstdlib> # include <algorithm> # include <string> # define min (a, B) (a) <(B )? (A): (B) const char fi [] = "base. in "; const char fo [] =" base. out "; const int maxn = 20010; const int max = 0x3f3f3f3f; const int min = ~ Max; struct segtree {int L, R, LC, RC, Min, sum ;}; segtree tr [maxn <1]; int f [maxn], L [maxn], R [maxn]; int pre [maxn], ord [maxn]; int d [maxn], C [maxn], s [maxn], W [maxn]; int N, k, TOT; void init_file () {freopen (FI, "r", stdin); freopen (FO, "W", stdout); return;} inline int getint () {int res = 0; char TMP; while (! Isdigit (TMP = getchar (); Do res = (RES <3) + (RES <1) + TMP-'0 '; while (isdigit (TMP = getchar (); Return res;} inline bool CMPL (const Int & A, const Int & B) {return l [a] <L [B];} inline bool cflat (const Int & A, const Int & B) {return R [a] <R [B];} void readdata () {n = getint (); k = getint (); For (INT I = 2; I <= N; ++ I) d [I] = getint (); For (INT I = 1; I <= N; ++ I) C [I] = getint (); for (INT I = 1; I <= N; ++ I) s [I] = getint (); For (INT I = 1; I <= N; ++ I) W [I] = getint (); For (INT I = 1; (ORD [I] = I) <= N; ++ I) L [I] = d [I]-s [I], R [I] = d [I] + s [I]; STD: Sort (ORD + 1, ord + n + 1, CMPL); For (INT I = 1, P = 1; I <= N; ++ I) while (P <= N & L [ord [p] <= d [I]) Pre [ord [p ++] = I; // records the first Vertex on the left of the vertex within the scope of its non-compensation. STD: Sort (ORD + 1, ord + n + 1, CMIP); return;} inline void Pushdown (INT p) {tr [tr [p]. LC]. sum + = tr [p]. SUM; tr [tr [p]. RC]. sum + = tr [p]. SUM; tr [tr [p]. LC]. min + = tr [p]. SUM; tr [tr [p]. RC]. min + = tr [p]. SUM; tr [p]. sum = 0; return;} inline void Update (INT p) {tr [p]. min = min (TR [tr [p]. LC]. min, TR [tr [p]. RC]. min); return;} void build (int l, int R) {int now = ++ tot; tr [now]. L = L, TR [now]. R = r, TR [now]. sum = 0; If (L = r) {tr [now]. min = f [l]; return;} int mid = (L + r)> 1; tr [now]. lc = tot + 1; build (L, mid); tr [now]. rc = tot + 1; build (Mid + 1, R); Update (now); return;} void add (int p, int L, int R, int delta) {If (L <= tr [p]. L & R> = tr [p]. r) {tr [p]. sum + = delta, TR [p]. min + = delta; return;} Pushdown (p); // The flag must be passed down. Int mid = (TR [p]. L + Tr [p]. r)> 1; if (L <= mid) add (TR [p]. LC, L, R, Delta); If (mid <r) add (TR [p]. RC, L, R, Delta); Update (p); return;} void work () {memset (F, 0x3f, sizeof F); F [0] = 0; int ans = max; For (int K = 0, P, I; k <k + 1; ++ K) {tot = 0; build (0, N ); // For ease of computing, a virtual node is 0. For (I = p = 1; I <n + 1; ++ I) {(; P <= N & R [ord [p] <D [I]; ++ p) add (1, 0, pre [ord [p]-1, W [ord [p]); F [I] = tr [1]. min + C [I] ;}for (; P <= N; ++ p) add (1, 0, pre [ord [p]-1, W [ord [p]); // you need to add all the post-I fees to the total fees. Ans = min (ANS, TR [1]. min);} printf ("% d \ n", ANS); return;} int main () {init_file (); readdata (); Work (); Return 0 ;} # UNDEF min

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.