跳到正文

个人记录

2026.8.27课程题目解析+总结

xiangrui0305约 11 分钟
原文

这是 2026年8月27日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2026年8月27日
收录
2026年8月27日
更新
2026年11月21日
篇幅
约 11 分钟

版本

  1. v2当前
  2. v1

题目解析+总结

CF1974G Money Buys Less Happiness Now

题目解析

反悔贪心就是尽可能保证下一次比上一次更优,优先满足答案,要是满足答案就通过撤回使得答案更优。
现在来到这一题,要是能买的话就一定买,这样收获就会增加。要是买不起的话就撤回之前最贵的一个。所以我们就使用大根堆。
如果之前购买的是最贵的,而且大于或等于现在的售价,那么价值不变。
我们就这样执行一遍,最后堆的大小就是本题答案。

AC代码

cmp
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
#define pait pair<int,int>
const int N = 2e5+7;
const int Mod = 998244353;
int m,x;
void solve(){
	cin>>m>>x; int sum = 0;
	priority_queue<int>op;
	for(int i=1;i<=m;++i){
		int a; cin>>a;
		op.push(a),sum -= a;
		if(sum < 0){
			sum += op.top();
			op.pop();
		}
		sum += x;
	}
	cout<<op.size()<<endl;
}
signed main(){
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	int t; cin>>t;
	while(t--) solve();
	return 0;
}

P3545 [POI 2012] HUR-Warehouse Store

题目解析

因为答案不唯一,所以我水过去了。
我们发现题目上可以无视掉那名顾客,所以这道题实际上和之前的一样,只不过是先进货再进行进行售卖,而且还要输出满足的是哪一天。
为了尽可能的满足顾客的需求,我们每天就将天数的下标入队,要是遇到了存货不足的情况,可以撤回上一个消耗大的顾客,而满足这一个顾客。
最后我们的答案就是堆的长度和里面的内容。

AC代码

cmp
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
#define pait pair<int,int>
const int N = 3e5+7;
const int Mod = 998244353;
int n,a[N],b[N];
struct node{
	int x;
	bool operator<(const node &rfs)const{
		return b[x] < b[rfs.x]; 
	}
};
signed main(){
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cin>>n;
	for(int i=1;i<=n;++i) cin>>a[i];
	for(int i=1;i<=n;++i) cin>>b[i];
	int sum = 0;
	priority_queue<node>op;
	for(int i=1;i<=n;++i){
		sum += a[i];
		op.push({i}),sum -= b[i];
		if(sum < 0){
			sum += b[op.top().x];
			op.pop();
		}
	}
	cout<<op.size()<<endl;
	while(!op.empty()){
		cout<<op.top().x<<' ';
		op.pop();
	}
	return 0;
}

P2107 小 Z 的 AK 计划

题目解析

这道题和之前的类型不一样,所以准备出发去第ii个点,只要能AK就一定要AK,要是不能的话就撤销之前AK时间最长的一部分,保证能到达下一个点。要是此时还是到达不了,那么就可以直接结束了。
所以我们先对坐标点排序后,使用While循环保证可以到达下一个点,要是已经超过了,就只能结束,因为后面的都会超时。
这里我们不输出堆得长度的原因是,堆得长度指的是到这一个坐标点时可以AK的数量,而我们要求最大化,所以我们需要打一个擂台。所以我们使用cur记录当前的数量(为什么不直接和堆得长度打擂台),然后输出每个点的cur的最大值,就OK了。

AC代码

cmp
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
#define pait pair<int,int>
const int N = 1e5+7;
const int Mod = 998244353;
int n,m;
struct node{
	int x,t;
}a[N];
bool cmp(node a,node b){
	if(a.x == b.x) return a.t < b.t;
	return a.x < b.x;
}
signed main(){
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n;++i) cin>>a[i].x>>a[i].t;
	sort(a+1,a+1+n,cmp); 
	int sum = 0,cur = 0,ans = 0;
	priority_queue<int>op;
	for(int i=1;i<=n;++i){
		sum += a[i].x - a[i-1].x + a[i].t;
		op.push(a[i].t),cur++;
		while(!op.empty() && sum > m){
			sum -= op.top();
			op.pop(),cur--;
		}
		if(sum > m) break; 
		ans = max(ans,cur);
	}
	cout<<ans<<endl;
	return 0;
}

P2949 [USACO09OPEN] Work Scheduling G

题目解析

AC代码

cmp
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
#define pait pair<int,int>
const int N = 1e6+7;
const int Mod = 998244353;
int n,m,vis[N];
struct node{
	int x,t;
	bool operator<(const node &rfs) const{
		return t > rfs.t; 
	}
}a[N];
bool cmp(node a,node b){
	return a.x < b.x;
}
signed main(){
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cin>>n;
	for(int i=1;i<=n;++i) cin>>a[i].x>>a[i].t;
	sort(a+1,a+1+n,cmp); 
	int sum = 0,ans = 0,cur = 0;
	priority_queue<node>op;
	for(int i=1;i<=n;++i){
		if(a[i].x > op.size()) ans += a[i].t,op.push(a[i]);
		else if(a[i].t > op.top().t){
			ans -= op.top().t;
			op.pop(),op.push(a[i]);
			ans += a[i].t;
		}
	}
	cout<<ans<<endl;
	return 0;
}

P4053 [JSOI2007] 建筑抢修

题目解析

AC代码

cmp
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
#define pait pair<int,int>
const int N = 1e6+7;
const int Mod = 998244353;
int n,m,vis[N];
struct node{
	int x,t;
	bool operator<(const node &rfs) const{
		return x < rfs.x; 
	}
}a[N];
bool cmp(node a,node b){
	return a.t < b.t;
}
signed main(){
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cin>>n;
	for(int i=1;i<=n;++i) cin>>a[i].x>>a[i].t;
	sort(a+1,a+1+n,cmp); 
	int sum = 0,ans = 0,cur = 0;
	priority_queue<node>op;
	for(int i=1;i<=n;++i){
		if(a[i].t >= ans + a[i].x) ans += a[i].x,op.push(a[i]);
		else if(a[i].x < op.top().x){
			ans -= op.top().x;
			op.pop(),op.push(a[i]);
			ans += a[i].x;
		}
	}
	cout<<op.size()<<endl;
	return 0;
}

P4823 [TJOI2013] 拯救小矮人

题目解析

AC代码

cmp
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
#define pait pair<int,int>
const int N = 1e5+7;
const int Mod = 998244353;
int n,h;
struct node{
	int a,b;
}a[N];
bool cmp(node a,node b){
	return a.a + a.b < b.a + b.b;
}
priority_queue<int>op;
signed main(){
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cin>>n;
	int sum = 0,ans = 0;
	for(int i=1;i<=n;++i){
		cin>>a[i].a>>a[i].b;
		sum += a[i].a;
	}
	cin>>h; sort(a+1,a+1+n,cmp);
	for(int i=1;i<=n;++i){
		if(sum + a[i].b >= h){
			sum -= a[i].a;
			op.push(a[i].a),ans++;
		}
		else if(!op.empty() && a[i].a < op.top()){
			sum += op.top() - a[i].a;
			op.pop(),op.push(a[i].a); 
		}
	}
	cout<<ans<<endl;
	return 0;
}