[Usaco2013 Jan] Square Overlap

本文详细介绍了如何解决USACO比赛中关于判断多个正方形是否存在公共部分,并求出公共部分面积的问题。通过构建描述中心点所在区域的数组和应用特定算法,实现O(n)时间复杂度的高效解决方法。注意数据中的第9个点的k值问题已修正。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


题目链接: http://www.usaco.org/index.php?page=viewproblem2&cpid=227

官方题解:http://www.usaco.org/current/data/sol_squares.html


模型:有n个大小相同的正方形,问是否存在两个正方形有公共部分,若仅有一个公共部分,求出它的面积;若没有,输出0;否则输出-1.


以下是我在比赛期间死磕出来的算法:

记以A为中心,k为边长的正方形为正方形A

建立一个用来描述中心点所在区域的数组area[][][],其中area[x][y][n]表示第n个落在以(xk, yk)为左下角,边长为k的正方形区域(不包括上边和右边)内的点的编号(显然area[][][]很容易进行初始化)。

定理:两个正方形A,B有公共部分必须满足这两个点所在的正方形区域M,N有公共点。

证明:(稍后补上) 偷个懒

当然这并不是充分条件,所以我们需要重新判断正方形A,B之间的是否由公共部分。由此可以得出算法:枚举每一个点A,判断与它所在区域有公共点的9个区域中是否有点B满足A,B有公共部分并求出其面积。

由于几乎所有区域中最多只会有一个中心点(若存在两个有两个(或更多)中心点的区域,答案即为-1),所以该算法的时间复杂度为O(n)


最后吐槽一下:数据的第9个点的k是奇数,但题目描述保证k为偶数,于是就悲剧了。。。


(代码正在修改中)代码就不贴了哈


由于我不知道我用的到底是什么算法,暂且归为ad hoc吧!


UPD: 官方数据的第9个点已经改正。

### USACO 2016 January Contest Subsequences Summing to Sevens Problem Solution and Explanation In this problem from the USACO contest, one is tasked with finding the size of the largest contiguous subsequence where the sum of elements (IDs) within that subsequence is divisible by seven. The input consists of an array representing cow IDs, and the goal is to determine how many cows are part of the longest sequence meeting these criteria; if no valid sequences exist, zero should be returned. To solve this challenge efficiently without checking all possible subsequences explicitly—which would lead to poor performance—a more sophisticated approach using prefix sums modulo 7 can be applied[^1]. By maintaining a record of seen remainders when dividing cumulative totals up until each point in the list by 7 along with their earliest occurrence index, it becomes feasible to identify qualifying segments quickly whenever another instance of any remainder reappears later on during iteration through the dataset[^2]. For implementation purposes: - Initialize variables `max_length` set initially at 0 for tracking maximum length found so far. - Use dictionary or similar structure named `remainder_positions`, starting off only knowing position `-1` maps to remainder `0`. - Iterate over given numbers while updating current_sum % 7 as you go. - Check whether updated value already exists inside your tracker (`remainder_positions`). If yes, compare distance between now versus stored location against max_length variable's content—update accordingly if greater than previous best result noted down previously. - Finally add entry into mapping table linking latest encountered modulus outcome back towards its corresponding spot within enumeration process just completed successfully after loop ends normally. Below shows Python code implementing described logic effectively handling edge cases gracefully too: ```python def find_largest_subsequence_divisible_by_seven(cow_ids): max_length = 0 remainder_positions = {0: -1} current_sum = 0 for i, id_value in enumerate(cow_ids): current_sum += id_value mod_result = current_sum % 7 if mod_result not in remainder_positions: remainder_positions[mod_result] = i else: start_index = remainder_positions[mod_result] segment_size = i - start_index if segment_size > max_length: max_length = segment_size return max_length ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值