我是靠谱客的博主 拼搏水池,最近开发中收集的这篇文章主要介绍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.
白人萨鲁曼必须带领他的军队沿着一条从伊辛格到赫尔姆湾的直线行进。为了追踪他的部队,萨鲁曼在部队中分发了一种叫做帕兰提斯的石头。每个帕兰蒂尔都有一个最大有效射程的R单位,必须由军队中的一些部队携带(即帕兰蒂尔不允许在空中“自由漂浮”)。帮助萨鲁曼控制中土,确定萨鲁曼所需的最少宫殿数量,以确保他的每一个仆从都在某个宫殿的R单位内。

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.
输入测试文件将包含多个案例。每个测试用例以包含整数R、所有palantir的最大有效范围(其中0≤R≤1000)和整数n(其中1≤n≤1000)的单行开始。下一行包含n个整数,指示每个部队的位置x1,…,xn(其中0个小于席1000)。文件结尾由一个R=n=-1的测试用例标记。

Output

For each test case, print a single integer indicating the minimum number of palantirs needed.
对于每个测试用例,打印一个表示所需最少palantir数的整数。

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 <algorithm>
#include <vector>
using namespace std;

int main()
{
    int n, r;
    while (cin >> r >> n)
    {
        if (n == -1)
            return 0;
        vector<int> v;
        for (int i = 0; i < n; i++)
        {
            int x;
            cin >> x;
            v.push_back(x);
        }
        // 把位置点排序
        sort(v.begin(), v.end());
        int ans = 0;
        // 坐标
        int i = 0;
        while (i < n)
        {
            // x是现在还没有被覆盖的最左边的点
            int x = v[i];
            // 一直往右 直到那个不能覆盖的点(不怎么严谨)
            while (i < n && x + r >= v[i])
                i++;
            // 不能覆盖的点减一就是能覆盖的点了(中点)
            int p = i - 1;
            // 一直往右找到这个中点不能覆盖的第一个点
            while (i < n && v[p] + r >= v[i])
                i++;
            ans++;
        }
        cout << ans << "n";
    }
    return 0;
}

最后

以上就是拼搏水池为你收集整理的Saruman's Army的全部内容,希望文章能够帮你解决Saruman's Army所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(39)

评论列表共有 0 条评论

立即
投稿
返回
顶部