C/-d D - Saruman's Army

2023-12-27 03:09
文章标签 army saruman

本文主要是介绍C/-d D - Saruman's Army,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

呀吧  可算是a掉了

补得题,可算是懂得题目是啥意思了,就是最少包含范围,在给定的数左右 r的范围内都是可以的覆盖的,

对于派出的兵,能够防守一个范围的,问最少守几个数


后来发现这其实也是一个贪心算法的例题

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 ofR 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 integerR, the maximum effective range of all palantirs (where 0 ≤ R ≤ 1000), and an integern, the number of troops in Saruman’s army (where 1 ≤ n ≤ 1000). The next line contains n integers, indicating the positionsx1, …, xn of each troop (where 0 ≤ xi ≤ 1000). The end-of-file is marked by a test case withR = 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 <iostream>
#include<algorithm>using namespace std;
int army[1005];
int main()
{int r,n;while(cin>>r>>n&&(r!=-1&&n!=-1)){   int num=0;for(int i=0;i<n;i++)cin>>army[i];sort(army,army+n);for(int i=0;i<n;){int sum=0;int mid=i;int left=i;while(i<n){if(army[mid]-army[left]>r)break;if(army[i]-army[mid]<=r){i++;}else{mid++;}}num++;}cout <<num<< endl;}return 0;
}

贪心算法:

从最左方开始,寻找范围内第一个节点,即驻扎点,标记后,再寻找右方新的第一个超出范围的点,然后重新开始

#include <iostream>
#include<algorithm>using namespace std;
int army[1005];
int r,n;
void solve()
{int i=0,ans=0;while(i<n){int s=army[i++];//第一个点的坐标,++后进入下一个点,其实等价于 s=army[i];i=i+1;while(i<n&&army[i]<=s+r) i++;int p=army[i-1];//此处不能忘了减一,因为此时的i已经超出了范围,需要减一while(i<n&&army[i]<=p+r) i++;ans++;}cout<<ans<<endl;
}
int main()
{while(cin>>r>>n&&(r!=-1&&n!=-1)){   int num=0;for(int i=0;i<n;i++)cin>>army[i];sort(army,army+n);solve();}return 0;
}


这篇关于C/-d D - Saruman's Army的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/541713

相关文章

Codeforces Round #291 (Div. 2) D. R2D2 and Droid Army RMQ问题 ST算法

D. R2D2 and Droid Army time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output An army of n droids is lined up in one row. Each

Army CodeForces - 38A

http://codeforces.com/problemset/problem/38/A 将l到r-1的数加起来即可 #include<bits/stdc++.h>using namespace std;int main(){int n;int aa[111];while(cin>>n){for(int i=1;i<=n-1;i++)cin>>aa[i];long long

【贪心】POJ 3069:Saruman‘s Army

一、题目内容 POJ 3069 原题地址 二、题意解释 一个直线上有N个点。点i的距离是Xi。从这些点中选取若干个加上标记。要求:对于每个点,与其距离为R的范围内必有做标记的点(包括自身)。求至少标记多少点才能满足要求。 三、代码及注释 #include<stdio.h>#include<algorithm>using namespace std;int X[1001];in

HTB pwn Dragon Army

逆向分析 程序使用了alloca函数扩大了栈区 此处可以泄露libc的地址 程序主要功能在下面 while ( 1 ){while ( 1 ){fflush(stdin);fflush(_bss_start);fprintf(_bss_start, "\n%sDragons: [%d/%d]%s\n\n", "\x1B[1;34m", v5, 13LL, "\x1B[1;37m");f

POJ 3069 Saruman's Army

题目链接:POJ 3069 题意:第一行r 和 n,表示半径为r,n个点。要求每个点周围距离r以内(包括r和0)都必须有1个点被标记,问至少需要标记多少个点。 思路:贪心,先从最左边点开始找与其距离最接近r且小于r的点,将此点标记,然后以此点开始向右找与其距离最接近r且大于r的点,将这个点作为新的起点,重复以上步骤。 注意判断不能超过界限。 挑战程序设计竞赛,贪心 2.2

攻防世界PWN之oneman_army题解

oneman_army 首先,检查一下程序的保护机制 然后,我们用IDA分析一下,9011功能可以溢出 最大申请0x1FF大小的堆,申请次数没有限制 Delete功能没有把指针清空 本题,已知libc为2.27,所以存在tcache bin机制,可以很容易利用。我们肯定是要攻击free_hook或者malloc_hook,那么首先需要泄露地址,而泄露地址,需要利用unso