思路
动态规划的思想,记录逗留的天数,和最长逗留天数,如果某一时刻在范围内,逗留天数+1,更新最长逗留天数,否则,逗留天数清0
满分代码实现(自解)
#include <map>
#include <set>
#include <queue>
#include <iostream>
#include <algorithm>
using namespace std;
class Solution {
public:
void solve();
};
void Solution::solve() {
int n = -1, k = -1, t = -1, xl = -1, yd = -1, xr = -1, yu = -1;
cin >> n >> k >> t >> xl >> yd >> xr >> yu;
// 经过高风险地区的人数count1,逗留人数count2
int count1 = 0, count2 = 0;
// 记录当前逗留天数cur_days,记录最长逗留天数max_days
int cur_days = -1, max_days = -1;
// 记录输入的坐标
int x = -1, y = -1;
while (n--) {
cur_days = 0, max_days = 0;
for (int i = 0; i < t; i++) {
cin >> x >> y;
if (xl <= x && x <= xr && yd <= y && y <= yu) {
cur_days++;
max_days = max(max_days, cur_days);
}
else {
cur_days = 0;
}
}
if (max_days >= 1) count1++;
if (max_days >= k) count2++;
}
cout << count1 << endl;
cout << count2;
}
int main() {
// 提高cin,cout的速度
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
Solution s;
s.solve();
return 0;
}