
贪心
QingQingDE23
这个作者很懒,什么都没留下…
展开
-
AcWing 125 耍杂技的牛 题解 (贪心—推公式)
在这里插入代码片#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int N = 1e5 + 10;typedef pair<int, int>PII;PII a[N];int n;int main(){ cin>>n; for(int i = 0; i <原创 2021-10-07 11:51:10 · 77 阅读 · 0 评论 -
AcWing 104 货仓选址 题解 (贪心—绝对值不等式)
原题链接:https://ac.nowcoder.com/acm/contest/1001/B解题思路:把A[1]~A[N]排序,假设货仓左边有P家店,右边有Q家店,如果P < Q,当货仓选址向右移动1单位,则总距离会减小Q - P, 如果P > Q,当货仓地址向左移,总距离会减小,所以最优解是P == Q。对A[]进行排序之后,当A[N]为奇数时,货仓x=A[(N+1)/2],当A[N]为偶数时,货仓地址x=A[N/2]—A[(N+1)]之间都可以(这道题难在思考货仓位置和总距离的关系,排原创 2021-08-20 12:53:03 · 159 阅读 · 0 评论 -
AcWing 913 排队打水 题解(贪心—排序不等式)
//思路:每个同学都需要排队等待前面的同学的时间,所以越靠前的同学就会被更多人等待,所以花费时间越小的同学越应该往前排 #include<iostream>#include<algorithm>using namespace std;const int N = 1e5 + 10;typedef long long ll;int n;int t[N];int main(){ cin>>n; for(int i = 0; i < n; i原创 2021-09-27 14:29:39 · 97 阅读 · 0 评论 -
AcWing 148 合并果子 题解 (贪心—Huffman树)
思路:按照树的思想考虑合并,树的最底层进行合并时需要计算的次数最多,所以质量最小的两个果子应该在树的最底层,最终所有果子合并到树的根节点,根节点的值即为消耗的体力和。将所有果子质量存入堆中(实现排序),每次挑出质量最小的两堆果子(堆顶的两个元素)合并,将总体力数加上这两堆果子的质量,将新合并成的果子堆的质量存入堆中(作为新的树的节点),这样就能实现合并消耗的总体力最小#include<iostream>#include<algorithm>#include<queue&g原创 2021-09-26 18:54:10 · 120 阅读 · 0 评论 -
AcWing 907 区间覆盖 题解 (贪心—区间问题)
#include<iostream>#include<cstring>#include<algorithm>using namespace std;const int N = 1e5 + 10;struct Range{//利用结构体储存区间 int l, r; bool operator< (const Range &W) const //按照左端点大小对区间进行排序 { return l < W.l; }}rang原创 2021-09-26 17:00:10 · 87 阅读 · 0 评论 -
AcWing 906 区间分组 题解(贪心—区间问题)
#include<iostream>#include<algorithm>#include<queue>using namespace std;const int N = 1e5;struct Range{//利用结构体储存区间 int l, r; bool operator< (const Range &W) const { return l < W.l; }}range[N];int main(){ int n原创 2021-09-26 00:08:42 · 168 阅读 · 0 评论 -
AcWing 905 区间选点 题解 (贪心—区间问题)
解题思路:选取点的时候,选取适合区间的右端点,能使点的总数量最少,将区间排序,然后遍历,从第一个区间的右端点作为第一个标记值,如果标记大于下一个区间的左端点值,代表这个标记也在新区间内,就不用新增点,如果标记值小于新区间的左端点值,代表新区间不含盖标记,点的数量需要加一,新增的点就是新区间的右端点,将标记值更新为新区间的右端点值,继续向下遍历,直到遍历完所有区间#include<iostream>#include<cstring>#include<algorithm>原创 2021-09-23 12:24:07 · 135 阅读 · 0 评论 -
牛客 803 区间合并 题解(贪心)
原题链接:https://www.acwing.com/problem/content/805/模糊的记得我见过这个题,但清楚地记得自己当时没弄明白,并且没有补题,不过这次算是弄懂了#include<iostream>#include<bits/stdc++.h>using namespace std;typedef pair<int, int>PII;void merge(vector<PII> &segs){ vector<原创 2021-08-26 15:20:20 · 195 阅读 · 1 评论