CodeForces 514B

本文介绍了一个经典的算法问题——汉索罗使用激光枪消灭战场上的帝国风暴兵。通过计算每名士兵与激光枪之间的斜率来确定最少射击次数。文章提供了完整的代码实现,并解释了如何处理特殊情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

B. Han Solo and Lazer Gun
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

There are n Imperial stormtroopers on the field. The battle field is a plane with Cartesian coordinate system. Each stormtrooper is associated with his coordinates (x, y) on this plane.

Han Solo has the newest duplex lazer gun to fight these stormtroopers. It is situated at the point (x0, y0). In one shot it can can destroy all the stormtroopers, situated on some line that crosses point (x0, y0).

Your task is to determine what minimum number of shots Han Solo needs to defeat all the stormtroopers.

The gun is the newest invention, it shoots very quickly and even after a very large number of shots the stormtroopers don't have enough time to realize what's happening and change their location.

Input

The first line contains three integers n, x0 и y0 (1 ≤ n ≤ 1000,  - 104 ≤ x0, y0 ≤ 104) — the number of stormtroopers on the battle field and the coordinates of your gun.

Next n lines contain two integers each xi, yi ( - 104 ≤ xi, yi ≤ 104) — the coordinates of the stormtroopers on the battlefield. It is guaranteed that no stormtrooper stands at the same point with the gun. Multiple stormtroopers can stand at the same point.

Output

Print a single integer — the minimum number of shots Han Solo needs to destroy all the stormtroopers.

Sample test(s)
Input
4 0 0
1 1
2 2
2 0
-1 -1
Output
2
Input
2 1 2
1 1
1 0
Output
1
Note

Explanation to the first and second samples from the statement, respectively:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <cstring>
 5 using namespace std;
 6 
 7 ///咋看吓一跳,感觉暴力不科学啊,怎么可能知道一条直线上有
 8 ///多少点,所以就去计算每个点同炮的斜率,斜率一样,就在同
 9 ///同一条直线上。
10 const int MAX_SIZE = 1E4 + 100;
11 
12 int main()
13 {
14     //freopen("in.txt", "r", stdin);
15     int n, i;
16     int x, y, px, py;
17     double kx, ky;
18     bool tag;
19     double k[MAX_SIZE];
20 
21     while(scanf("%d %d %d", &n, &x, &y) != EOF)
22     {
23         tag = false;
24         int j = 0;
25         for(i = 0; i < n; i++)
26         {
27             scanf("%d %d", &px, &py);
28             kx = px - x;
29             ky = py - y;
30             if(kx == 0)
31                 tag = true;
32             else
33                 k[j++] = ky / kx;
34         }
35 
36         sort(k, k+j);
37         int ans = (tag == true) ? 1 : 0;
38 
39         for(i = 0; i < j-1; i++)
40         {
41             if(k[i] != k[i+1])
42             {
43                 ans++;
44             }
45         }
46         ans += (i == j-1) ? 1 : 0;
47         cout << ans << endl;
48     }
49     return 0;
50 }
51 
52 //题目需要注意的地方是,在中间计算过程,要用double去存储横纵坐标的差值。
View Code

 

转载于:https://www.cnblogs.com/ya-cpp/p/4362889.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值