ACWing 贪心题目总结
文章目录
区间问题
905. 区间选点
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
// 先判断有多少区间没有重合:具体现将若干个区间以区间右端点排序,
// 如果下一个区间的左端点在上一个右端点之前,则有重合,区间右端点不更新;
// 否则更新右端点为新区间的右端点,同时将未重合的区间数加一
const int N = 1e5 + 10;
struct Range{
int l, r; // l, r 为区间左右端点
bool operator < (const Range &w)const
{
return r < w.r;
}
}Range[N];
int main()
{
int n;
cin >> n;
for(int i = 0; i < n; i++)
{
int l, r;
cin >> l >> r;
Range[i] = {l, r};
}
sort(Range, Range + n);
int res = 1;// 没有重合的区间数
int r = Range[0].r;
for (int i = 1; i < n; i ++ )
{
if(Range[i].l > r)
{
res ++;
r = Range[i].r;
}
}
cout << res;
return 0;
}
908. 最大不相交区间数量
-
和905. 区间选点思路一致
-
#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 r < w.r; } }Range[N]; int main() { int n; cin >> n; for (int i = 0; i < n; i ++ ){ int l, r; cin >> l >> r; Range[i] = {l, r}; } sort(Range, Range + n); int res = 1; // 不相交区间数量 int r = Range[0].r; for (int i = 1; i < n; i ++ ){ if(Range[i].l > r) { res++; r = Range[i].r; } } cout << res; return 0; }
906. 区间分组
#include <iostream>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
const int N = 1e5 + 10;
typedef pair<int, int> PII;
vector<PII> ans;
int main()
{
int n;
cin >> n;
for (int i = 0; i < n; i ++ )
{
int l, r;
cin >> l >> r;
ans.push_back({l, r});
}
sort(ans.begin(), ans.end()); // 将区间按照左端点从小到大排列
priority_queue<int, vector<int>, greater<int>> q; // 建立小根堆,存储从小到大区间的右端点即为每组的最右端点,q的大小即为组数
q.push(ans[0].second);
for (int i = 1; i < n; i ++ )
{
int l = ans[i].first, r = ans[i].second, t = q.top();
// 只需要与小根堆的堆顶数相比就行,因为当区间是经过从小到大排列过的
if(l <= t) // 如果遍历到的区间的左端点小于等于对比组的最右端点,则将需要创建一个新组
{
q.push(r);
}
else // 如果遍历到的区间的左端点大于对比组的最右端点,则证明,遍历区间属于对比组,更新对比组的最右端点
{
q.pop();
q.push(r);
}
}
cout << q.size();
return 0;
}
907. 区间覆盖
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int N = 1e5 + 10;
typedef pair<int,int> PII;
vector<PII> ans;
int main()
{
int start, end;
cin >> start >> end;
int n;
cin >> n;
for (int i = 0; i < n; i ++ )
{
int l, r;
cin >> l >> r;
ans.push_back({l, r});
}
// 将区间按照左端点从小到大排列
sort(ans.begin(), ans.end());
int start_ = start; // start_暂时保存start的值,等出现左端点大于start点的值,将start_的值赋值给start
int res = 0; // 最小重叠区间
for(int i = 0; i < n;)
{
int prei = i;
// 查找左端点小于start点,同时其右端点最大的区间,并将start点付志伟其右端点
while(ans[i].first <= start)
{
start_ = max(start_, ans[i].second);
i++;
}
if(prei == i) break; // 不存在左端点比start还小的的区间,证明区间断裂
start = start_;
res++;
if(start >= end)
break;
}
if(start >= end) cout << res; // start>=end说明,区间完全覆盖
else cout << -1;
return 0;
}
哈夫曼树
148. 合并果子
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 1e4 + 10;
// 本题用到哈夫曼编码,使用小根堆来存储每一堆果子的数量
int main()
{
int n;
cin >> n;
priority_queue<int, vector<int>, greater<int>> q; // 建立小根堆
for(int i = 0; i < n; i++)
{
int x;
cin >> x;
q.push(x);
}
int res = 0; //res为耗费的体力值
while(q.size() > 1) // 堆中最少存在两堆果子
{
int a = q.top();
q.pop();
int b = q.top();
q.pop();
res += (a + b); // 取出小根堆中最小的两个数进行相加求出耗费的体力值,加入res,并将a+b的值压入堆中
q.push((a + b));
}
cout << res;
return 0;
}
排序不等式
913. 排队打水
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1e5 + 10;
typedef long long LL;
// 先让排队时间最少的人打水
int q[N];
int main()
{
int n;
cin >> n;
for(int i = 0; i < n; i++)
cin >> q[i];
sort(q, q + n);
LL res = 0;
for(int i = 0; i < n; i++)
res += q[i] * (n - 1 - i);
cout << res;
return 0;
}
绝对值不等式
104. 货仓选址
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1e5 + 10;
int w[N];
// 将仓库建立在中间两个地点的中间就行
int main()
{
int n;
cin >> n;
for(int i = 0; i < n; i++)
cin >> w[i];
sort(w, w + n);
int res = 0;
for (int i = 0; i < n - 1 - i; i ++ )
{
res += w[n-1-i] - w[i];
}
cout << res;
return 0;
}
推公式
125. 耍杂技的牛
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int N = 5e4 + 10;
typedef pair<int, int> PII;
vector<PII> res;
//经过推导,令下标1~i+1的牛依次从上到下排列,i+1牛位于i牛下层:i+1牛风险值w1+...+wi-s(i+1),i牛风险值w1+...w(i-1)-si;
//交换i牛和i+1牛的位置:i+1牛风险值:w1+...+w(i-1)-s(i+1),i牛风险值:w1+...+w(i-1)+w(i+1)-si;
//求得这两种情况下最大风险值为w1+...+wi-s(i+1)或者w1+...+w(i-1)+w(i+1)-si
//可以求得如果wi+si>w(i+1)+s(i+1)的时候i+1牛在i牛下层时风险值最大值比i牛在i+1牛下更小
// 所以将w+s按升序排列即可确保最大值最小,然后依次求出每个牛的风险值,相比求出最大
int main()
{
int n;
cin >> n;
for(int i = 0; i < n; i++)
{
int w, s;
cin >> w >> s;
res.push_back({w + s, w});
}
sort(res.begin(), res.end()); // 这种情况下,最大值最小
int sum = 0;
int ans = -1e9;
for(int i = 0; i < n; i++)
{
int w = res[i].second, s = res[i].first - w;
ans = max(ans, sum - s); // 求出风险最大值
sum += w;
}
cout << ans;
return 0;
}