Greedy:萨鲁曼军队(POJ 3069)

               2015-09-06

              北大神奇的萨鲁曼军队

               

问题大意:萨鲁曼白想要让他的军队从sengard到Helm’s Deep,为了跟踪他的军队,他在军队中放置了魔法石(军队是一条线),魔法石可以看到前后距离为R的距离,为了让魔法石发挥最大的效益,魔法石戴在军人的身上,问你怎么才能使用最少的石头

问题很清晰,思路也很清晰,这道题挺典型的,就是贪心算法,

很容易想到的就是我们只用在距离内找到最后的点(人),然后把魔法石放到其上面就行了,然后依次类推,最后就可以达到最少的数量

具体可以以2R为一次循环,在前R的区间内我们找到要标记的点,然后移动R的距离,再来一次就可以了。很容易想到,画个图就可以了

          

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #define SWAP(a,b) { (*a)^=(*b);(*b)^=(*a);(*a)^=(*b);}
 4 #define CUTOFF 20
 5
 6 typedef int Position;
 7
 8 void Quick_Sort(Position *, Position, Position);
 9 int Get_Pivot(Position, Position, Position,Position *);
10 void Insertion_Sort(Position *, Position, Position);
11 void Search(Position *, const int, const int);
12
13 int main(void)
14 {
15     int R, T, i;
16     Position *Troops = NULL;
17     while (~scanf("%d%d", &R, &T)
18         && R >= 0
19         && T >= 0)
20     {
21         Troops = (Position *)malloc(sizeof(int)*T);
22         for (i = 0; i < T; i++)
23             scanf("%d", &Troops[i]);
24         Quick_Sort(Troops, 0, T - 1);
25         Search(Troops, R, T);
26         free(Troops);
27     }
28 }
29
30 int Get_Pivot(Position left, Position right, Position mid,Position *A)
31 {
32     if (A[left] > A[mid]) SWAP(&A[left], &A[mid]);
33     if (A[left] > A[right]) SWAP(&A[left], &A[right]);
34     if (A[mid] > A[right]) SWAP(&A[mid], &A[right]);
35
36     SWAP(&A[mid], &A[right]);//隐藏枢纽元
37     return A[right];
38 }
39
40 void Quick_Sort(Position *A, Position left, Position right)
41 {
42     Position i = left, j = right , mid = (left + right) / 2;
43     int pivot;
44     if (right - left > CUTOFF)
45     {
46         pivot = Get_Pivot(left, right, mid, A);
47         while (1)
48         {
49             while (A[--j] > pivot);
50             while (A[++i] < pivot);
51             if (i < j)
52                 SWAP(&A[i], &A[j])
53             else break;
54         }
55         SWAP(&A[i], &A[right]);//重新显示枢纽元
56         Quick_Sort(A, left, i - 1);
57         Quick_Sort(A, i + 1, right);
58     }
59     else Insertion_Sort(A, left, right);//转插入排序
60 }
61
62 void Insertion_Sort(Position *A, Position left, Position right)
63 {
64     Position i, j;
65     int tmp;
66     for (i = left; i <= right; i++)
67     {
68         tmp = A[i];
69         for (j = i; j > left && A[j - 1] > tmp; j--)
70             A[j] = A[j - 1];
71         A[j] = tmp;
72     }
73 }
74
75 void Search(Position *Troops, const int R, const int T)
76 {
77     Position i = 0, mark, s, ans = 0;
78     for (; i < T;)
79     {
80         s = Troops[i++];//获得区间的最左点
81         for (; i < T && Troops[i] <= s + R; i++);
82         mark = Troops[i - 1];//在R内的最右点,标记
83         for (; i < T && Troops[i] <= mark + R; i++);
84         ans++;
85     }
86     printf("%d\n", ans);
87 }
时间: 2024-10-11 18:12:23

Greedy:萨鲁曼军队(POJ 3069)的相关文章

编程算法 - 萨鲁曼的军队(Saruman&#39;s Army) 代码(C)

萨鲁曼的军队(Saruman's Army) 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 直线上有N个点, 每一个点, 其距离为R以内的区域里, 必须带有标记的点, 本身的距离为0. 尽可能少的添加标记点, 至少要有多少点被加上标记? 贪心算法, 从最左边的点开始, 依次查找距离为R需要添加标记的点, 直到结束. 代码: /* * main.cpp * * Created on: 2014.7.17 * Author: spike */

编程算法 - 萨鲁曼的军队(Saruman&amp;#39;s Army) 代码(C)

萨鲁曼的军队(Saruman's Army) 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 直线上有N个点, 每个点, 其距离为R以内的区域里, 必须带有标记的点, 本身的距离为0. 尽可能少的加入?标记点, 至少要有多少点被加上标记? 贪心算法, 从最左边的点開始, 依次查找距离为R须要加入?标记的点, 直到结束. 代码: /* * main.cpp * * Created on: 2014.7.17 * Author: spike *

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&

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

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 &

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

Saruman's Army Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 3069 Appoint description:  System Crawler  (2015-04-27) Description Saruman the White must lead his army along a straight path from

Saruman&#39;s Army (POJ 3069)

直线上有N个点.点i的位置是Xi.从这N个点中选择若干个,给它们加上标记.对每一个点,其距离为R以内的区域里必须又带有标记的点(自己本身带有标记的点,可以认为与其距离为0的地方有一个带有标记的点).在满足这个条件的情况下,希望能为尽可能少的点添加标记.请问至少要有多少点被加上标记? #include "iostream" #include "algorithm" using namespace std; const int MAX_N = 1000; int N=6

POJ 3069 Saruman&amp;#39;s Army

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