Diamond Collector
问题描述
USACO 2016 US Open Contest, Bronze Problem 1. Diamond Collector https://usaco.org/index.php?page=viewproblem2&cpid=639
给定长度为 的数组和 ,输出 最长的长度,要求 中任意两数之差不超过 。
样例
input :
5 3
1
6
4
3
1output :
4思路
遍历数组中每个数 ,遍历搜索数组中有几个 使得 大于 且 , 更新为最长的长度。
和 之差不能使用 绝对值。因为这样会导致一个 比 大 ,另一个 比 小 ,那么这两个 的差值大于 ,导致答案错误。 # 代码
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
freopen("diamond.in", "r", stdin);
freopen("diamond.out", "w", stdout);
int n, k;
cin >> n >> k;
vector<int> a(n);
for(int& x:a) cin >> x;
int ans = 0;
for(int x: a){
int now = 0;
for(int y:a){
if(y >= x && y - x <= k) now++;
}
ans = max(ans, now);
}
cout << ans;
return 0;
}Diamond Collector
https://mingsm17518.github.io/2026/04/30/算法学习/03_Bronze/01_Complete_Search/02_Diamond_Collector/