Codeforces Round #280 (Div. 2)E Vanya and Field(简单题)

本文探讨了在限定方向移动的情况下,如何选择最优起始点以遍历最多目标点的问题。通过对y坐标的预处理,实现了O(1)时间内确定任一目标点对应的起始点,最终找到遍历最多目标的有效起始位置。

转载请注明出处: http://www.cnblogs.com/fraud/           ——by fraud

 

本场题目都比较简单,故只写了E题。

E. Vanya and Field

Vanya decided to walk in the field of size n × n cells. The field contains m apple trees, the i-th apple tree is at the cell with coordinates(xi, yi). Vanya moves towards vector (dx, dy). That means that if Vanya is now at the cell (x, y), then in a second he will be at cell . The following condition is satisfied for the vector: , where  is the largest integer that divides both a and b. Vanya ends his path when he reaches the square he has already visited.

Vanya wonders, from what square of the field he should start his path to see as many apple trees as possible.

Input

The first line contains integers n, m, dx, dy(1 ≤ n ≤ 106, 1 ≤ m ≤ 105, 1 ≤ dx, dy ≤ n) — the size of the field, the number of apple trees and the vector of Vanya's movement. Next m lines contain integers xi, yi (0 ≤ xi, yi ≤ n - 1) — the coordinates of apples. One cell may contain multiple apple trees.

Output

Print two space-separated numbers — the coordinates of the cell from which you should start your path. If there are several answers you are allowed to print any of them.

Sample test(s)
input
5 5 2 3
0 0
1 2
1 3
2 4
3 1
output
1 3
input
2 3 1 1
0 0
0 1
1 1
output
0 0
Note

In the first sample Vanya's path will look like: (1, 3) - (3, 1) - (0, 4) - (2, 2) - (4, 0) - (1, 3)

In the second sample: (0, 0) - (1, 1) - (0, 0)


题意:在一个n*n的方阵内,有m棵果树,在选取一个点之后,每次只能以向量(dx,dy)方向前进。问选取哪个点可以经过最多的的果树,只要输出一个点的坐标即可。

分析:由于gcd(n,dx)=gcd(n,dy)=1,所以,在n次之后一定会回到原点。同时,每个x坐标都只经过一次,每个y坐标都只经过一次。

那么,通过O(n)的预处理,我们可以得出对于任意一个y坐标从y坐标为0的点开始,需要经过多少次的移动才能够得到该y坐标。

那么在后面的m棵果树,我们可以O(1)得知该果树所对应的y坐标为0的起点,到该点所需要的步数,接下来,只需要减去相应的x坐标即可得到对应的y坐标为0的点。

 1 //gaoshenbaoyou  ------ pass system test
 2 #include <iostream>
 3 #include <sstream>
 4 #include <ios>
 5 #include <iomanip>
 6 #include <functional>
 7 #include <algorithm>
 8 #include <vector>
 9 #include <string>
10 #include <list>
11 #include <queue>
12 #include <deque>
13 #include <stack>
14 #include <set>
15 #include <map>
16 #include <cstdio>
17 #include <cstdlib>
18 #include <cmath>
19 #include <cstring>
20 #include <climits>
21 #include <cctype>
22 using namespace std;
23 #define XINF INT_MAX
24 #define INF 0x3FFFFFFF
25 #define MP(X,Y) make_pair(X,Y)
26 #define PB(X) push_back(X)
27 #define REP(X,N) for(int X=0;X<N;X++)
28 #define REP2(X,L,R) for(int X=L;X<=R;X++)
29 #define DEP(X,R,L) for(int X=R;X>=L;X--)
30 #define CLR(A,X) memset(A,X,sizeof(A))
31 #define IT iterator
32 typedef long long ll;
33 typedef pair<int,int> PII;
34 typedef vector<PII> VII;
35 typedef vector<int> VI;
36 const int maxn=1000010;
37 int num[maxn];
38 int d[maxn];
39 int main()
40 {
41     ios::sync_with_stdio(false);
42     int n,m,dx,dy;
43     cin>>n>>m>>dx>>dy;
44     int y=0;
45     for(int i=0;i<n;i++)
46     {
47         d[y]=i;
48         y+=dy;
49         y%=n;
50     }
51     long long tx,ty;
52     int ansx,ans=0;
53     for(int i=0;i<m;i++)
54     {
55         cin>>tx>>ty;
56         long long t=d[ty];
57         t=(tx+(n-t)*dx)%n;
58         num[t]++; 
59         if(num[t]>ans)
60         {
61             ans=num[t];
62             ansx=t;
63         }
64     }
65     cout<<ansx<<" "<<0<<endl;
66     return 0;
67 }
代码君

 

转载于:https://www.cnblogs.com/fraud/p/4137594.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值