我是靠谱客的博主 机智悟空,这篇文章主要介绍CodeForces - 1073C(二分),现在分享给大家,希望可以做个参考。

题解:

二分区间长度,去尺取每一个区间,除去这段区间的操作量,判断现操作距离目的操作所需步数与区间长度作对比,如果是偶数说明可以到达,否则不行。

复制代码
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include <cstdio> #include <string> #include <vector> #include <bitset> #include <stack> #include <cmath> #include <deque> #include <queue> #include <list> #include <set> #include <map> #pragma comment(linker, "/STACK:1024000000,1024000000") #define line printf("---------------------------n") #define mem(a, b) memset(a, b, sizeof(a)) #define pi acos(-1) using namespace std; typedef long long ll; const double eps = 1e-9; const int inf = 0x3f3f3f3f; const int mod = 1e9+7; const int maxn = 2000+10; int n; int aim_x, aim_y; int now_x = 0, now_y = 0; string s; bool is_ok(int temp_x, int temp_y, int mid){ int need_x = abs(aim_x-temp_x), need_y = abs(aim_y-temp_y); mid -= need_x; mid -= need_y; if(mid < 0){ return false; } if(mid % 2 == 0){ return true; } return false; } bool judge(int mid){ int temp_x = 0, temp_y = 0; for(int i = 0; i < mid; i++){ if(s[i] == 'U'){ temp_y++; } if(s[i] == 'D'){ temp_y--; } if(s[i] == 'L'){ temp_x--; } if(s[i] == 'R'){ temp_x++; } } if(is_ok(now_x-temp_x, now_y-temp_y, mid)){ return true; } for(int i = 1; i+mid-1 < n; i++){ if(s[i+mid-1] == 'U'){ temp_y++; } if(s[i+mid-1] == 'D'){ temp_y--; } if(s[i+mid-1] == 'R'){ temp_x++; } if(s[i+mid-1] == 'L'){ temp_x--; } if(s[i-1] == 'U'){ temp_y--; } if(s[i-1] == 'D'){ temp_y++; } if(s[i-1] == 'R'){ temp_x--; } if(s[i-1] == 'L'){ temp_x++; } if(is_ok(now_x-temp_x, now_y-temp_y, mid)){ return true; } } return false; } int main(){ cin>>n; cin>>s; cin>>aim_x>>aim_y; for(int i = 0; i < n; i++){ if(s[i] == 'U'){ now_y++; } if(s[i] == 'D'){ now_y--; } if(s[i] == 'L'){ now_x--; } if(s[i] == 'R'){ now_x++; } } int l = 0, r = n, ans = -1; while(l <= r){ int mid = (l+r) >> 1; if(judge(mid)){ ans = mid; r = mid-1; } else{ l = mid+1; } } printf("%dn", ans); }

 

最后

以上就是机智悟空最近收集整理的关于CodeForces - 1073C(二分)的全部内容,更多相关CodeForces内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(53)

评论列表共有 0 条评论

立即
投稿
返回
顶部