Codeforces 123 B squares

Source: Internet
Author: User

Question link ~~>

Question recognition:I found a rule for a long time for this question I did yesterday, but I did not get it out. I read the official question and did not understand it. So I started to study the question again, I finally completed "she" for half a day, but I still did not understand the official question.

Solution:

Let's take a picture first (it looks a little big !)


If you draw a picture like this, is it the least from one point to the other? Of course, it is the intersection of the two, as shown in the middle, because if you take the intersection, you can cross the two straight lines. How can this intersection point be calculated? It is the maximum number of blue lines between the red line and the Green Line on the way.

Now, the main request is the number of lines passing through the red line and the Blue Line in the diagram (take the maximum value), because the red line is 45 degrees and knows a point, so ~> We can know the square name of this straight line. Similarly, we can also know the linear equation of the green line, so that we can find the intersection of the two straight lines, it is easy to know the number of straight lines after the intersection, But if you directly divide the distance from the intersection to a certain point by the width, this will not work, because it may be less than 1, or add 1, SO ~> We can use a straight line as the standard. Here we have the red line as an example (the Green Line is the same): we use the orange line of 135 degrees as the standard, if the number of two points on both sides of the orange line is equal to the distance from two points to the orange line, the integer plus 1 is obtained. Otherwise, the absolute value of the distance from two points to the orange line is the number of lines passing through the straight line.

Code:

# Include <iostream> # include <sstream> # include <map> # include <cmath> # include <fstream> # include <queue> # include <vector> # include <sstream> # include <cstring> # include <cstdio> # include <stack> # include <bitset> # include <ctime> # include <string> # include <cctype> # include <iomanip> # include <algorithm> using namespace STD; # define int _ int64const int INF = 0x3f3f3f; const double ESP = 0.0000000001; const double Pi = ACOs (-1.0 ); Const int mod = 1000000007; const int up = 1000000000; const int DN =-1000000000; const int my = 205; const int MX = 205; int worka (double X1, double Y1, double X2, double Y2, double D) {double da, DB; int num1, num2; da = FABS (Y1 + X1)/SQRT (2.0 ); DB = FABS (y2 + x2)/SQRT (2.0); num1 = (INT) (da/d + 1.0); num2 = (INT) (db/d + 1.0 ); if (X1 + Y1> 0) & (X2 + y2> 0) | (X1 + Y1 <0 & X2 + y2 <0) // return ABS (num1-num2) on the same side; else return ABS (num1 + num2-1);} int workb (double X1, double Y1, double X2, double Y2, double D) {double da, DB; int num1, num2; da = FABS (Y1-X1)/SQRT (2.0); DB = FABS (Y2-X2) /SQRT (2.0); num1 = (INT) (da/D) + 1; num2 = (INT) (db/D) + 1; if (Y1> X1) & (Y2> x2) | (Y1 <X1) & (Y2 <X2) // if both values are greater than and less than return ABS (num1-num2 ); else return ABS (num1 + Num2-1);} int main () {double A, B, X1, Y1, X2, Y2, B1, B2, X, Y; while (~ Scanf ("% lf", & A, & B, & X1, & Y1, & X2, & Y2 )) {If (x1! = X2 & y1-y2 = x1-X2) // for 45 degrees cout <worka (x1, Y1, X2, Y2, SQRT (2.0) * A) <Endl; else if (x1! = X2 & Y1-y2 = x2-X1) // 135 degree cout <workb (x1, Y1, X2, Y2, SQRT (2.0) * B) <Endl; else {b1 = Y1-x1; b2 = y2 + x2; X = (B2-B1)/2.0; // intersection y = x + b1; cout <max (worka (x1, Y1, X, Y, SQRT (2.0) * A), workb (X2, Y2, X, Y, SQRT (2.0) * B) <Endl ;}} return 0 ;}

Add the official question:

Official code:

#include <cstdio>#include <algorithm>#include <iostream>using namespace std;int a, b, x1, y1, x2, y2;int x, y;int main(){    #ifndef ONLINE_JUDGE        freopen("in", "r", stdin);        freopen("out", "w", stdout);    #endif    cin >> a >> b >> x1 >> y1 >> x2 >> y2;    x = x1; y = y1;    x1 = x + y;    y1 = y - x;    x = x2; y = y2;    x2 = x + y;    y2 = y - x;     a *= 2;    b *= 2;    x1 = x1 / a + (x1 > 0);    x2 = x2 / a + (x2 > 0);    y1 = y1 / b + (y1 > 0);    y2 = y2 / b + (y2 > 0);    cout << max(abs(y2 - y1), abs(x2 - x1)) << endl;    return 0;}


 

Codeforces 123 B squares

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.