Question:
Go northwest! Is a game usually played in the park main hall when occasional rainy weather discourages the visitors from enjoying outdoor attractions.
The game is played by a pair of players and it is based entirely on luck, the players can hardly influence its outcome.
The game plan consists of one large map on the wall with N distinct towns displayed on it. before the start of the game, the leader of the game hands a bowl filled with N identical BALS to each player. inside each ball, there is a reference to some town on the map. each bowl contains references to all towns on the map.
When the game starts, one of the players randomly draws one ball from his/her bowl, opens it and reveals the town inside. next, the other player randomly draws one ball form his/her bowl, opens it and also reveals the town inside.
If both towns are the same, the game ends in a draw. if the towns are not the same, the leader of the game highlights the towns on the map. if one of the towns lies exactly northwest to the other, the first player wins the game. if one of the towns lies exactly northeast to the other, the second player wins the game. in all other cases, the game ends in a draw. two towns are considered to lie exactly northwest or northeast from each other, if the angle between the line connecting the towns and the horizontal axis is exactly 45 degrees.
It is clear that the chance of winning the game depends substantially on the layout of the towns on the map. all town coordinates are known in advance. you are asked to find the probability that there is a winner in the game.
Input specification:
There are more test cases. each case starts with a line containing one integer N (1 ≤ n ≤105) specifying the number of towns on the map. next, there are n lines each of which contains two integers x and y separated by space and specifying the coordinates of one town on the map. the coordinates are standard Cartesian coordinates in the plane. the absolute value of any coordinate does not exceed 109.
Output specification:
For each test case, print a single line with one floating point number P denoting the probability that there is a winner in the game. P shoshould be printed with the maximum allowed error of 10-6.
Question:
Given the coordinates of some points, two players randomly select two points (the two points can be the same), if the slope of these two points is 1 or-1, then the two players can win or lose, or the game ends. Q: What is the probability that players will win in all the choices given.
Ideas:
Place the straight line and Y-axis at the slope of 1 and-1, respectively, and record the Y coordinate of the two points. Then traverse this map container, the answer is to sum the K * (k-1) of the ing value (k), the final result is divided by N * n.
PS:
Be sure to pay attention to the data range !!!!
Code:
#include <bits/stdc++.h>#define inf 0x3f3f3f3fusing namespace std;typedef long long ll;const int maxn = 2e5+10;int main() { ll n; while(scanf("%lld",&n)!=EOF) { map<ll, ll> add; //map<int, int> sub; for(ll i = 0; i<n; i++){ ll x,y; scanf("%lld%lld",&x,&y); add[y+x]++; add[y-x]++; } map<ll, ll>::iterator it; ll sum = 0,temp = n*n; for(it = add.begin(); it!=add.end(); it++){ sum += it->second*(it->second-1); } printf("%.6f\n",1.0*sum/temp); } return 0;}/*PutIn:41 22 11 12 233 12 21 3PutOut:0.250.666667*/View code
CTU open contest 2017 go northwest! (THINKING questions + map)