"POJ" 1160 Post Office

Source: Internet
Author: User

http://poj.org/problem?id=1160

Test instructions: There are N cities on a straight line, and there is a post office in the city of P, asking how to build a P post office to make each city to the nearest post office and the smallest. (n<=300, P<=30&p<=n)

#include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include < iostream>using namespace Std;const int n=1005;typedef long long ll;ll sum[n], D[n][n];int N, p, X[n], s[n][n];//t= (k+j+ 1)/2, W (k, j) = (2*t-j-k) *x[t]-sum (k, t-1) +sum (t+1, J) inline ll W (int i, int j) {int t= (i+j+1) >>1; return (LL) ((t< <1)-i-j) *x[t]-sum[t-1]+sum[i-1]+sum[j]-sum[t]; }int Main () {while (~scanf ("%d%d", &n, &p)) {for (int i=1; i<=n; ++i) scanf ("%d", &x[i]); sort (x+1, x+1+n); fo R (int i=1; i<=n; ++i) sum[i]=sum[i-1]+x[i];for (int i=2; i<=n; ++i) d[1][i]=w (1, i), s[1][i]=1;for (int i=2; i<=p; ++i) {s[i][n+1]=n;for (int j=n; j>=1;--j) {int l=s[i-1][j], r=s[i][j+1], &pos=s[i][j]; ll &now=d[i][j];now=~ 0ull>>1;for (int k=l; k<=r; ++k) {ll t=d[i-1][k-1]+w (k, j); if (now>=t) now=t, Pos=k;}}} printf ("%lld\n", D[p][n]);} return 0;}

  

Mom, I didn't think of the equation. Qaq really is SB = =

Start from small to big sort once again $x$.

Set $d (i, j) $ to indicate the minimum distance of $i$ post offices currently in the former $j$ cities:

$ $d (i, J) =min \{D (i-1, k-1) +w (k, j) \}, i<j$$

where $w (i, j) $ represents the cost of $i to j$ to build a post office. Apparently the post office was built on the median = =, making $t= \lfloor \frac{i+j+1}{2} \rfloor$, apparently $w (i, j) = (t-i) x[t]-sum (i, t-1) + (J-T) x[t]-sum (T+1, J) $

Find and quadrilateral inequality is too far away ah = = (although the subject of violence can be over = =)

We tried to re-prove ...

1, $w (i, j) $ obviously satisfies the interval monotonicity (in fact, this is completely less than the bottom of the below = =)

2, first prove $w (I, j) $ of the quadrilateral inequality = =, that is, prove $w (i, j+1)-W (i, j) $ about $i$ monotonically decreasing

$$
\begin{align}
W (i, j+1)-W (i, J)
& = \left ((t-i) x[t]-sum (i, t-1) + (J+1-T) x[t]-sum (t+1, j+1) \right)-\left ((t-i) x[t]-sum (i, t-1) + (J-T) x[t]-sum (t+ 1, j) \right) \ \
& = \left ((j+1-t) x[t]-sum (t+1, j+1) \right)-\left ((j-t) X[t]-sum (t+1, j) \right) \ \
& = X[t]-x[j+1]
\end{align}
$$

Obviously $t < j+1$ is $x[t]-x[j+1] \le 0$ so $w$ satisfies the quadrilateral inequality = =

There is no need to prove that the $d$ also satisfies the quadrilateral = = because of the particularity of the topic, we directly prove the interval monotonicity of the decision $s$ =

Set $d_{k} (i, j) = d (i-1, k-1) + W (k, J) $

Set $k \le K ' \le J < j+1$ by quadrilateral inequalities

$ $w (k, J) +w (K ', j+1) \le W (k, j+1) + W (k ', j) $$

Both sides plus $d (i-1, k-1) +d (i-1, K '-1) $, into

$ $d _{k} (i, J) +d_{k '} (i, j+1) \le d_{k} (i, j+1) + d_{k '} (i, j) $$

That

$ $d _{k} (i, j)-D_{k '} (i, J) \le D_{k} (i, j+1)-d_{k '} (i, j+1) $$

So to all $d_{k '} (i, J) \le D_{k} (i, j) \rightarrow D_{k '} (i, j+1) \le d_{k} (i, j+1) $

So obviously, $s (I, J) \le s (i, j+1) $. The same can be $s (i, j+1) \le s (i+1, j+1) $

So get $s (i, j-1) \le s (i, J) \le S (i+1, j) $ spicy!

But found! Mom, it's not good to move in the same length! Because $d (i, i+1) $ is a good trouble to calculate! We decided to deform the inequality!

Due to $s (I, J) \le S (i+1, J) \rightarrow S (i-1, J) \le S (i, j) $, $s (i, j-1) \le s (i, J) \rightarrow S (i, J) \le s (i, j+1) $.

And then you get the

$ $s (i-1, J) \le S (i, J) \le (i, j+1) $$

This takes $i$ as the stage and then pushes the $j$ to get spicy! So it's so hot.

"POJ" 1160 Post Office

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.