40007045Saruman's Army


试题描述

直线上有N个点。点i的位置是Xi。从这N个点中选择若干个,给它们加上标记。对每一个点,其距离为R以内的区域里必须有带有标记的点(自己本身才有标记的点,可以认为与其距离为0的地方有一个带有标记的点)。在满足这个条件的情况,希望能为尽可能少的点添加标记。请问至少有多少个点被加上标记?


输入

共两行,第一行包含两个整数 N 和 R ,分别表示点的个数 N 和距离 R;第二行包含 N 个整整数,依次表示 N 个点的坐标。两数之间用一个空格分隔。

输出

一个数,表示被标记的点的个数

输入示例

6 10
1 7 15 20 30 50

输出示例

3

其他说明

限制条件:
1≤N≤1000
0≤R≤1000
0≤Xi≤1000
 

从左向右依次搜索就行了。算是一个比较简单的数学问题。

(然而标题和内容并没有什么关系)

 1 #include <iostream>
 2
 3 using namespace std;
 4 int a[1001];
 5 int main()
 6 {
 7     int n,r,i;
 8     scanf("%d%d",&n,&r);
 9     for(i=1;i<=n;i++) scanf("%d",&a[i]);
10     int ans=0;
11     sort(a+1,a+n+1);
12     for(i=2;i<=n;i++)  //从左往右依次判断
13     {
14         if(a[i]-a[i-1]<=r) {ans++;i++;}  //可覆盖前一个数,则标记当前点,并i++.不然的话第i+1个点如果能覆盖第i个点的话,也会ans++.但第i个点已经标记了,就不用在标记第i+1个了
15         else ans++;  //覆盖不了
16     }
17     printf("%d",ans);
18     //system("pause");
19     return 0;
20 }

40007045Saruman‘s Army

40007045Saruman's Army

时间: 2024-10-06 00:39:07

40007045Saruman's Army的相关文章

40007045Saruman&#39;s Army(C++)

40007045Saruman's Army 难度级别:A: 运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 直线上有N个点.点i的位置是Xi.从这N个点中选择若干个,给它们加上标记.对每一个点,其距离为R以内的区域里必须有带有标记的点(自己本身才有标记的点,可以认为与其距离为0的地方有一个带有标记的点).在满足这个条件的情况,希望能为尽可能少的点添加标记.请问至少有多少个点被加上标记? 输入 共两行,第一行包含两个整数 N 和 R ,分别

POJ 3069 Saruman&#39;s Army (贪心)

题目大意:直线上有N个点,点i的位置是Xi,从这N个点中选取若干,给他们加上标记,对每一个点,其距离为R以内的区域内必须有被标记的点.求至少需要多少个点被标记. 题目思路:设最左边的点:点p的坐标为x,那么离其距离为R的点的坐标为(x+R),我们应该标记的点应为坐标最接近且小于等于(x+R)的点p,则此时[x,p+R]范围内点点均被标记.依次进行下去,直到包含所有点为止. #include<stdio.h> #include<queue> #include<iostream&

Saruman&#39;s Army

第一部分:题目 题目链接:http://poj.org/problem?id=3069 Saruman's Army Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6839   Accepted: 3516 Description Saruman the White must lead his army along a straight path from Isengard to Helm’s Deep. To keep

poj 3069 Saruman&#39;s Army

Saruman's Army Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5760   Accepted: 2949 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 se

Saruman&#39;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 his army along a straight path from Isengard to Helm's Deep. To keep track of his f

Linux Netcat command – The swiss army knife of net

Swiss Army Knife of networking netcat is a versatile tool that is able to read and write data across TCP and UDP network . Combined with other tools and redirection it can be used in number of ways in your scripts. You will be surprised to see what y

Educational Codeforces Round 22 E. Army Creation 主席树 或 分块

E. Army Creation As you might remember from our previous rounds, Vova really likes computer games. Now he is playing a strategy game known as Rage of Empires. In the game Vova can hire n different warriors; ith warrior has the type ai. Vova wants to

POJ 3069 Saruman&#39;s Army (简单贪心)

Saruman's Army Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5343   Accepted: 2733 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 se

贪心/POJ 3069 Saruman&#39;s Army

1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 int n,r,ans; 6 int a[1010]; 7 int cmax(int x,int y){return x>y?x:y;} 8 int main() 9 { 10 scanf("%d%d",&r,&n); 11 while (n!=-1 &