Saruman's Army

C语言用三种策略实现贪心算法 贪心算法: 是指在对问题进行求解时,在每一步选择中都采取最好或者最优(即最有利)的选择,从而希望能够导致结果是最好或者最优的算法。但贪心算法并不能保证得到全局最优。本实验选择【-1背包问题】,请用贪心算法求解(贪心策略的选择是关键)。 问题如下:有一个背包,背包容量是150kg。现有8个物品,物品大小固定,即不可分割。 要求:尽可能让装入背包中的物品总价值最大,但前提条件是:不能超过总容量150kg。 8件物品的标号、重量、价值如下表所示: 物品 重量 价值 1 35kg 10 2 30kg 40 3 6kg 30 4 50kg 50 5 40kg 35 6 10kg 40 7 25kg 30 8 12kg 25 代码采用了三种策略来实现 立即下载

Saruman’s Army

Saruman the White must lead his army along a straight path from Isengard to Helm’s Deep. To keep track of his forces, Saruman distributes seeing stones, known as palantirs, among the troops. Each palantir has a maximum effective range of R units, and must be carried by some troop in the army (i.e., palantirs are not allowed to “free float” in mid-air). Help Saruman take control of Middle Earth by determining the minimum number of palantirs needed for Saruman to ensure that each of his minions is within R units of some palantir.

Input

The input test file will contain multiple cases. Each test case begins with a single line containing an integer R, the maximum effective range of all palantirs (where 0 ≤ R ≤ 1000), and an integer n, the number of troops in Saruman’s army (where 1 ≤ n ≤ 1000). The next line contains n integers, indicating the positions x1, …, xn of each troop (where 0 ≤ xi ≤ 1000). The end-of-file is marked by a test case with R = n = −1.

Output

For each test case, print a single integer indicating the minimum number of palantirs needed.

Sample Input

0 3
10 20 20
10 7
70 30 1 7 15 20 50
-1 -1

Sample Output

2
4

Hint

In the first test case, Saruman may place a palantir at positions 10 and 20. Here, note that a single palantir with range 0 can cover both of the troops at position 20.

In the second test case, Saruman can place palantirs at position 7 (covering troops at 1, 7, and 15), position 20 (covering positions 20 and 30), position 50, and position 70. Here, note that palantirs must be distributed among troops and are not allowed to “free float.” Thus, Saruman cannot place a palantir at position 60 to cover the troops at positions 50 and 70.

C++编写:

#include<iostream>
#include<algorithm>
using namespace std;
const int MAX_N=1000;
int Army[MAX_N+5];
int R,n;     //R是最大有效范围,n代表军队个数

void min_mark()
{
    sort(Army,Army+n);
    int i=0,min=0;
    while(i<n)
    {
        int s=Army[i++];
        while(i<n && Army[i]<=s+R)   i++;
        int p=Army[i-1];       //p是新被标记的点
        while(i<n && Army[i]<=p+R)    i++;
        min++;
    }
    cout<<min<<endl;
}

int main()
{
    ios::sync_with_stdio(false);
    while(cin>>R>>n && R!=-1 & n!=-1)
    {
        for(int i=0;i<n;i++)
            cin>>Army[i];
        min_mark();
    }
}
AG16K MCU ARM Cortex M3 其中,Input文件为Keil生成的mcu程序文件,输出为合成后的存储格式Bin文件,Program address 为上方表格中“程序运行 RAM 地址(4bytes)”,即 Keil 中设置的程序运行地址。其中 MCU 程序的地址,根据 mcu.ip 中设置的 FLASH_BIAS 进行设置。AG16K MCU 的 FPGA 工程中,需调用 alta_mcu_m3 的硬核 IP,包含在 alta_sim.v 的库文。Quartus II。//MCU 程序在 FLASH 中存放的首地址。 阅读详情

相关推荐

Cosmos Transfer2.5多模态AI模型部署与优化实战

多模态AI技术通过融合文本、图像、代码等不同模态数据,实现跨模态理解与生成。其核心原理基于Transformer架构的注意力机制,结合对比学习实现模态对齐。这类技术在智能内容创作、跨模态搜索等领域具有重要价值,尤其适合电商营销图文生成、UI设计转代码等场景。Cosmos Transfer2.5作为先进的多模态模型,支持8192 tokens长文本窗口和混合模态输入,实测推理速度达17.5 samples/s(T4显卡)。部署时需注意FlashAttention优化可提升3倍长文本处理效率,8-bit量化则能

weixin_29038303的博客 281

POJ3069 Saruman's Army Greedy

很裸的题目 Saruman's Army Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2527   Accepted: 1261 Description Saruman the White must lead his army alon

1126

从手机到单片机:聊聊ARM Cortex家族那些事,A、R、M系列到底有啥不同?

本文深入解析ARM Cortex家族的A、R、M三大系列处理器,揭示它们在性能、实时响应和能效方面的核心差异。从智能手机到汽车电子和物联网设备,Cortex-A、R、M各司其职,分别应对复杂运算、实时控制和超低功耗场景。通过技术指标对比和典型应用案例,帮助开发者理解如何根据DMIPS/MHz等关键参数选择适合的处理器架构。

weixin_31574149的博客 350

Saruman's Army 贪心

Saruman the White must lead his army along a straight path from Isengard to Helm’s Deep. To keep track of his forces, Saruman distributes seeing stones, known as palantirs, among the troops. Each pala...

Annmike的博客 630

机器人强化学习入门学习笔记

如果你追求高精度动力学仿真、用于强化学习或机器人研究,MuJoCo 是一个非常优秀的选择。如果需要快速实验、使用真实机器人模型,PyBullet 或 Gazebo可能更合适;如果你追求大规模并行训练,Isaac Gym 或 BraX会更高效。legged gym底层是​编辑isaacgym,基于gpu训练可以多环境并行训练,收敛到一个稳定行走的策略快。本视频MuJoCo基于cpu训练很慢。

xiaomu_347的博客 1458

Xshell 连接 kali 虚拟机教程

启动ssh服务,启动后重新查看ssh服务状态,如下图active(running)正常。至此便可以直接使用Xshell登录kali系统了。至此虚拟机已经可以上网。-查看ssh服务状态。

qq_40726937的博客 2219

Saruman‘s Army(贪心)

Saruman’s Army(贪心) Description Saruman the White must lead his army along a straight path from Isengard to Helm’s Deep. To keep track of his forces, Saruman distributes seeing stones, known as palantirs, among the troops. Each palantir has a maximum effect

Eagle222的博客 287

手把手教你用AFL挖掘C程序漏洞:从安装到实战(附常见错误排查)

本文详细介绍了如何使用American Fuzzy Lop (AFL)对C/C++程序进行模糊测试,从环境准备、目标程序插桩编译、初始语料库构建到实战运行与结果分析。重点讲解了AFL的安装、常见错误排查(如核心转储配置、执行速度优化)以及提升测试效率的高级策略(如持久模式、并行化),帮助开发者和安全工程师高效挖掘程序漏洞。

weixin_29014237的博客 407

Saruman's Army(贪心)——区间问题

刚接触到区间问题这样的贪心题目,写篇博客记录一下 Saruman the White must lead his army along a straight path from Isengard to Helm’s Deep. To keep track of his forces, Saruman distributes seeing stones, known as palantirs, ...

ミ渴望ヽ不可及ゞ的博客 373

面经-2023-联发科MTK-数字芯片设计

面经-2023-联发科MTK-数字芯片设计

w0shishabi的博客 2944

【PyTorch项目实战】图像分类与识别:手写数字识别(MNIST)、普适物体识别(CIFAR-10)

主要介绍了两个经典的实战项目:(1)基于 MNIST 数据集的手写数字识别(2)基于 CIFAR-10 数据集进行图像分类

📷 图像处理践行者 | 传统算法 · 深度学习 · 多模态 | 分享实战经验与技术洞见,探索从像素到智能的无限可能。 4400

【贪心】Saruman's Army题解

一道贪心题。贪心的特点是找到局部最优解,做出当前的最好选择,且不能回退,只能一路往前。动态规划主要运用于二维或三维问题,而贪心一般是一维问题。 下面给出题目: Saruman's Army Description Saruman the White must lead his army along a straight path from Isengard to Helm’s...

aizhiyan2320的博客 310

【异常】Claude Code 在 Windows CMD 中出现重影现象的排查与解决

Windows CMD运行Claude Code时出现文字重影、乱码等问题,原因是传统CMD对现代ANSI终端控制序列支持不足。解决方案包括:1)推荐安装Windows Terminal(完全支持ANSI标准);2)使用PowerShell 7;3)优化CMD设置(启用虚拟终端、UTF-8编码);4)关闭Claude Code视觉特效。建议优先使用Windows Terminal以获得最佳体验,这是最彻底的解决方案。其他方案仅能部分改善或缓解问题。

奶爸的编程之路,也就一周冷个三天 1958

Saruman's Army(POJ 3069)

POJ 3069(Saruman's Army)(贪心算法) 原题链接:http://poj.org/problem?id=3069 该题目思路相对简单,即从最左边向右以此判断即可 #include &lt;iostream&gt; #include&lt;algorithm&gt; using namespace std; const int MAX_N=1000; int n,R...

种花家的奋斗兔的博客 700

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

poj 3069 Saruman's Army

poj 3069 Saruman's Army 贪心 题目链接: http://poj.org/problem?id=3069 思路: 数组从小到大排序。从最左边的点开始,看他能够达到范围内最远的电视哪一个,找到了计数器加1,相当于在此点建立了一个标记。然后从此点继续向右延伸,看能够包括到的最远的点是哪一个。 代码: #include <iostream> #includ...

ddqb3235的博客 124

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

POJ-3069 Saruman's Army

POJ-3069 Saruman’s Army 题目链接:POJ-3069 题目大意:给你一穿点,再给你一个放置在某个点的监视器左右最大覆盖范围,问你最少放多少监视器能覆盖到所有的点 解题思路:在一头开始寻找,找到能覆盖当前点的最远圆心,这样就能把利用效率提升到最大 代码块: #include<iostream> #include<algorithm> using na...

鱼头的水题之路 198

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

POJ 3069 Saruman's Army 贪心算法

题目链接:Saruman's Army 其实就是...寻找一个点,能保证左边已经被最大化标记,右边也能拉到最远。(>_ 代码如下:View on Github #include #include #include #include #include using namespace std; int R,N,cnt; vector X; int find_target(int S

Kiritow的学园 620

POJ3069 Saruman's Army

题目链接:POJ3069 Saruman’s Army Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20492 Accepted: 9926 Description Saruman the White must lead his army along a straight path from Isengard to He...

cutlery1137的博客 315

POJ3069 Saruman's Army【贪心】

问题链接:POJ3069 Saruman's Army。 题意简述:直线上有N个点。点i的距离是Xi。从这N个点中选取若干点加上标记。对于每个点,与其距离为R的范围内必有做标记的点(包括自身)。求至少标记多少点才能满足要求。 问题分析: 这个问题可以用贪心法来解决。 先将各个点排序,从小到大顺序找必须标记的点,并且将其距离R以内的点排除。反复这个过程即可。 程序说明:(略)

海岛Blog 1866

【POJ 3069 --- Saruman's Army】

【POJ 3069 --- Saruman's Army】 Description Saruman the White must lead his army along a straight path from Isengard to Helm’s Deep. To keep track of his forces, Saruman distributes seeing stones, known...

来日浅谈的博客 426

poj-3069-Saruman's Army

http://poj.org/problem?id=3069Saruman's ArmyTime Limit:1000MSMemory Limit:65536KTotal Submissions:4256Accepted:2186DescriptionSaruman the White must lead his army along a straight path from Iseng...

weixin_30809333的博客 83
上一篇: Best Cow Line
下一篇: Fence Repair
Slatter
博客等级 码龄8年 104粉丝 128原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值