Hdu-4631-Sad Love Story

Source: Internet
Author: User

An empty plane, adding a point each time, its coordinates are calculated based on the previous point: (x [I-1] * Ax + Bx) mod Cx, (y [I-1] * Ay + By) mod Cy calculates the square of the distance from the nearest point of a certain set, adding n points in total, calculate the sum of squares obtained each time (n <= 5*100000, T (T <= 10) group of test data ).

--> It takes 20 seconds for a long time, but the brute force command still applies to TLE ...... Greedy pruning is performed here.

In the set of existing points, the point is sorted by the x coordinate. Each time a point is added, the point is scanned from the start to the right, and the square of the single abscissa difference> = Min jumps out, because the square of the deviation of the vertical coordinates must be greater than or equal to Min, and the greater the vertical coordinate is, the same way, scanning from the first point on the left of the point to the left, after scanning the statistical results.

# Include <cstdio> # include <set> using namespace std; const int maxn = 500000 + 10; const long INF = (1LL <60); int n, x [maxn], y [maxn]; struct Point {int x; int y; bool operator <(const Point & e) const {return x <e. x ;}}; void read (int * a) {int A, B, C, I; a [0] = 0; scanf ("% d ", & A, & B, & C); for (I = 1; I <= n; I ++) {a [I] = (long) a [I-1] * A + B) % C ;}} long solve () {int I; long M In = INF, ret = 0; multiset <Point> se; se. clear (); Point v; v. x = x [1]; v. y = y [1]; se. insert (v); for (I = 2; I <= n; I ++) {v. x = x [I]; v. y = y [I]; multiset <Point>: iterator p = se. lower_bound (v), iter; for (iter = p; iter! = Se. end (); iter ++) {// starts from p and ends with se. long dx = v. x-iter-> x; dx * = dx; if (dx> = Min) break; // pruning long dy = v. y-iter-> y; dy * = dy; Min = min (Min, dx + dy);} for (iter = p; iter! = Se. begin ();) {// start from the last position of p and finish calculating se. begin () iter --; long dx = v. x-iter-> x; dx * = dx; if (dx> = Min) break; // pruning long dy = v. y-iter-> y; dy * = dy; Min = min (Min, dx + dy);} ret + = Min; se. insert (v) ;}return ret ;}int main () {int T; scanf ("% d", & T); while (T --) {scanf ("% d", & n); read (x); read (y); printf ("% I64d \ n", solve ());} return 0 ;}

 

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.