A

排完序就可知道大小了,有效减少代码量

C

一种是动态规划

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
def solve2():
    n = int(input())
    h = list(map(int, input().split()))

    # 初始化 dp 数组
    # dp[i][j] 以i开头区间距离是j的最多个数
    dp = [[1] * n for _ in range(n)]
    ans = 1

    # 动态规划
    for i in range(n):
        for j in range(1, n - i):
            if h[i + j] == h[i]:
                dp[i + j][j] = dp[i][j] + 1
                ans = max(ans, dp[i + j][j])
    print(ans)

另一种是没那么暴力的暴力。看似是三重循环实则是$O(n^2logn)$

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
def solve1():
    N=int(input())
    H=list(map(int,input().split()))
    ans=1
    # 从第i个开始
    for i in range(N):
        # 枚举区间距离
        for w in range(1,N-i):
            cnt=1
            # 没到末尾并且高度相同
            while i+cnt*w<N and H[i]==H[i+cnt*w]:
                cnt+=1
            ans=max(ans,cnt)

    print(ans)

D

orderset呜呜呜,所以我们用c++!

解决思路

  1. 数据结构
    • 使用两个字典来存储房子的位置:
      • Ypos[x]:存储所有 x 坐标为 x 的房子的 y 坐标,按升序排序。
      • Xpos[y]:存储所有 y 坐标为 y 的房子的 x 坐标,按升序排序。
    • 这些有序列表可以通过二分查找快速查询和更新。
  2. Santa 的移动
    • 当 Santa 水平移动(例如从 (X, Y) 移动到 (X, Y + C)),在 Ypos[X] 中查找所有 y 坐标在 [Y, Y + C] 范围内的房子。
    • 当 Santa 垂直移动(例如从 (X, Y) 移动到 (X + C, Y)),在 Xpos[Y] 中查找所有 x 坐标在 [X, X + C] 范围内的房子。
  3. 更新房子状态
    • Santa 经过的房子需要从 YposXpos 中移除,避免重复计算。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include<bits/stdc++.h>
using namespace std;
using ll=long long;

int main(){
	int n,m;
	ll x,y;
	cin >> n >> m >> x >> y;
	
	map<ll,set<ll>>xy;//xy[i]={j s.t. (i,j) に家}
	map<ll,set<ll>>yx;//yx[j]={i s.t. (i,j) に家}
	
	for(int i=0;i<n;i++){
		int xx,yy;
		cin >> xx >> yy;
		xy[xx].insert(yy);
		yx[yy].insert(xx);
	}
	
	int ans=0;
	for(int i=0;i<m;i++){
		char c;
		int d;
		cin >> c >> d;
		if(c=='U'){
			ll new_y=y+d;
			auto it=xy[x].lower_bound(y);
			while(it!=xy[x].end()&&*it<=new_y){
				ans++;
				yx[*it].erase(x);
				it=xy[x].erase(it);
			}
			y=new_y;
		}else if(c=='D'){
			ll new_y=y-d;
			auto it=xy[x].lower_bound(new_y);
			while(it!=xy[x].end()&&*it<=y){
				ans++;
				yx[*it].erase(x);
				it=xy[x].erase(it);
			}
			y=new_y;
		}else if(c=='L'){
			ll new_x=x-d;
			auto it=yx[y].lower_bound(new_x);
			while(it!=yx[y].end()&&*it<=x){
				ans++;
				xy[*it].erase(y);
				it=yx[y].erase(it);
			}
			x=new_x;
		}else if(c=='R'){
			ll new_x=x+d;
			auto it=yx[y].lower_bound(x);
			while(it!=yx[y].end()&&*it<=new_x){
				ans++;
				xy[*it].erase(y);
				it=yx[y].erase(it);
			}
			x=new_x;
		}
	}
	
	cout << x << ' ' << y << ' ' << ans << endl;
}