首页 > 其他分享 >贪心

贪心

时间:2022-10-26 20:01:34浏览次数:67  
标签:index 20 int palantirs Saruman position 贪心


Saruman's Army

Time Limit: 1000MS

 

Memory Limit: 65536K

Total Submissions: 19615

 

Accepted: 9580

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.

Source

​Stanford Local 2006​

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<queue>
#include<cstdio>

using namespace std;

const int MAX_LEN = 1005;

int N, R,Result;
int a[MAX_LEN];

int main() {
scanf("%d%d", &R, &N);
while (R != -1 && N != -1) {
for (int i = 0; i < N; i++) {
scanf("%d", &a[i]);
}
sort(a, a + N);
int index = 0; int Result = 0;
while (index < N) {
int Start = a[index++];
while (index < N && a[index] <= Start + R) {
index++;
}
int Pos = a[index - 1];
while (index < N && a[index] <= Pos + R) {
index++;
}
Result++;
}
printf("%d\n", Result);
scanf("%d%d", &R, &N);
}
system("pause");
return 0;
}

 

标签:index,20,int,palantirs,Saruman,position,贪心
From: https://blog.51cto.com/u_13121994/5798307

相关文章

  • 贪心--带最少的零钱
    题目描述你就要去购物了,现在你手上有N种不同面值的硬币,每种硬币有无限多个。为了方便购物,你希望带尽量少的硬币,但要能组合出1到X之间的任意值。输入格式第一行两个数X、N,以......
  • Monster (贪心)
    Monster【题目描述】明明的手机上有这样一个游戏,有一排n个怪物,每个怪物的血量是mi。现在明明可以射出k个伤害均为p的火球射向某些怪物。当某个火球射到第i个怪物,除了这个怪......
  • BZOJ 1007(水平可见直线-斜率排序+栈贪心)
    1007:[HNOI2008]水平可见直线TimeLimit: 1Sec  MemoryLimit: 162MBSubmit: 1830  Solved: 656[​​Submit​​][​​Status​​][​​Discuss​​]......
  • 贪心算法-455分发饼干
    题目455分发饼干假设你是一位很棒的家长,想要给你的孩子们一些小饼干。但是,每个孩子最多只能给一块饼干。对每个孩子i,都有一个胃口值 g[i],这是能让孩子们满足胃口的饼干......
  • 第三个贪心
    A-汽车加油问题#include<bits/stdc++.h>usingnamespacestd;inta[5010],n,k;signedmain(){cin>>n>>k;for(inti=0;i<=k;i++)......
  • 贪心算法 455
    455.分发饼干classSolution{publicintfindContentChildren(int[]g,int[]s){Arrays.sort(g);//孩子列表Arrays.sort(s);//饼干列表......
  • Madoka and the Sixth-graders (全排列队列,每一个点可以向外连1条线题型+倍增法处理
    题意:Madoka的教室里有 nn 个座位,一开始,编号为 ii 的座位上坐着编号为 b_i(1\leb_i\len)bi​(1≤bi​≤n) 的同学。门外有排成一队的,编号从 n+1n+1 开始的,......
  • Madoka and Childish Pranks (贪心+逆序即可)
    题目大意: 给定一个01矩阵,其中0代表黑色,1代表白色Madoka要对一个同样大小的0矩阵染色,每次染色可以将一个矩形染成国际象棋的颜色(-1)^(x+y)的颜色(1白2黑)现......
  • Scapegoat Gym - 101775B (贪心+推公式)
    题目链接https://vjudge.csgrandeur.cn/problem/Gym-101775B原文题意:现在某人闯祸了,产生了N个锅,每个锅有个严重点数,现在可以安排M个替罪羊去背锅。每个替罪羊最多......
  • 贪心算法
    贪心算法所采用的策略,是保证每次操作都是局部最优,从而使最后结果是全局最优。1.n个孩子站成一排。给你一个整数数组ratings表示每个孩子的评分。你需要按照以下要求,......