使用C++的CCF-CSP满分解决方案 202009-2 风险人群筛查

在这里插入图片描述

思路

动态规划的思想,记录逗留的天数,和最长逗留天数,如果某一时刻在范围内,逗留天数+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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值