CTU Open Contest 2017 Go Northwest!(思维题+map)

题目:

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\n",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
*/

原文地址:https://www.cnblogs.com/sykline/p/9864010.html

时间: 2024-10-09 08:07:47

CTU Open Contest 2017 Go Northwest!(思维题+map)的相关文章

Codeforces Round #625 (Div. 2, based on Technocup 2020 Final Round) A. Contest for Robots(思维题)

Polycarp is preparing the first programming contest for robots. There are nn problems in it, and a lot of robots are going to participate in it. Each robot solving the problem ii gets pipi points, and the score of each robot in the competition is cal

Gym - 101670G Ice cream samples(CTU Open Contest 2017 尺取法)

题目: To encourage visitors active movement among the attractions, a circular path with ice cream stands was built in the park some time ago. A discount system common for all stands was also introduced. When a customer buys ice cream at some stand, he

Gym - 101670F Shooting Gallery(CTU Open Contest 2017 区间dp)

题目&题意:(有点难读...) 给出一个数字序列,找出一个区间,当删除这个区间中的两个相同的数字后,只保留这两个数字之间的序列,然后继续删除相同的数字,问最多可以实行多少次删除操作. 例如: 所以执行两次删除操作. 思路: 区间dp,关键在于确定大的区间是由哪些小的区间转化来的. 当a[l] == a[r]的时候,dp[l][r] = dp[l+1][r-1]+1(因为要得到最多的删除次数,大的区间的次数在相等的情况下肯定是由内部小的区间加一得来的): 当a[l] != a[r]的时候,dp[l

杭电2018多校第一场(2018 Multi-University Training Contest 1) 1001.Maximum Multiple (HDU6298)-数学思维题(脑子是个好东西,可惜我没有)

暑假杭电多校第一场,这一场是贪心场,很多贪心的题目,但是自己太菜,姿势挫死了,把自己都写吐了... 2018 Multi-University Training Contest 1 HDU6298.Maximum Multiple 题目意思就是给你一个n,找出来三个数x,y,z, 使得n=x+y+z,而且x,y,z都是n的因数,并且x*y*z为最大值,让你输出来x*y*z的最大值.如果没有满足条件的情况就输出-1. 由1=1/2+1/3+1/6=1/3+1/3+1/3=1/2+1/4+1/4,所

Benelux Algorithm Programming Contest 2014 Final ACM-ICPC Asia Training League 暑假第一阶段第二场 E. Excellent Engineers-单点更新、区间最值-线段树 G. Growling Gears I. Interesting Integers-类似斐波那契数列-递推思维题

先写这几道题,比赛的时候有事就只签了个到. E. Excellent Engineers 传送门: 这个题的意思就是如果一个人的r1,r2,r3中的某一个比已存在的人中的小,就把这个人添加到名单中. 因为是3个变量,所以按其中一个变量进行sort排序,然后,剩下的两个变量,一个当位置pos,一个当值val,通过线段树的单点更新和区间最值操作,就可以把名单确定. 代码: 1 //E-线段树 2 #include<iostream> 3 #include<cstdio> 4 #incl

ACM: Gym 101047K Training with Phuket&#39;s larvae - 思维题

Gym 101047K Training with Phuket's larvae Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Practice Description standard input/output Thai cuisine is known for combining seasonings so that every dish has flavors that are s

[ICPC训练联盟周赛1] CTU Open Contest 2019

昨天ICPC训练联盟进行了第一场比赛,题目是CTU Open Contest 2019,烟台大学给出了解析. 题目和题解在此(提取码zre3) 我和索队还有yz组队,喊着队友nb就AK了,抱大腿真爽. A题是简单的组合数学,显然有公式 \(1\times C_n^1+2\times C_n^2+ \dots + n \times C_n^n=n \times 2^{n-1}\) 但忘了考虑\(n=0\)的情况,我wa了一发. B题题目我不太读得懂,yz A的. C题是算圆和矩形相交的面积,我抄了

ICPC North Central NA Contest 2017 部分题解

ICPC North Central NA Contest 2017 部分题解 B. Pokemon Go Go 大意:用最短路径来抓住所有的稀有精灵,DFS求最短路 #include <bits/stdc++.h> #define mem(a) memset(a,0,sizeof(a)) #define forn(i,n) for(int i=0;i<n;++i) #define for1(i,n) for(int i=1;i<=n;++i) #define IO std::io

Unique Encryption Keys (思维题 预处理)

题目 题意:给m个数字, q次询问, 询问b到e之间如果有重复数字就输出, 没有就输出OK 思路:用f[i]数组 记录从i开始向后最近的有重复数字的 位置, 如 1 3 2 2, 则f[1] = 4; 如果离a最近的重复数字的位置 都大于b, 就说明没有重复数字. f[]数组需要预处理,从后向前. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <vector>