这个题目本来也不是很难, 仔细分析一下 就只有两个难点:
第一点: 就是看懂题意思和考点有一点难度, 特别是还是英文问题。
第二点 : 就是要注意边界的问题, 而且非常复杂, 比如我一开始的代码: 自认为都考虑到了, 但是还是没有过, 主要是这样写左边界比较难以判断。
后面换了一种,后缀和, 这样左边的影响就可以不用管了, 省略了很多东西, 一个从前往后, 一个从后往前, 一点点不同, 写法是复杂程度完全不同, 还特别简化, 所以以后写题目时要好好想想更好的方法, 优化优化, 没有buy 在写, 不然后面思路, 代码都会很乱的!!!!
AC代码:
/*//
Problem: C. Omkar and Determination
Contest: CF749
URL: https://codeforces.ml/contest/1586/problem/C
/*/
#include <bits/stdc++.h>
#define ll long long int
#define ull unsigned long long int
using namespace std;
const int inf = 0x3f3f3f3f;
const ll ll_inf = 0x3f3f3f3f3f3f3f3f;
const ll mod = 1e9 + 7;
const int maxn = 1e6 + 5;
string s[maxn];
int g[maxn];
int main(int argc, char const *argv[]) {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> s[i];
}
for (int i = 0; i < n - 1; i ++) {
for (int j = 0; j < m - 1; j ++) {
if (s[i][j + 1] == 'X' && s[i + 1][j] == 'X') {
g[j] = 1;
}
}
}
for (int i = m - 2; i >= 0; i --) {
g[i] += g[i + 1];
}
int Q;
cin >> Q;
while (Q --) {
int a, b;
cin >> a >> b;
a --, b --;
if (a == b) {
puts("YES");
continue;
}
if (g[a] - g[b] > 0) {
puts("NO");
} else {
puts("YES");
}
}
return 0;
}
WA代码:
/*//
Problem: C. Omkar and Determination
Contest: CF749
URL: https://codeforces.ml/contest/1586/problem/C
/*/
#include <bits/stdc++.h>
#define ll long long int
#define ull unsigned long long int
using namespace std;
const int inf = 0x3f3f3f3f;
const ll ll_inf = 0x3f3f3f3f3f3f3f3f;
const ll mod = 1e9 + 7;
const int maxn = 1e6 + 1;
string s[maxn];
int t[maxn];
int ans[maxn];
int main(int argc, char const *argv[]) {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> s[i];
}
for (int i = 0; i < n; i ++) {
for (int j = 0; j < m; j ++) {
if (s[i][j] == 'X') {
if (i + 1 < n && j - 1 >= 0 && s[i + 1][j - 1] == 'X' && s[i + 1][j] != 'X') {
t[j] = 1;
t[j - 1] = 1;
}
if (i - 1 >= 0 && j + 1 < m && s[i - 1][j + 1] == 'X' && s[i][j+1] != 'X') {
t[j] = 1;
t[j + 1] = 1;
}
}
}
}
for (int i = 1; i <= m; i ++) {
ans[i] = ans[i - 1] + t[i - 1];
}
int Q;
cin >> Q;
while (Q --) {
int a, b;
cin >> a >> b;
if (a == b) {
cout << "YES" << endl;
continue;
}
if (ans[b] - ans[a-1] >= 4) {
cout << "NO" << endl;
} else if (ans[b] - ans[a-1] == 3) {
if (ans[b] == 0 && ans[a] == 0) {
cout << "NO" << endl;
} else if (ans[a] ^ ans[b]) {
cout << "NO" << endl;
} else if (ans[a] != 0 && ans[a + 1] != 0) {
cout << "NO" << endl;
} else if (ans[b] != 0 && ans[b - 1] != 0) {
cout << "NO" << endl;
}else {
cout << "YES" << endl;
}
} else if (ans[b] - ans[a-1] == 2) {
int idx = lower_bound(ans + a, ans + b, 1) - ans;
if (ans[idx + 1] != 0) {
cout << "NO" << endl;
} else {
cout << "YES" << endl;
}
} else {
cout << "YES" << endl;
}
}
return 0;
}