Mice and Holes CodeForces, holescodeforces

Source: Internet
Author: User

Mice and Holes CodeForces, holescodeforces

Mice and Holes CodeForces-797F

There are n rats and m holes on a digital axis. The coordinates of the mouse are x [1],..., x [n], the coordinate of the hole is p [1],..., p [m], each hole can accommodate c [1],..., c [m]: Ask the minimum value of the sum of the distances that every mouse runs after hiding in the hole.

Analysis:

If the sequence of holes and mice is not given, a set operation is required, which is enumeration.

Therefore, the holes and mice are sorted in the order of coordinates, and then the State is defined: ans [I] [j], which indicates the minimum distance and size of the first j mice.

This is because if a mouse runs overlapping routes, the answer is definitely not the best. The first sort after this definition of the state, it implies that only the first I-1 holes put the first k mouse, then the first I hole put the k + 1 TO THE j mouse, routes do not overlap.

(I cannot think of the following parts)

However, you may not be able to think about it in practice. You must make a bold attempt (for example, try sorting, and it is very likely that the status can be defined by sorting ).

So ans [I] [j] = min (ans [I-1] [j] + 0, ans [I-1] [J-1] + {j ~ J point to I distance and}, ans [I-1] [J-2] + {J-1 ~ The Point-to-I distance of j and}..., ans [I-1] [j-c [I] + {j-c [I] + 1 ~ The distance from j to I and })

Look at ans [I] [J-1] = min (ans [I-1] [J-1] + 0, ans [I-1] [J-2] + {J-1 ~ J-1 point to I distance and},..., ans [I-1] [j-c [I]-1] + {j-c [I] ~ J-1 point to I distance and })

Taking a closer look is like taking the minimum value in a "Sliding Window", but the form cannot be accelerated, it is still too slow.

So let's take a look:

Ans [I] [j] = min (ans [I-1] [j] + 0, ans [I-1] [J-1] + 1 to j point to I distance and-1 to (J-1) point to I distance and, ans [I-1] [J-2] + 1 to j point to I distance and-1 to (J-2) point to I distance and ,..., ans [I-1] [j-c [I] + 1 to j point to I distance and-1 to (j-c [I]) point to I distance and)
Ans [I] [J-1] = min (ans [I-1] [J-1] + 0, ans [I-1] [J-2] + 1 to J-1 point to I distance and-1 to J-2 point to I distance and ,..., ans [I-1] [j-c [I]-1] + 1 to J-1 point to I distance and-1 to j-c [I]-1 point to I distance and)

That is, ans [I] [j] = the point-to-I distance from 1 to j and the point-to-point I distance from + min {ans [I-1] [j-k]-1 to j-k and} (0 <= k <= c [I])

Ans [I] [J-1] = 1 to J-1 point to I distance and + min {ans [I-1] [j-k-1]-1 to j-k-1 point to point I distance and} (0 <= k <= c [I]-1)

That is, ans [I] [j] = 1 to j point to I distance and + min (ans [I-1] [j]-1 to j point to I distance and ,..., ans [I-1] [j-c [I]-1 to j-c [I] Point to I distance and)

Ans [I] [J-1] = 1 to J-1 point to I distance and + min (ans [I-1] [J-1]-1 to J-1 location to I distance and ,..., ans [I-1] [j-c [I]-1]-1 to j-c [I]-1 point to I distance and)

In this way, we can maintain min {ans [I-1] [j-...]-...} with a monotonous queue every time I changes.

Also, ans [I] [...] is only related to ans [I-1] [...] and can save space with a rolling array.

1 # include <cstdio> 2 # include <cstring> 3 # include <algorithm> 4 using namespace std; 5 typedef long LL; // I forgot to open LL 6 struct Hole 7 {8 LL p, c; 9 bool operator <(const Hole & B) const10 {11 return p <B. p; 12} 13} h [5010]; 14 LL n, m; 15 LL x [5010]; 16 LL ans [2] [5010]; 17 // an error was written as ans [5010] [2] 18 LL q [5010], l, r, i1; 19 LL s1 [5010]. // indicates the total number of mice in the first I hole: 20 LL s2 [5010]; // s2 [j] indicates 1 ~ J's mouse to I distance and 21 int main () 22 {23 LL I, j; 24 scanf ("% I64d % I64d", & n, & m ); 25 for (I = 1; I <= n; I ++) 26 scanf ("% I64d", & x [I]); 27 for (I = 1; I <= m; I ++) 28 scanf ("% I64d % I64d", & h [I]. p, & h [I]. c); 29 sort (x + 1, x + n + 1); 30 sort (h + 1, h + m + 1); 31 for (I = 1; I <= m; I ++) 32 s1 [I] = s1 [I-1] + h [I]. c; 33 memset (ans, 0x3f, sizeof (ans); 34 ans [0] [0] = 0; // The ans [0] [..] set 035 if (n> s1 [m]) 36 {37 printf ("-1"); 38 return 0; 39} 40 for (I = 1; I <= m; I ++) 41 {42 i1 ^ = 1; 4 3 l = r = 0; 44 for (j = 1; j <= min (s1 [I], n); j ++) 45 s2 [j] = s2 [J-1] + abs (x [j]-h [I]. p); 46 // q [r ++] = 0; 47 for (j = 0; j <= min (s1 [I], n); j ++) // initially treated as 148 {49 if (r> l & q [l] <j-h [I]. c) l ++; // The j-c [I] ~ J, that is, remove the one before j-c [I, add j50 while (r> l & ans [i1 ^ 1] [q [r-1]-s2 [q [r-1]> = ans [i1 ^ 1] [j]-s2 [j]) 51 r --; 52 q [r ++] = j; 53 ans [i1] [j] = ans [i1 ^ 1] [q [l]-s2 [q [l] + s2 [j]; 54} 55} 56 printf ("% I64d", ans [m % 2] [n]); 57 return 0; 58}

Related Article

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.