首页 > 其他分享 >POJ--3069 Saruman's Army(贪心)

POJ--3069 Saruman's Army(贪心)

时间:2023-01-25 10:55:47浏览次数:60  
标签:case 20 Army -- palantirs int POJ Saruman test

记录
0:33 2023-1-24

http://poj.org/problem?id=3069

reference:《挑战程序设计竞赛(第2版)》2.2.4 p45

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

贪心算法,体现在不断地找到当前当前的最优策略,这里最优策略体现在,尽可能的让中心点靠后。

#include<cstdio>
#include<algorithm>
using namespace std;
#define MAX_N 10000

int N, R;
int X[MAX_N];

void solve() {
    sort(X, X + N);

    int i = 0, ans = 0;
    while (i < N) {
        int s = X[i++];

        while(i < N && X[i] <= s + R) i++;
        
        int p = X[i - 1];

        while(i < N && X[i] <= p + R) i++;

        ans++;
    }
    printf("%d\n", ans);
}

int main() {
    while (~scanf("%d %d", &R, &N)) {
        if(R == -1 && N == -1) {
            break;
        }
        for(int i = 0; i < N; i++) {
            scanf("%d", &X[i]);
        }
        solve();
    }
}

标签:case,20,Army,--,palantirs,int,POJ,Saruman,test
From: https://www.cnblogs.com/57one/p/17066741.html

相关文章

  • 边缘节点管理
    KubeEdge中对边缘节点的管理有如下3种形式。1)以节点的形式管理边缘计算资源:在云上部署整个系统的控制面,计算资源在边缘都以节点的形式来管理。2)以独立集群的形式管理边缘......
  • C++ 实现复制赋值运算符重载
    考察点返回值类型MyClass&,可以连续赋值参数类型:(constMyClass&rhs)或者(MyClassrhs)值传递(copy-swap)自赋值安全无内存泄漏,旧值需要析构异常安全参考实现c......
  • 云边协同CloudCore组件
    CloudCore架构CloudCore通过List/Watch的方式与云交互,将监听到的事件下发到边缘,同时负责接收边缘以事件的形式上报的状态数据。这些功能是由CloudCore中的不同模块完成的......
  • 天梯赛练习题L2(036-040)
    L2-036网红点打卡攻略题目不难,但是需要仔细解读题目所给条件是什么意思我们需要在所给的每个打卡点打卡只一次,说明每个网红店都只能经过一次从家出发,顺着道路所......
  • Docker 基础 - 2
    容器操作系统类型Busybox集成了一百多个最常用Linux命令和工具的软件工具箱.包含catechogrepfindmounttelnet等Busybox是Linux系统的瑞士军刀Debian/Ubun......
  • caddyserver 几个有用的配置参数
    不是介绍caddyserver的配置参数,核心是关于ssl证书以及配置存储存储的几个参数XDG_DATA_HOME主要是关于caddyserver基于acme协议处理证书的,比较有用,可以更好的管理证......
  • 管理边缘负载EdgeCore组件
     EdgeCore架构EdgeCore包含的功能模块比较多,包括EdgeHub、MetaManager、DeviceTwin、EventBus、Edged、EdgeMesh、CSI和CNI。接下来逐个对其进行解析。1)EdgeHub:KubeEdg......
  • 与终端设备交互Mapper组件
    Mapper架构从与KubeEdge边部分EdgeCore对接的协议划分,终端设备可以分为通过MQTT协议进行对接的终端设备和通过HTTP进行对接的终端设备。1)通过MQTT协议进行对接的终端设......
  • 云边协同
    云边协同机制是边缘计算系统中边部分解决方案KubeEdge的关键。有了该机制,KubeEdge便可以适应边缘恶劣的网络环境,即在边缘节点与云失去联系时,边缘节点也可以独立工作,不影响......
  • 每日一题1.25
      我的思路:把&(x)直接当作x带入f(x)然后求出&(x)=根号ln(1-x);作用域是x<1;复合函数求定义域:  x的定义域做错了,因为e的x次方是单调的增函数,而&(x)的平方......