P3143 [USACO16OPEN]Diamond Collector S

本文介绍了一个关于如何在两个陈列架上最大化展示相似大小钻石的问题。Bessie希望钻石之间的大小差值不超过K,通过排序和计算每个位置的最大显示数,可以找到最佳展示方案。算法包括对钻石进行排序,计算每个位置左侧和右侧的最大距离,然后枚举中间值求解最大总和。

Bessie the cow, always a fan of shiny objects, has taken up a hobby of mining diamonds in her spare time! She has collected NN diamonds (N \leq 50,000N≤50,000) of varying sizes, and she wants to arrange some of them in a pair of display cases in the barn.

Since Bessie wants the diamonds in each of the two cases to be relatively similar in size, she decides that she will not include two diamonds in the same case if their sizes differ by more than KK (two diamonds can be displayed together in the same case if their sizes differ by exactly KK). Given KK, please help Bessie determine the maximum number of diamonds she can display in both cases together.

奶牛Bessie很喜欢闪亮亮的东西(BalingBaling),所以她喜欢在她的空余时间开采钻石!她现在已经收集了N颗不同大小的钻石(N<=50,000),现在她想在谷仓的两个陈列架上摆放一些钻石。

Bessie想让这些陈列架上的钻石保持相似的大小,所以她不会把两个大小相差K以上的钻石同时放在一个陈列架上(如果两颗钻石的大小差值为K,那么它们可以同时放在一个陈列架上)。现在给出K,请你帮Bessie确定她最多一共可以放多少颗钻石在这两个陈列架上。

输入格式
The first line of the input file contains NN and KK (0 \leq K \leq 1,000,000,0000≤K≤1,000,000,000).

The next NN lines each contain an integer giving the size of one of the

diamonds. All sizes will be positive and will not exceed 1,000,000,0001,000,000,000.

输出格式
Output a single positive integer, telling the maximum number of diamonds that

Bessie can showcase in total in both the cases.

算是思维题吧
先sort一趟,然后求一下每个点到左右最远的一个长度
对于每个点取左边的最大值,右边同理
枚举中间值,求左侧和右侧的最大加和

#include<iostream>
#include<algorithm>
using namespace std;
int a[50010];
int z[50010];
int y[50010];
int main()
{
    int n,k,l=1,ma=0;
    cin>>n>>k;
    for(int i=1;i<=n;i++) cin>>a[i];
    sort(a+1,a+n+1);
    for(int i=1;i<=n;i++)
    {
        while(a[i]-a[l]>k) l++;
        z[i]=i-l+1;
    }
    for(int i=n,l=n;i;i--)
    {
        while(a[l]-a[i]>k) l--;
        y[i]=l-i+1;
    }
    for(int i=1;i<=n;i++) z[i]=max(z[i],z[i-1]);
    for(int i=n;i>=1;i--) y[i]=max(y[i],y[i+1]);
    for(int i=1;i<=n;i++) ma=max(ma,z[i]+y[i+1]);
    cout<<ma<<endl;
}

虽然没有直接关于P9954 [USACO20OPEN] Cowntact Tracing B的引用内容,但可参考相关题目信息进行思路分析。 ### 解题思路 从引用[1]可知,该类问题会给出奶牛数量`N`、互动次数`T`,以及每头奶牛的初始健康状态,还有互动记录(包含互动时间、互动的两头奶牛编号)。 可以通过枚举可能的初始感染奶牛和疾病的传播强度`K`(即一头感染奶牛在一次互动中能感染其他奶牛的能力)。对于每一种初始感染奶牛和`K`的组合,模拟疾病的传播过程,看是否能与已知的最终奶牛感染状态相匹配。如果匹配,则该初始感染奶牛和`K`的组合是有效的。最后统计所有有效的初始感染奶牛的数量以及`K`的可能取值范围。 ### 代码实现(Python示例) ```python # 读取输入 n, t = map(int, input().split()) status = list(map(int, input())) interactions = [] for _ in range(t): t, x, y = map(int, input().split()) interactions.append((t, x - 1, y - 1)) interactions.sort() # 模拟疾病传播 def simulate(infected, k): new_infected = infected.copy() infection_count = [0] * n for t, x, y in interactions: if new_infected[x]: if infection_count[x] < k: if not new_infected[y]: new_infected[y] = 1 infection_count[x] += 1 if new_infected[y]: if infection_count[y] < k: if not new_infected[x]: new_infected[x] = 1 infection_count[y] += 1 return new_infected # 枚举初始感染奶牛和K值 possible_patient_zero = set() min_k = float('inf') max_k = 0 for patient_zero in range(n): for k in range(251): initial_infected = [0] * n initial_infected[patient_zero] = 1 result = simulate(initial_infected, k) if result == status: possible_patient_zero.add(patient_zero) min_k = min(min_k, k) max_k = max(max_k, k) print(len(possible_patient_zero), min_k, max_k if max_k < 250 else 'Infinity') ``` ### 代码解释 1. **读取输入**:读取奶牛数量`n`、互动次数`t`、每头奶牛的初始状态`status`,以及互动记录`interactions`,并按时间排序。 2. **模拟疾病传播函数`simulate`**:根据初始感染状态和传播强度`k`,模拟疾病的传播过程,返回最终的感染状态。 3. **枚举初始感染奶牛和`K`值**:对于每一种初始感染奶牛和`K`的组合,调用`simulate`函数进行模拟,如果结果与已知的最终感染状态匹配,则记录该初始感染奶牛和`K`的信息。 4. **输出结果**:输出可能的初始感染奶牛的数量、最小的`K`值和最大的`K`值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值