POJ3069 Saruman's Army Greedy

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: 2527 Accepted: 1261

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.

#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cstdio>

using namespace std;

struct point
{
    int l,r,x;
}p[1100];

int ans;
int n,r;
bool cmp(point a,point b)
{
   return a.l<b.l;
}

int main()
{
    while(~scanf("%d%d",&r,&n))
    {
        if(r==-1&&n==-1) break;
        for(int i=0;i<n;i++) scanf("%d",&p[i].x);
        for(int i=0;i<n;i++)
        {
            p[i].l=p[i].x-r;
            p[i].r=p[i].x+r;
        }
        sort(p,p+n,cmp);
        int l,r;
        ans=1;l=p[0].x,r=p[0].r;
        for(int i=1;i<n;i++)
        {
            if(p[i].l<=l)
                r=p[i].r;
            else if(r<p[i].x)
            {
                ans++;
                l=p[i].x;
                r=p[i].r;
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}


eNSP网络构建—建立小型局域网 企业私有网络构建运维 实战案例——使用模拟器构建局域网络 案例分析 1.架构分析 (1)需求分析 本实验的目的在于建立小型局域网。由于公司由不同部门组成,并分布在不同地点,因此需要划分不同网络实现互联互通。设计以下网络:两个部门各使用一台交换机连接,然后连接到总交换机,再通过路由器与外网以及其他部门网络相连。为了控制网络上的广播风暴,增加网络的安全性,在交换机上需要设置VLAN,在路由器与交换机之... 阅读详情

相关推荐

华为云Stack交付实战:从工勘到上线的全流程拆解

本文详细拆解华为云Stack交付全流程,从项目启动、工勘、LLD设计到硬件部署和软件安装,提供实战技巧与避坑指南。重点解析华为云Stack交付流程中的关键环节,如标准化工具使用、规范化文档管理和流程化协作,帮助团队高效完成从工勘到上线的复杂项目交付。

weixin_29243063的博客 160

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的博客 1196

HFSS实战:手把手教你设计3.3-10.6GHz超宽带微带天线(附S11优化技巧)

本文提供了一份基于Ansys HFSS的超宽带微带天线实战设计指南,详细讲解了从3.3GHz到10.6GHz天线的建模、仿真与优化全流程。文章重点剖析了阻抗匹配的核心挑战,并分享了通过接地板开槽、参数化扫描及目标驱动优化等系统性技巧来优化S11曲线的实用方法,帮助射频工程师快速掌握超宽带天线设计的关键技能。

2y3u4i5o6p的博客 1085

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

VS2022离线环境下的NuGet包管理:从本地源到依赖项全解析

本文详细解析了在VS2022离线环境下如何高效管理NuGet包,包括搭建本地NuGet源的两种方案(本地文件夹源和私有NuGet服务器)、获取离线包的三大渠道以及依赖项处理的实战技巧。通过自动化脚本和依赖树分析工具,帮助开发者解决离线环境中的依赖项黑洞问题,提升开发效率。

5f4d3s2a1q的博客 1067

深度解析跨境支付之业务架构

详解跨境支付之业务架构

weixin_47493863的博客 1891

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的博客 289

在Cursor中集成Ollama与Qwen2.5‑Coder:零成本本地AI开发环境搭建

本文详细介绍了如何零成本搭建本地AI开发环境,通过将Cursor IDE与本地运行的Ollama及Qwen2.5-Coder模型集成,实现数据不出本地的智能编程辅助。教程涵盖Ollama安装、模型拉取、ngrok内网穿透配置及Cursor设置等核心步骤,并提供了实战测试与常见问题排错指南,为注重隐私与成本的开发者提供安全高效的解决方案。

weixin_29296241的博客 1080

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

告别里程焦虑:手把手教你用MATLAB/Simulink仿真电动车AMT换挡规律(附代码)

本文详细介绍了如何使用MATLAB/Simulink仿真电动车AMT换挡规律,帮助解决里程焦虑问题。通过搭建完整的仿真系统,包括电机MAP图处理、Stateflow逻辑实现和动力性与经济性换挡曲线生成,优化电动车换挡策略以提升续航与性能。附带的代码示例和实用技巧使开发者能够快速上手并验证仿真结果。

weixin_26757925的博客 309

10X Genomics单细胞数据分析实战:从FASTQ到UMAP可视化的完整流程

本文详细介绍了10X Genomics单细胞数据分析的完整流程,从FASTQ原始数据处理到UMAP可视化。通过Seurat工具包实现数据质量控制、标准化、降维聚类和细胞注释,特别适合生物信息学新手学习。文章包含可复现的代码示例和常见问题解决方案,帮助研究者快速掌握单细胞测序数据分析的核心技术。

weixin_30952535的博客 540

【贪心】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

对halcon形状匹配算法的一点分析

基于Intel VTune Profiler对halcon形状匹配算法的一点分析

manuoo的专栏 579

Greedy:Saruman's Army(POJ 3069)

               2015-09-06               萨鲁曼军队                 问题大意:萨鲁曼白想要让他的军队从sengard到Helm’s Deep,为了跟踪他的军队,他在军队中放置了魔法石(军队是一条线),魔法石可以看到前后距离为R的距离,为了让魔法石发挥最大的效益,魔法石戴在军人的身上,问你怎么才能使用最少的石头 问题很清...

bandilan2400的博客 145

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

Devexpress16.2.6破解工具

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

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 along a strai

kalilili的专栏 604

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

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的博客 317

老版QQ原生表情gif动态

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

POJ 3069 Saruman's Army (贪心)

Saruman's Army Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4487   Accepted: 2314 Description Saruman the White must lead his army along a straight

sxk 885

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

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

bu_shushu的博客 847

POJ 3069 Saruman's Army

Saruman's Army Time Limit:1000MS Memory Limit:65536K Total Submissions:2582 Accepted:1284 Description Saruman the White must lead his army along a straight p...

weixin_30457551的博客 243

贪心算法——Saruman's Army

问题 源码 #include<iostream> #include<algorithm> #include<queue> using namespace std; const int N=100; int n=6;//点的个数 int R=10;//半径大小 int X[N]= {1,7,15,20,30,50}; //各个点坐标 queue<int&g...

The_Only_God的博客 318

萨鲁曼的军队 Saruman‘s Army(挑战程序设计竞赛)

直线上有NN个点,点ii的位置是x_ixi​(多个点可以在同一个位置)。 可以在NN个点中选择某些点打上标记。 要求最终,每个点,距离其RR范围内必须有带有标记的点(自己本身带有标记的点,可以认为与其距离为00的地方有一个带有标记的点)。 在满足上述条件下,至少要选中多少个点打上标记? 输入 多组测试数据,每组测试: 第一行两个整数R(0 \leq R \leq 1000)R(0≤R≤1000)和N(1 \leq N \leq 1000)N(1≤N≤1000) 第...

Recursions的博客 466

Saruman's Army(贪心)

 Saruman's Army Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 3069 Description Saruman the White must lead hi

bao_libra的专栏 687

【POJ3069】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 troop...

Clayborn voyage without an end 219

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

qq_40129237的博客 313

每日一题 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 effective

撒浪嘿呦✨的博客 268

算法入门 6.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 effective range of R unit

m0_57006708的博客 2113
上一篇: ZOJ2112 Dynamic Rankings 线段树套平衡树
下一篇: ZOJ3261 Connections in Galaxy War 并查集
Albafica
博客等级 码龄16年 98粉丝 359原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值