Gym

题目:

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 balls 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 should be printed with the maximum allowed error of 10-6.

题意:

给出一些点的坐标,两个玩家随机选取两个点(这两个点可以是同一个),如果这两个点的斜率是1或者是-1,则这两个玩家就能分出胜负,否则游戏结束。问给出的点中,所有的选择中有玩家胜出的概率是多少。

思路:

将给出的每一个点,分别以1和-1的斜率做直线与y轴交于两个点,将这两个点的y坐标的大小记录下来。那么遍历这个map容器,答案就是对映射值(k)的k*(k-1)求和,最终结果除以n*n。

PS:

一定要注意数据范围!!!!

代码:

#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
using 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
",1.0*sum/temp);
    }
    return 0;
}
/*
PutIn:
4
1 2
2 1
1 1
2 2
3
3 1
2 2
1 3
PutOut:
0.25
0.666667
*/
View Code
原文地址:https://www.cnblogs.com/sykline/p/9864010.html