Greedy:Saruman's Army(POJ 3069)
2015-09-06

问题大意:萨鲁曼白想要让他的军队从sengard到Helm’s Deep,为了跟踪他的军队,他在军队中放置了魔法石(军队是一条线),魔法石可以看到前后距离为R的距离,为了让魔法石发挥最大的效益,魔法石戴在军人的身上,问你怎么才能使用最少的石头
问题很清晰,思路也很清晰,这道题挺典型的,就是贪心算法,
很容易想到的就是我们只用在距离内找到最后的点(人),然后把魔法石放到其上面就行了,然后依次类推,最后就可以达到最少的数量
具体可以以2R为一次循环,在前R的区间内我们找到要标记的点,然后移动R的距离,再来一次就可以了。很容易想到,画个图就可以了

#include <stdio.h>
#include <stdlib.h>
#define SWAP(a,b) { (*a)^=(*b);(*b)^=(*a);(*a)^=(*b);}
#define CUTOFF 20 typedef int Position; void Quick_Sort(Position *, Position, Position);
int Get_Pivot(Position, Position, Position,Position *);
void Insertion_Sort(Position *, Position, Position);
void Search(Position *, const int, const int); int main(void)
{
int R, T, i;
Position *Troops = NULL;
while (~scanf("%d%d", &R, &T)
&& R >=
&& T >= )
{
Troops = (Position *)malloc(sizeof(int)*T);
for (i = ; i < T; i++)
scanf("%d", &Troops[i]);
Quick_Sort(Troops, , T - );
Search(Troops, R, T);
free(Troops);
}
} int Get_Pivot(Position left, Position right, Position mid,Position *A)
{
if (A[left] > A[mid]) SWAP(&A[left], &A[mid]);
if (A[left] > A[right]) SWAP(&A[left], &A[right]);
if (A[mid] > A[right]) SWAP(&A[mid], &A[right]); SWAP(&A[mid], &A[right]);//隐藏枢纽元
return A[right];
} void Quick_Sort(Position *A, Position left, Position right)
{
Position i = left, j = right , mid = (left + right) / ;
int pivot;
if (right - left > CUTOFF)
{
pivot = Get_Pivot(left, right, mid, A);
while ()
{
while (A[--j] > pivot);
while (A[++i] < pivot);
if (i < j)
SWAP(&A[i], &A[j])
else break;
}
SWAP(&A[i], &A[right]);//重新显示枢纽元
Quick_Sort(A, left, i - );
Quick_Sort(A, i + , right);
}
else Insertion_Sort(A, left, right);//转插入排序
} void Insertion_Sort(Position *A, Position left, Position right)
{
Position i, j;
int tmp;
for (i = left; i <= right; i++)
{
tmp = A[i];
for (j = i; j > left && A[j - ] > tmp; j--)
A[j] = A[j - ];
A[j] = tmp;
}
} void Search(Position *Troops, const int R, const int T)
{
Position i = , mark, s, ans = ;
for (; i < T;)
{
s = Troops[i++];//获得区间的最左点
for (; i < T && Troops[i] <= s + R; i++);
mark = Troops[i - ];//在R内的最右点,标记
for (; i < T && Troops[i] <= mark + R; i++);
ans++;
}
printf("%d\n", ans);
}
Greedy:Saruman's Army(POJ 3069)的更多相关文章
- POJ 3069 Saruman's Army(萨鲁曼军)
POJ 3069 Saruman's Army(萨鲁曼军) Time Limit: 1000MS Memory Limit: 65536K [Description] [题目描述] Saruman ...
- POJ 3069 Saruman's Army (模拟)
题目连接 Description Saruman the White must lead his army along a straight path from Isengard to Helm's ...
- poj 3069 Saruman's Army(贪心)
Saruman's Army Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Tot ...
- Saruman's Army(贪心)
Saruman the White must lead his army along a straight path from Isengard to Helm’s Deep. To keep tra ...
- 01背包问题:Charm Bracelet (POJ 3624)(外加一个常数的优化)
Charm Bracelet POJ 3624 就是一道典型的01背包问题: #include<iostream> #include<stdio.h> #include& ...
- Scout YYF I(POJ 3744)
Scout YYF I Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5565 Accepted: 1553 Descr ...
- 广大暑假训练1(poj 2488) A Knight's Journey 解题报告
题目链接:http://vjudge.net/contest/view.action?cid=51369#problem/A (A - Children of the Candy Corn) ht ...
- Games:取石子游戏(POJ 1067)
取石子游戏 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 37662 Accepted: 12594 Descripti ...
- BFS 或 同余模定理(poj 1426)
题目:Find The Multiple 题意:求给出的数的倍数,该倍数是只由 1与 0构成的10进制数. 思路:nonzero multiple 非零倍数 啊. 英语弱到爆炸,理解不了题意... ...
随机推荐
- 【ZOJ 1221】Risk
题 题意 给你20个城市的相邻关系,求给定任意两个城市的最短距离 分析 求任意两个城市最短距离,就是用floyd算法,我脑残忘记了k是写在最外层的. 代码 #include<stdio.h> ...
- 学习笔记--(平衡树)splay
坑爹的splay,毁我青春,耗我钱财,颓我精力 是一种用于保存有序集合的简单高效的数据结构.伸展树实质上是一个二叉查找树.允许查找,插入,删除,删除最小,删除最大,分割,合并等许多操作,这些操作的时间 ...
- BZOJ2818 欧拉函数
题意:求1--n中满足gcd(x,y)的值为质数的数对(x,y)的数目 ( (x,y)和(y,x)算两个 ) sol: 设p[i]是一个质数,那么以下两个命题是等价的: 1.gcd(x,y)=1 2. ...
- CODEVS1995 || TYVJ1863 黑魔法师之门
P1863 [Poetize I]黑魔法师之门 时间: 1000ms / 空间: 131072KiB / Java类名: Main 背景 经过了16个工作日的紧张忙碌,未来的人类终于收集到了足够的能源 ...
- groovy-真值
Boolean expressions Groovy支持标准的条件运算符的布尔表达式: 1 def a = true 2 def b = true 3 def c = false 4 assert a ...
- Android 系统属性SystemProperty分析
http://www.cnblogs.com/bastard/archive/2012/10/11/2720314.html Android System Property 一 System Pro ...
- 人工神经网络(ANN)
参考资料:http://www.cnblogs.com/subconscious/p/5058741.html 从函数上来看,神经网络是回归方程的级联叠加,用来逼近目标函数的,本质是一种模拟特征与目标 ...
- linux 优化&安全运维&黑客攻防
优化: 可删除用户:adm,lp,sync,shutdown,halt,news,uucp,operator,games,gopher. :userdel games 可删除组:adm,lp,ne ...
- String与Date、Timestamp互转
一.String与Date(java.util.Date)互转 1.1 String -> Date String dateStr = "2010/05/04 12:34:23&quo ...
- 面向对象分析设计-------02UML+UML各种图形及作用
一.UML是什么?UML有什么用? 二.UML的历史 三.UML的上层结构(Superstructure) 四.UML建模工具 五.UML的图(重点) 1.用例图(use case diagram) ...