![](https://img-blog.csdnimg.cn/20201014180756927.png?x-oss-process=image/resize,m_fixed,h_224,w_224)
#贪心算法
贪心
Enki_Liu
这个作者很懒,什么都没留下…
展开
-
AcWing 734. 能量石
//两个相邻的石头i,j//先吃i后吃j获取的能量为Ei+Ej-Si*Lj//先吃j后吃i获取的能量为Ej+Ei-Sj*Li//所以当Li/Si>Lj>Sj是先吃i获取1的能量更多(贪心)#include<bits/stdc++.h>using namespace std;const int N = 110, M =11000;typedef pair<int,int> PII;typedef pair<int,pair<int,int>原创 2021-07-31 15:25:57 · 93 阅读 · 0 评论 -
AcWing 187. 导弹防御系统
#include<bits/stdc++.h>using namespace std;const int N = 55;int a[N],up[N],down[N];int n;int cnt;void dfs(int u,int uu,int dd){ //剪枝,并且只有比当前的cnt小,才能更新,换成n直接tle。。。 if(uu+dd>=cnt)return; //遍历完所有点后,就能求出最小的上升子序列和下降子序列之和 if(u=.原创 2021-06-16 16:30:49 · 52 阅读 · 0 评论 -
AcWing 125. 耍杂技的牛
思路:体重越大的牛,就越要在下面。越强壮的牛,就越要在下面。所以两者之和最大的牛在最下面。随意两头牛w1,s1和w2,s2 假设w1+s1>w2+s21在2上 x1=w1-s2 2在1上 x2=w2-s1 x1-x2=w1+s1-w2-s2>0 所以x1>x2 所以应选择2在1上#include<bits/stdc++.h>using namespace std;typedef long long LL;typedef pair<LL,LL> PLL;原创 2021-05-19 19:08:39 · 41 阅读 · 0 评论 -
AcWing 104. 货仓选址
思路:选在两个边界之外的点(a[r]-a[l])一定比选在边界之内要长(a[r]-a[l]+2*d)选在边界之内任意点离边界长度都相同(a[r]-a[l]) 去掉两个边界依次类推偶数个点 选择最内侧两个点的中间任意位置奇数个点 选择最内侧的点#include<bits/stdc++.h>using namespace std;typedef long long LL;const int N = 100007;int a[N];int n;int main(){原创 2021-05-18 23:58:53 · 39 阅读 · 0 评论 -
AcWing 913. 排队打水
思路:每次选打水时间少的,使得总等待时间最少#include<bits/stdc++.h>using namespace std;typedef long long LL;priority_queue<LL,vector<LL>,greater<LL>> heap;int n;int main(){ scanf("%d",&n); for(int i=0;i<n;i++) { LL a;原创 2021-05-18 22:48:43 · 79 阅读 · 0 评论 -
AcWing 148. 合并果子
思路:每次去除最小的两个数进行合并,并加入集合(Huffman数)#include<bits/stdc++.h>using namespace std;typedef long long LL;const int N = 10007;priority_queue<LL,vector<LL>,greater<LL>> heap;int n;int main(){ scanf("%d",&n); for(int i=0;原创 2021-05-18 22:28:43 · 40 阅读 · 0 评论 -
AcWing 907. 区间覆盖
思路:每次选择左端点小于线段区间的左端点(可以覆盖)并且右端点最大的闭区间(覆盖面积最大)然后用右端点更新线段区间的左端点#include<bits/stdc++.h>using namespace std;typedef pair<int,int> PII;const int N = 1e5+7;PII c[N];int s,t,n,cnt;//按照左端点从小到大排序int cmp(PII a,PII b){ if(a.first!=b.first原创 2021-05-18 01:24:22 · 43 阅读 · 0 评论 -
AcWing 905. 区间选点
#include<bits/stdc++.h>using namespace std;typedef pair<int,int> PII;const int N = 1e5+7;PII c[N];int n,cnt;int cmp(PII a,PII b){ if(a.second!=b.second)return a.second<b.second; else return a.first<b.first;}int main().原创 2021-05-16 00:00:37 · 38 阅读 · 0 评论 -
AcWing 906. 区间分组
#include<bits/stdc++.h>using namespace std;priority_queue<int,vector<int>,greater<int>> heap;typedef pair<int,int> PII;const int N = 1e5+7;PII c[N];int n,cnt;int sec[N];int cmp(PII a,PII b){ if(a.first!=b.first.原创 2021-05-17 22:53:41 · 43 阅读 · 0 评论