![](https://img-blog.csdnimg.cn/20201014180756919.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
贪心算法
花无凋零之时
这个作者很懒,什么都没留下…
展开
-
【POJ】3258 River Hopscotch
该题目采用二分的思想: #include <iostream> #include <cstdio> #include <algorithm> using namespace std; const int N = 50000 + 10; int l, n, m; int dis[N]; int main(){ while(cin >> l >> n >> m){ dis[0] = 0; d.原创 2022-03-12 16:53:40 · 148 阅读 · 0 评论 -
【LeetCode】11. 盛最多水的容器
我们最容易想到的方法就是设置两个指针依次遍历所有情况,然后找到最大的那个,但是这种方法的时间复杂度为O(n2),最后运行的结果会超时,因此需要另一种更好的方法。 思路:一开始两个指针一个指向开头一个指向结尾,此时容器的底是最大的,接下来随着指针向内移动,会造成容器的底变小,在这种情况下想要让容器盛水变多,就只有在容器的高上下功夫。 那我们该如何决策哪个指针移动呢?我们能够发现不管是左指针向右移动一位,还是右指针向左移动一位,容器的底都是一样的,都比原来减少了 1。这种情况下我们想要让指针移动后的容器面积增.原创 2022-03-01 16:08:26 · 127 阅读 · 0 评论 -
【牛客网】KY118 继续畅通工程
这道题可以使用并查集解决,利用Kruskal算法的思想构建最小生成树,其中,我使用优先队列根据修建状态进行排序,优先已建的路: #include<iostream> #include<string> #include<queue> using namespace std; const int N = 100 + 10; int arr[N]; struct Distance{ int n1, n2, dis, state; Distance(in.原创 2022-02-06 17:49:53 · 223 阅读 · 0 评论 -
【牛客网】KY148 还是畅通工程
因为要总长度最小,所以想到贪心,使用Kruskal算法的思想构造最小生成树即可: #include<iostream> #include<string> #include<queue> using namespace std; const int N = 100 + 10; int arr[N]; struct Distance{ int n1, n2; int dis; Distance(int n1, int n2, int d): .原创 2022-02-06 17:51:55 · 202 阅读 · 0 评论 -
【牛客网】KY155 To Fill or Not to Fill
这是一道贪心算法的题目,因为题目要求花费最低,所以我想到区间贪心的思想,那么首先要将各个加油站的费用从小到大排序,然后根据汽车最多能走的距离计算所需要的花费,再将这些区间的花费加起来即可: #include<iostream> #include<algorithm> using namespace std; const int MAXDIS = 30000 + 10; const int MAXN = 500 + 10; struct Station{ double .原创 2022-01-28 18:13:44 · 230 阅读 · 0 评论 -
【牛客网】KY4 代理服务器
这道题需要采用贪心的策略,即每次选择代理服务器的时候,都要选择从当前位置出发一次能够访问尽可能多的地址,同时要考虑到可能会出现无法访问完的情况,即代理服务器只有一个,而其IP地址正好在要访问的地址中,完整代码如下: #include<iostream> #include<string> using namespace std; const int N = 1000 + 10; const int M = 5000 + 10; string proxy[N], go[M]; .原创 2022-01-28 13:54:55 · 331 阅读 · 0 评论 -
【POJ】1328 Radar Installation
这是一道区间贪心的题目,对于这道题,由于岛屿的位置在坐标轴上是确定的,而雷达的位置及数量则无法确定,因此我们可以根据岛屿来确定雷达的位置及数量。首先,我们可以根据岛屿的位置以及雷达的半径来确定覆盖该岛屿的雷达的位置区间,如下图: 我们可以以岛屿为圆心,雷达的半径为半径画圆,则其与x轴有两个交点分别为a、b,雷达在[a, b]区间任意位置都能够覆盖当前的岛屿,因此我们可以将岛屿的位置处理成a、b两个x的坐标: #include<iostream> #include<algorithm&g.原创 2022-01-28 16:41:11 · 276 阅读 · 0 评论