贪心算法的三个经典例题

A- Saruman’s Army

题目描述: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.

代码示例:

#include <iostream>
#include <algorithm>using namespace std;int solve(int a[], int n,int r)
{int ans = 0,i = 0;sort(a,a+n);       // 将数组升序while(i < n){int s = a[i++];     // 一直向右前进直到距s的距离大于r的距离的点while(i < n&&a[i] <= s + r) i++;int k = a[i-1];     // 一直向右前进直到距k的距离大于r的距离的点while(i < n&&a[i] <= k + r) i++;ans++;}return ans;
}
int main(void)
{int n,m;while(cin>> n >> m,~n&&~m){int a[m];for(int i = 0; i != m; ++i){cin >> a[i];}cout << solve(a,m,n) << endl;}return 0;
}

B- Fence Repair
题目描述:



代码示例:

#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
#include<iterator>
#include<iterator>
#include<cstdio>
#include<queue>
using namespace std;
int main()
{#ifdef ONLINE_JUDGE
#else  freopen("D:\\in.txt", "r", stdin);freopen("D:\\out.txt", "w", stdout);
#endif // ONLINE_JUDEGpriority_queue<int,vector<int>,greater<int> > pq;int n(0);cin >> n;//忽略第一行while (cin >> n){pq.push(n);}long long sum(0);int l1(0), l2(0);while (pq.size() > 1){l1= pq.top();pq.pop();l2= pq.top();pq.pop();sum = sum+l1 + l2;pq.push(l1 + l2);}cout << sum;return 0;
}

C–Safe Or Unsafe
题目描述:


代码如下:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
//char str[10010];
//int num[30];
int cmp(int a,int b)
{return a>b;
}
int main()
{char str[10010];int num[30];int i,j;int n,m;int sum;scanf("%d",&n);while(n--){memset(num,0,sizeof(num));scanf("%d",&m);scanf("%s",str);for(i=0;str[i]!='\0';i++){num[str[i]-'a']++;}sort(num,num+26,cmp);for(i=0;i<26;i++){if(num[i]==0){break;}}sum=0;if(i==1)    // 单一字符{sum=num[0];}else{for(j=i-1;j>=1;j--){sort(num,num+j+1,cmp); // 每次都排序取最小 sum+=num[j]+num[j-1];num[j-1]+=num[j];}}//printf("%d\n",sum);if(sum<=m){printf("yes\n");}else{printf("no\n");}}return 0;
}

贪心算法三个经典例题相关推荐

  1. 总结 贪心算法_用经典例题轻松帮你搞定贪心算法

    转自:奶糖猫 贪心算法概念叙述 运用贪心算法求解问题时,会将问题分为若干个子问题,可以将其想象成俄罗斯套娃,利用贪心的原则从内向外依次求出当前子问题的最优解,也就是该算法不会直接从整体考虑问题,而是想 ...

  2. 【算法基础】经典例题说递归

    目录 [算法基础]经典例题说递归 递归简介 递归的适用范围 递归的基本思路 经典例题解析 移梵塔 题目描述 题目分析 题解 九连环 题目描述 题目分析 题解 更新日志 [算法基础]经典例题说递归 递归 ...

  3. python贪心算法几个经典例子_关于贪心算法的一些探讨、经典问题的解决和三种典型的贪心算法算法(哈弗曼,Kruskal,Prim)的Python实现。...

    贪心算法(又称贪婪算法)是指,在对问题求解时,总是做出在当前看来是最好的选择.也就是说,不从整体最优上加以考虑,他所做出的是在某种意义上的局部最优解. 贪心算法不是对所有问题都能得到整体最优解,关键是 ...

  4. java贪心算法几个经典例子_经典算法思想5——贪心(greedy algorithm)

    贪心算法,是指在对问题求解时,总是做出再当前看来是最好的选择.也就是说,不从整体最优上加以考虑,他所做出的仅是某种意义上的局部最优解. 贪心算法没有固定算法框架,算法设计的关键是贪心策略的选择.必须注 ...

  5. 古老的密码(Ancient Cipher,UVa1339)(算法竞赛入门经典 例题4-1)C++

    题目:给定两个不超过100的字符串,判断是否可以做到将其中一个字符串通过重排和映射的操作,使得两个字符串相同.例如,JWPUDJSTVP重排后可以得到WJDUPSJPVT,然后每个字母只要通过一次映射 ...

  6. 贪心算法单源点最短路径例题c语言源代码,Dijkstra算法是解单源最短路径问题的一个贪心算法...

    问题描述 给定一个带权有向图 G=(V,E) ,其中每条边的权是一个非负实数. 另外,还给定 V 中的一个项点,称为源. 现在我们要计算从源到所有其他各项点的最短路径长度. 这里的长度是指路上各边权之 ...

  7. 第1部分 基础算法(提高篇)--第1章 贪心算法1425:【例题4】加工生产调度

    1425:[例题4]加工生产调度 时间限制: 1000 ms 内存限制: 65536 KB 提交数: 2047 通过数: 529 [题目描述] 某工厂收到了 n 个产品的订单,这 n 个产品分别在 A ...

  8. Java算法大全_java贪心算法几个经典例子

    Java经典问题算法大全   /*[程序1]   题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?  ...

  9. 贪心算法(三)——最佳合并模式

    问题描述 给定n个有序文件,每个文件的记录数分别为w1~wn,请给出一种两两合并的方案,使得总合并次数最少. 注意: 1. 外排序算法是将多个有序文件合并成一个有序文件的过程. 2. 在一次合并的过程 ...

  10. 算法竞赛入门经典 例题6-6 小球下落(python、C)

    同个人网站 https://www.serendipper-x.cn/,欢迎访问 ! 问题描述: 有一颗二叉树,最大深度为D,且所有叶子的深度都相同.所有结点从上到下从左到右编号为 1,2,3,- , ...

最新文章

  1. 使用Active Directory的常见问题2
  2. 台湾国立大学郭彦甫Matlab教程笔记(8)文件读写
  3. 浅议C#客户端和服务端通信的几种方法:Rest和GRPC和其他
  4. 使用matlab内存不足,Matlab内存不足问题(Out of memory)
  5. substring 在C#,Javascript,SQL 中index开始值
  6. mysql日期纬度表_mysql中生成时间维度表
  7. Mybatis集成日志与ehcache
  8. FastDFS启动报错
  9. 大数据时代亟需消除八大“疑云”
  10. baidu搜索出现错误提示页面
  11. 19、SPI 和 SST25VF016B
  12. MOOC 研究生学术与职业素养 课后答案
  13. [每日一题]128:四则运算(小米OJ)表达式求值
  14. php 支付宝支付怎样开发,PHP实现个人支付宝支付开发(二) - cmpay
  15. 无线网络的暴力破解密码
  16. 什么是url,herf和src的区别
  17. 帝国CMS[!--onclick--]标签动态显示页面点击数,解决刷新页面浏览量无变化的问题
  18. MIT多变量微积分--8.多元函数,等值面,偏导数,切平面逼近
  19. python语句print(type([1、2、3、4))_Python语句print(type([1,2,3,4]))的输出结果是() 。...
  20. 用计算机制作flash动画教案,Flash动画制作教案

热门文章

  1. Linux中文输入法安装
  2. 天翼校园客户端拨号服务器无响应,天翼校园客户端问题总结及解决办法
  3. java 邮件发送 多人_java 发送邮件(可发送多人,抄送多人,可带附件)
  4. FPGA Marvell 88exxxx phy 动起来
  5. 手把手教你学DSP 28335学习笔记
  6. Androd TV开发-前言
  7. 让油猴脚本只执行一次
  8. Python多线程小例子
  9. 神马js都是浮云-----限时秒杀
  10. linux删除用户名命令,linux删除用户命令