POJ 3069 - 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
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 2727 Accepted: 1373

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 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.



==============================

范围半径为R,并且只能放置在给定的坐标上


#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;
int ran,n,a[1111];

int main()
{
    while(~scanf("%d%d",&ran,&n))
    {
        if(ran==-1&&n==-1) break;
        for(int i=0;i<n;i++)
            scanf("%d",&a[i]);
        sort(a,a+n);
        int temp,mid,ans=0;
        for(int i=0;i<n;i++)
        {
            mid=i;temp=i;
            for(int j=i+1;j<n;j++)
            {
                if(a[j]-a[i]<=ran)
                {
                    mid=j;
                    temp=j;
                }
                else if(a[j]-a[mid]<=ran)
                {
                    temp=j;
                }
                else break;
            }
            i=temp;
            ans++;
        }
        printf("%d\n",ans);
    }
    return 0;
}


华为无线WiFi实战:基于802.1x与NPS的企业级认证部署指南 本文提供了一份详尽的华为无线WiFi企业级认证部署实战指南。通过整合802.1x协议与Windows NPS服务器,构建基于AD域账号的安全认证体系,彻底解决传统WiFi密码共享带来的安全风险。文章详细拆解了从NPS服务器配置、证书部署到华为AC端RADIUS模板、AAA方案、认证域及VAP模板调用的全流程,并附有终端连接测试与常见故障排查方法,助力企业实现高效、安全的无线网络接入管理。 阅读详情

相关推荐

解码运动想象:BCI Competition IV 2a数据集实战指南

本文提供了BCI Competition IV 2a数据集的实战指南,详细解析了该经典运动想象脑电数据集的结构、文件格式与实验范式。通过MNE-Python等工具,逐步演示了从原始信号读取、预处理(通道选择、滤波、分段)到特征提取(CSP、频带功率)和模型训练(LDA、SVM)的完整流程,并强调了受试者独立评估的重要性,为BCI研究者提供了可复现的入门与进阶路径。

weixin_29304985的博客 362

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. Ea...

weixin_45653525的博客 1195

AltiumDesigner 09安装过程破解问题 .

首先分享一下破解文件,网上总是下载不到,好不容易下载到了,放在网盘上,有需要的自取。AltiumDesigner 09破解文件。链接:https://pan.baidu.com/s/1ZGvbR_6T51hpcaFbQiMX5w 密码:qwvaAltiumDesigner 09安装安装文件,比较大,包含破解文件:链接:https://pan.baidu.com/s/1mfgragB519jpGfR...

xiaoxiao_rabbit的博客 3万+

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

用python将dycom .dcm格式图像转换为nifti .nii

因为发现目前搜索到的多数中文教程并不能正确将FMRI图像正确转换,例如很多用simpleITk的seriesreader的,所以写了一下自己正确转换的方法。因为FMRI是一个4D数据,有时间轴,所以用seriesreader读写出来的图像把时间轴维度给降维了。当然你用SPM12工具的dycom import也是能正确转换的,但是我还是喜欢python这样的,代码直接简单。

qq_41103479的博客 3609

从零开始 使用OMNET++结合VEINS,INET和SUMO的联合仿真

搭建仿真的环境,导入VEINS和INET开源库,使用INET库中提供的网络协议和功能模型来模拟通信协议和网络功能,使用SUMO创建道路网络和车辆轨迹,并导出到OMNeT++中 在OMNeT++中使用VEINS通过socket和SUMO连接,借助VEINS实现的TRACI接口来设置车辆相关的行为和场景,最后在OMNET++中结合VEINS,SUMO和INET运行联合仿真实验,并分析车辆在遇到紧急情况下的通信状况和具体行为

mjmmm的博客 1万+

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

别再手动调参了!用Optuna+PyTorch搞定Transformer时序预测超参数(附完整代码)

本文详细介绍了如何利用Optuna和PyTorch实现Transformer时序预测模型的自动化超参数调优。通过贝叶斯优化技术,显著提升单变量时序预测的效率和准确性,并附有完整代码实现。文章涵盖模型架构设计、数据预处理、Optuna调参实战及生产环境部署建议,为开发者提供一站式解决方案。

weixin_33704234的博客 576

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

【异常】CNB提示The bill authorization scope cannot access the current request.

摘要:CNB API返回403错误,表明当前token的授权范围无法访问目标仓库。主要原因是:1) REPO_SLUG填写错误,导致资源地址不匹配;2) CNB_TOKEN未授权目标仓库;3) 账号账单状态异常。解决步骤:先检查REPO_SLUG是否与CNB平台仓库地址完全一致;再确认CNB_TOKEN是否绑定目标仓库且有读取权限;最后检查账号账单是否正常且覆盖目标资源。完成排查后更新配置即可解决此授权问题。

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

最新Python爬虫实战(入门爬虫篇)——案例17:某天气网40天天气预报数据采集(详细爬虫思路截图+抓包动图演示+完整爬虫代码+详细注释)

本次案例通过分析某天气网的数据接口,成功实现了对40天天气预报数据的批量采集。抓包分析:利用浏览器开发者工具找到隐藏的JSONP数据接口,掌握URL构成规律。请求模拟:构造带有Referer和User-Agent的请求头,添加时间戳参数避免缓存,获取原始数据。数据解析:处理JSONP格式,去除前缀后使用json模块解析,提取所需字段并规范化日期格式。数据存储:利用pandas将结构化数据保存为Excel文件,便于后续分析和使用。动态数据接口的发现与分析JSONP数据的清洗与解析。

努力让自己发光,对的人才能迎着光而来 22万+

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

RTDETR最强结构图 | 包含模型整体结构图 | 全模块展开图 | AiFi模块展开图

RT-DETR 最全结构图

Coding... 9599

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

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

poj 3069 Saruman's Army

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

ddqb3235的博客 124

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

POJ-3069 Saruman's Army

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

鱼头的水题之路 198

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

【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

POJ3069 Saruman's Army【贪心】

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

海岛Blog 1866

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

POJ 3069 Saruman's Army(贪心)

题目链接: http://poj.org/problem?id=3069 解题思路: 代码里已经写的很清楚了。。。贪心就行了。。。 AC代码: #include #include #include using namespace std; int n,r; int a[1010]; void solve(){ sort(a,a+n); int i = 0,

piaocoder 507

【题解】POJ 3069 Saruman's Army(贪心)

POJ 3069 Saruman’s Army 原题 白种人萨鲁曼必须带领他的军队沿着从艾辛格到圣盔谷的直线前进。为了跟踪他的部队,萨鲁曼在部队中分配了看得见的石头,这些石头被称为palantirs。每个palantir都有一个最大有效距离的R单位,并且必须由军队中的某些部队携带。, palantirs不允许在半空中“自由漂浮”)。帮助萨鲁曼控制中土世界,通过确定萨鲁曼所需要的最低帕兰提尔数来确...

bu_shushu的博客 844

POJ——3069 Saruman's Army

题目链接:http://poj.org/problem?id=3069 题目大意: 直线上有N个点,需要将它们中的若干个点标记,每个标记点的影响范围是R,问最少需要多少标记点可以覆盖所有的点? 输入: 输入多个用例。每个用例第一行输入两个整数,第一个是标记点的影响范围R (0<=...

chiweiluo5790的博客 300

【贪心】——Saruman's Army(POJ 3069)

题目链接:http://poj.org/problem?id=3069 题目思路:以最左边的点为圆心,往右边找半径R内最远的点,并做标记。 接着,以当前的点为圆心,往右边找超过半径的第一个点,把这个点视作起点(因为它不需要管左边区域) 也就是当前点为最左边的点了,重复以上步骤。 #include<iostrea...

九袋面的博客 174

【OJ】贪心法 Saruman's Army POJ 3069 /acmclub 12132

题目链接:点击打开链接

Wu_Being 的优快云 博客 705
上一篇: POJ 1836 - Alignment(最长递增子序列)
下一篇: POJ 1458 & HDU 1159 - Common Subsequence(最长公共子序列)
coco430
博客等级 码龄14年 9粉丝 107原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值