干草堆(差分)

本文通过一个具体的干草堆问题实例介绍了如何运用裸差分算法进行优化处理。该算法能够有效地减少时间复杂度,避免超时问题。文章详细展示了算法的具体实现过程,包括输入输出流程及关键步骤。

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

'只要坚持就会成功--2022.1.21'

思路:裸差分

题目链接: 干草堆

 题目代码:

#include<iostream>
#include<algorithm>
using namespace std;


int n, k;
int p[1000010];
void insert(int l, int r, int c)
{
    p[l] += c;
    p[r + 1] -= c;
}
//一定要用差分来优化,不然会超时;
int main()
{
    cin >> n >> k;

    for(int i = 0; i < k; i ++)
    {
        int a, b;
        cin >> a >> b;
        insert(a, b, 1);

    }
    for(int i = 0; i < n; i ++)p[i] += p[i - 1];
    sort(p, p + n);
    cout << p[n / 2] << endl;
    return 0;
}

 

 

<?xml version="1.0" encoding="utf-8" ?> <Defs> <ThingDef Abstract="True" Name="OrganicProductBase" ParentName="ResourceBase"> <graphicData> <graphicClass>Graphic_Single</graphicClass> </graphicData> <statBases> <MaxHitPoints>60</MaxHitPoints> <Flammability>1.0</Flammability> <DeteriorationRate>6</DeteriorationRate> <Mass>0.03</Mass> </statBases> <tickerType>Rare</tickerType> <healthAffectsPrice>false</healthAffectsPrice> </ThingDef> <ThingDef Abstract="True" Name="PlantFoodRawBase" ParentName="OrganicProductBase"> <ingestible> <preferability>RawBad</preferability> <tasteThought>AteRawFood</tasteThought> <ingestEffect>EatVegetarian</ingestEffect> <ingestSound>RawVegetable_Eat</ingestSound> </ingestible> <statBases> <Nutrition>0.05</Nutrition> <FoodPoisonChanceFixedHuman>0.02</FoodPoisonChanceFixedHuman> </statBases> <socialPropernessMatters>true</socialPropernessMatters> <thingCategories> <li>PlantFoodRaw</li> </thingCategories> <allowedArchonexusCount>200</allowedArchonexusCount> </ThingDef> <ThingDef ParentName="PlantFoodRawBase"> <defName>RawPotatoes</defName> <label>potatoes</label> <description>Raw potatoes.</description> <possessionCount>10</possessionCount> <graphicData> <texPath>Things/Item/Resource/PlantFoodRaw/Potatoes</texPath> </graphicData> <statBases> <MarketValue>1.1</MarketValue> </statBases> <ingestible> <foodType>VegetableOrFruit</foodType> </ingestible> <comps> <li Class="CompProperties_Rottable"> <daysToRotStart>30</daysToRotStart> <rotDestroys>true</rotDestroys> </li> </comps> </ThingDef> <ThingDef ParentName="PlantFoodRawBase"> <defName>RawFungus</defName> <label>raw fungus</label> <description>Raw fungus.</description> <possessionCount>10</possessionCount> <graphicData> <texPath>Things/Item/Resource/PlantFoodRaw/RawFungus</texPath> </graphicData> <statBases> <MarketValue>1.1</MarketValue> </statBases> <ingestible> <foodType>Fungus</foodType> </ingestible> <ingredient> <mergeCompatibilityTags> <li MayRequire="Ludeon.RimWorld.Ideology">Fungus</li> </mergeCompatibilityTags> </ingredient> <comps> <li Class="CompProperties_Rottable"> <daysToRotStart>30</daysToRotStart> <rotDestroys>true</rotDestroys> </li> </comps> </ThingDef> <ThingDef ParentName="PlantFoodRawBase"> <defName>RawRice</defName> <label>rice</label> <description>Raw rice.</description> <soundInteract>Grain_Drop</soundInteract> <soundDrop>Grain_Drop</soundDrop> <graphicData> <texPath>Things/Item/Resource/PlantFoodRaw/Rice</texPath> </graphicData> <statBases> <MarketValue>1.1</MarketValue> </statBases> <ingestible> <foodType>Seed</foodType> </ingestible> <comps> <li Class="CompProperties_Rottable"> <daysToRotStart>40</daysToRotStart> <rotDestroys>true</rotDestroys> </li> </comps> </ThingDef> <ThingDef ParentName="PlantFoodRawBase"> <defName>RawAgave</defName> <label>agave fruit</label> <description>Raw agave fruit.</description> <possessionCount>10</possessionCount> <graphicData> <texPath>Things/Item/Resource/PlantFoodRaw/AgaveFruit</texPath> </graphicData> <statBases> <MarketValue>1.1</MarketValue> </statBases> <ingestible> <foodType>VegetableOrFruit</foodType> </ingestible> <comps> <li Class="CompProperties_Rottable"> <daysToRotStart>25</daysToRotStart> <rotDestroys>true</rotDestroys> </li> </comps> </ThingDef> <ThingDef ParentName="PlantFoodRawBase"> <defName>RawCorn</defName> <label>corn</label> <description>Raw corn.</description> <soundInteract>Grain_Drop</soundInteract> <soundDrop>Grain_Drop</soundDrop> <possessionCount>10</possessionCount> <graphicData> <texPath>Things/Item/Resource/PlantFoodRaw/Corn</texPath> </graphicData> <statBases> <MarketValue>1.1</MarketValue> </statBases> <ingestible> <foodType>VegetableOrFruit</foodType> </ingestible> <comps> <li Class="CompProperties_Rottable"> <daysToRotStart>60</daysToRotStart> <rotDestroys>true</rotDestroys> </li> </comps> </ThingDef> <ThingDef ParentName="PlantFoodRawBase"> <defName>RawBerries</defName> <label>berries</label> <description>Assorted berries. Nice to eat, even when raw.</description> <possessionCount>10</possessionCount> <graphicData> <texPath>Things/Item/Resource/PlantFoodRaw/Berries</texPath> </graphicData> <statBases> <MarketValue>1.2</MarketValue> <Mass>0.027</Mass> </statBases> <comps> <li Class="CompProperties_Rottable"> <daysToRotStart>14</daysToRotStart> <rotDestroys>true</rotDestroys> </li> </comps> <ingestible> <preferability>RawTasty</preferability> <tasteThought></tasteThought> <foodType>VegetableOrFruit</foodType> </ingestible> </ThingDef> <!-- Plant matter (inedible by people) --> <ThingDef ParentName="OrganicProductBase" Name="RoughPlantBase" Abstract="True"> <ingestible> <foodType>Plant</foodType> <preferability>DesperateOnlyForHumanlikes</preferability> </ingestible> <statBases> <Nutrition>0.05</Nutrition> </statBases> </ThingDef> <ThingDef ParentName="RoughPlantBase"> <defName>Hay</defName> <label>hay</label> <description>Nutrient-rich grasses and shoots, harvested and compacted for storage. Hay is good animal feed, but inedible for humans.</description> <stackLimit>200</stackLimit> <graphicData> <texPath>Things/Item/Resource/PlantFoodRaw/Hay</texPath> <graphicClass>Graphic_StackCount</graphicClass> </graphicData> <statBases> <MarketValue>0.6</MarketValue> <Mass>0.014</Mass> <Flammability>1.5</Flammability> </statBases> <comps> <li Class="CompProperties_Rottable"> <daysToRotStart>60</daysToRotStart> <rotDestroys>true</rotDestroys> </li> </comps> <thingCategories> <li>Foods</li> </thingCategories> <ingestible> <optimalityOffsetFeedingAnimals>7</optimalityOffsetFeedingAnimals> </ingestible> <allowedArchonexusCount>400</allowedArchonexusCount> </ThingDef> <ThingDef ParentName="RoughPlantBase"> <defName>RawHops</defName> <label>hops</label> <description>Raw hops. A flavoring and preserving agent that is necessary for making beer.</description> <possessionCount>10</possessionCount> <graphicData> <texPath>Things/Item/Resource/PlantFoodRaw/Hops</texPath> </graphicData> <statBases> <MarketValue>1.3</MarketValue> </statBases> <comps> <li Class="CompProperties_Rottable"> <daysToRotStart>30</daysToRotStart> <rotDestroys>true</rotDestroys> </li> </comps> <thingCategories> <li>PlantMatter</li> </thingCategories> <ingestible> <preferability>DesperateOnly</preferability> <canAutoSelectAsFoodForCaravan>false</canAutoSelectAsFoodForCaravan> </ingestible> </ThingDef> <ThingDef ParentName="RoughPlantBase"> <defName>PsychoidLeaves</defName> <label>psychoid leaves</label> <description>Raw cut leaves of a psychoid plant. Can be refined into various form of the stimulant drug psychite.</description> <possessionCount>10</possessionCount> <graphicData> <texPath>Things/Item/Resource/PlantFoodRaw/PsychoidLeaves</texPath> </graphicData> <statBases> <MarketValue>1.9</MarketValue> <Flammability>1.3</Flammability> </statBases> <comps> <li Class="CompProperties_Rottable"> <daysToRotStart>30</daysToRotStart> <rotDestroys>true</rotDestroys> </li> </comps> <thingCategories> <li>PlantMatter</li> </thingCategories> <ingestible> <preferability>DesperateOnly</preferability> <canAutoSelectAsFoodForCaravan>false</canAutoSelectAsFoodForCaravan> </ingestible> </ThingDef> <ThingDef ParentName="RoughPlantBase"> <defName>SmokeleafLeaves</defName> <label>smokeleaf leaves</label> <description>Raw cut leaves of a smokeleaf plant. Can be rolled into smokeable joints at a crafting spot.</description> <graphicData> <texPath>Things/Item/Resource/PlantFoodRaw/SmokeleafLeaves</texPath> </graphicData> <statBases> <MarketValue>1.6</MarketValue> <Flammability>1.3</Flammability> </statBases> <comps> <li Class="CompProperties_Rottable"> <daysToRotStart>30</daysToRotStart> <rotDestroys>true</rotDestroys> </li> </comps> <thingCategories> <li>PlantMatter</li> </thingCategories> <ingestible> <preferability>DesperateOnly</preferability> <canAutoSelectAsFoodForCaravan>false</canAutoSelectAsFoodForCaravan> </ingestible> </ThingDef> </Defs> 逐行解释
07-20
这是一个 XML 格式的 `Defs` 文件,用于定义 RimWorld 中的“植物类原材料”(Organic Product)的多个 `ThingDef`,即游戏中的物品定义。这些定义包括生食植物、粗制植物材料等,它们在游戏内作为资源使用。以下是逐行解释: --- ### XML 标记解释 ```xml <?xml version="1.0" encoding="utf-8" ?> ``` - 定义该 XML 文件的版本和编码格式为 UTF-8。 ```xml <Defs> ``` - 开始一个定义块,通常用于 RimWorld 模组中的自定义定义。 --- ### 基础类定义 #### OrganicProductBase(有机产品基础类) ```xml <ThingDef Abstract="True" Name="OrganicProductBase" ParentName="ResourceBase"> ``` - 定义一个抽象类 `OrganicProductBase`,继承自 `ResourceBase`。 ```xml <graphicData> <graphicClass>Graphic_Single</graphicClass> </graphicData> ``` - 图形使用 `Graphic_Single` 类型,表示单一贴图。 ```xml <statBases> <MaxHitPoints>60</MaxHitPoints> <Flammability>1.0</Flammability> <DeteriorationRate>6</DeteriorationRate> <Mass>0.03</Mass> </statBases> ``` - 基础属性:最大耐久 60、可燃性 1.0、劣化率 6、质量 0.03。 ```xml <tickerType>Rare</tickerType> ``` - 更新频率为“稀有”,表示该物品不需要频繁更新。 ```xml <healthAffectsPrice>false</healthAffectsPrice> ``` - 健康状态不影响价格。 --- #### PlantFoodRawBase(生食植物基础类) ```xml <ThingDef Abstract="True" Name="PlantFoodRawBase" ParentName="OrganicProductBase"> ``` - 抽象类 `PlantFoodRawBase`,继承自 `OrganicProductBase`。 ```xml <ingestible> <preferability>RawBad</preferability> <tasteThought>AteRawFood</tasteThought> <ingestEffect>EatVegetarian</ingestEffect> <ingestSound>RawVegetable_Eat</ingestSound> </ingestible> ``` - 可食用属性:生吃效果差、触发 `AteRawFood` 思想、素食效果、食用音效。 ```xml <statBases> <Nutrition>0.05</Nutrition> <FoodPoisonChanceFixedHuman>0.02</FoodPoisonChanceFixedHuman> </statBases> ``` - 营养值 0.05、人类中毒几率 2%。 ```xml <socialPropernessMatters>true</socialPropernessMatters> ``` - 社交礼仪影响是否食用。 ```xml <thingCategories> <li>PlantFoodRaw</li> </thingCategories> ``` - 所属分类为 `PlantFoodRaw`。 ```xml <allowedArchonexusCount>200</allowedArchonexusCount> ``` - 允许在 Archonexus 中出现的最大数量为 200。 --- ### 具体生食植物定义 #### RawPotatoes(生土豆) ```xml <ThingDef ParentName="PlantFoodRawBase"> <defName>RawPotatoes</defName> ``` - 内部名称为 `RawPotatoes`。 ```xml <label>potatoes</label> <description>Raw potatoes.</description> ``` - 显示名称和描述。 ```xml <possessionCount>10</possessionCount> ``` - 默认拥有数量为 10。 ```xml <graphicData> <texPath>Things/Item/Resource/PlantFoodRaw/Potatoes</texPath> </graphicData> ``` - 使用指定贴图路径。 ```xml <statBases> <MarketValue>1.1</MarketValue> </statBases> ``` - 市场价格为 1.1。 ```xml <ingestible> <foodType>VegetableOrFruit</foodType> </ingestible> ``` - 食物类型为蔬菜或水果。 ```xml <comps> <li Class="CompProperties_Rottable"> <daysToRotStart>30</daysToRotStart> <rotDestroys>true</rotDestroys> </li> </comps> ``` - 添加腐烂组件,30 天后开始腐烂,腐烂后销毁。 --- 其余如 `RawFungus`、`RawRice`、`RawAgave`、`RawCorn`、`RawBerries` 的结构与 `RawPotatoes` 类似,只是贴图路径、价格、腐烂时间等参数不同。 --- ### 粗制植物材料定义 #### RoughPlantBase(粗制植物基础类) ```xml <ThingDef ParentName="OrganicProductBase" Name="RoughPlantBase" Abstract="True"> ``` - 抽象类 `RoughPlantBase`,继承自 `OrganicProductBase`。 ```xml <ingestible> <foodType>Plant</foodType> <preferability>DesperateOnlyForHumanlikes</preferability> </ingestible> ``` - 仅供绝望时食用,人类不会主动吃。 ```xml <statBases> <Nutrition>0.05</Nutrition> </statBases> ``` - 营养值 0.05。 --- #### Hay(干草) ```xml <ThingDef ParentName="RoughPlantBase"> <defName>Hay</defName> ``` - 内部名称为 `Hay`。 ```xml <description>Nutrient-rich grasses and shoots...</description> ``` - 描述为动物饲料。 ```xml <stackLimit>200</stackLimit> ``` - 最大堆叠数量为 200。 ```xml <graphicData> <graphicClass>Graphic_StackCount</graphicClass> </graphicData> ``` - 使用堆叠贴图类。 ```xml <statBases> <MarketValue>0.6</MarketValue> <Mass>0.014</Mass> <Flammability>1.5</Flammability> </statBases> ``` - 市场价 0.6、质量 0.014、可燃性 1.5。 ```xml <comps> <li Class="CompProperties_Rottable">...</li> </comps> ``` - 添加腐烂组件。 ```xml <thingCategories> <li>Foods</li> </thingCategories> ``` - 所属分类为 `Foods`。 ```xml <ingestible> <optimalityOffsetFeedingAnimals>7</optimalityOffsetFeedingAnimals> </ingestible> ``` - 动物食用时的最优性偏移值为 7。 ```xml <allowedArchonexusCount>400</allowedArchonexusCount> ``` - Archonexus 中最大允许数量为 400。 --- 其余如 `RawHops`、`PsychoidLeaves`、`SmokeleafLeaves` 的结构与 `Hay` 类似,但用途不同(如酿酒、制毒、卷烟)。 --- ```xml </Defs> ``` - Defs 块结束。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值