Codeforces 30D King's Problem? Simulation

Source: Internet
Author: User

First, sort n points, find the K after sorting, and then discuss the situation.

When k = n + 1, it is obviously a small value of k-> 1-> n | k-> n-> 1, because the sum of the two sides of the triangle is greater than the third side.

When 1 <= k & k <= n:

1, k-> 1-> n + 1-> k + 1-> n | k-> n + 1-> k-1-> 1, the corresponding step is ignored when k + 1 | K-1 does not exist.

2, k-> 1-> n + 1-> n-> k + 1 | k-> n + 1-> 1-> k-1, the corresponding step is ignored when k + 1 | K-1 does not exist.

3. This is a wonderful strategy. At the beginning, I intuitively thought that this situation would not exist, but five WA people taught me how to behave.

K-> I-> n + 1-> I-1-> 1 (I <k) | k-> I-> 1-> n + 1-> I + 1-> n (I> k ).

It can be understood that, at the expense of abs (site [k]-site [I]), the total value is minimized by changing the starting point, and the IQ is urgent.

# Include <algorithm> # include <iostream> # include <cstring> # include <cstdlib> # include <cstdio> # include <queue> # include <cmath> # include <stack> # include <map> # include <ctime> # include <iomanip> # pragma comment (linker, "/STACK: 1024000000"); # define EPS (1e-6) # define _ LL long # define ULL unsigned long # define LL _ int64 # define INF 0x3f3f3f3f # define Mod 1000000007/** I/O Accelerator Interface .. **/# Define g (c = getchar () # define d isdigit (g) # define p x = x * 10 + c-'0' # define n x = x * 10 + '0'-c # define pp l/= 10, p # define nn l/= 10, ntemplate <class T> inline T & RD (T & x) {char c; while (! D); x = c-'0'; while (d) p; return x;} template <class T> inline T & RDD (T & x) {char c; while (g, c! = '-'&&! Isdigit (c); if (c = '-') {x = '0'-g; while (d) n ;} else {x = c-'0'; while (d) p;} return x;} inline double & RF (double & x) // scanf ("% lf ", & x); {char c; while (g, c! = '-' & C! = '.'&&! Isdigit (c); if (c = '-') if (g = '. ') {x = 0; double l = 1; while (d) nn; x * = l;} else {x = '0'-c; while (d) n; if (c = '. ') {double l = 1; while (d) nn; x * = l ;}} else if (c = '. ') {x = 0; double l = 1; while (d) pp; x * = l;} else {x = c-'0'; while (d) p; if (c = '. ') {double l = 1; while (d) pp; x * = l ;}} return x ;} # undef nn # undef pp # undef n # undef p # undef d # undef gusing namespace std; LL num [100010]; double Cal (LL x, LL x0, LL y0 ){ Return sqrt (x-x0) * (x-x0) + y0 * y0);} int main () {int n, I; LL x, y, k; scanf ("% d % I64d", & n, & k); for (I = 1; I <= n; ++ I) scanf ("% I64d ", & num [I]); scanf ("% I64d % I64d", & x, & y); if (k = n + 1) {sort (num + 1, num + n + 1); double anw = num [n]-num [1] + min (Cal (num [n], x, y ), cal (num [1], x, y); for (I = 2; I <n; ++ I) anw = min (anw, min (num [n]-num [I], num [I]-num [1]) + num [n]-num [1] + Cal (num [I], x, y); printf ("%. 10lf \ n ", anw ); Return 0;} k = num [k]; sort (num + 1, num + n + 1); for (I = 1; I <= n & num [I]! = K; ++ I); k = I; double Min = 1000000000; double tmp; tmp = num [k]-num [1] + num [n]-num [1]; for (I = 2; I <= n; ++ I) min = min (Min, tmp + Cal (num [i-1], x, y) + Cal (num [I], x, y) -(num [I]-num [i-1]); tmp = num [n]-num [k] + num [n]-num [1]; for (I = 2; I <= n; ++ I) Min = min (Min, tmp + Cal (num [i-1], x, y) + Cal (num [I], x, y)-(num [I]-num [i-1]); tmp = num [n]-num [1]; if (k-1) {Min = min (Min, tmp + Cal (num [n], x, y) + Cal (num [k-1], x, y)-(num [k]-num [k-1]); Min = min (Min, tmp + Cal (num [n], x, y) + Cal (num [1], x, y)-(num [k]-num [k-1]);} else {Min = min (Min, tmp + Cal (num [n], x, y);} if (k + 1 <= n) {Min = min (Min, tmp + Cal (num [1], x, y) + Cal (num [k + 1], x, y) -(num [k + 1]-num [k]); Min = min (Min, tmp + Cal (num [1], x, y) + Cal (num [n], x, y)-(num [k + 1]-num [k]);} else {Min = min (Min, tmp + Cal (num [1], x, y);} Min = min (Min, n Um [n]-num [1] + num [k]-num [1] + Cal (num [n], x, y); Min = min (Min, num [n]-num [1] + num [n]-num [k] + Cal (num [1], x, y )); for (I = k + 1; I <= n; ++ I) {if (I! = N) {Min = min (Min, num [I]-num [k] + num [I]-num [1] + Cal (num [1], x, y) + Cal (num [I + 1], x, y) + num [n]-num [I + 1]);} else {Min = min (Min, num [I]-num [k] + num [I]-num [1] + Cal (num [1], x, y) + Cal (num [I + 1], x, y) ;}for (I = k-1; I> = 1; -- I) {if (I! = 1) {Min = min (Min, num [k]-num [I] + num [n]-num [I] + Cal (num [n], x, y) + Cal (num [i-1], x, y) + num [i-1]-num [1]);} else {Min = min (Min, num [k]-num [I] + num [n]-num [I] + Cal (num [n], x, y) + Cal (num [i-1], x, y) ;}} printf ("%. 10lf \ n ", Min); return 0 ;}

Codeforces 30D King's Problem? Simulation

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.