跳到正文

个人记录

2026.8.26课程题目解析+总结

xiangrui0305约 15 分钟
原文
文章信息与版本

文章信息

发布
2026年8月26日
收录
2026年8月26日
更新
2026年11月14日
篇幅
约 15 分钟

版本

  1. v1当前

题目解析+总结

P14361 [CSP-S 2025] 社团招新

题目解析

这道题是有三个部门,需要将多个人进行分配使得满意度最大化,那么我们就直接将他们满意度最大的部门给分过去。
但是题目又加了一个约束条件:每一个部门的人数不能超过总人数的一半。虽然如此,但是我们可以得出一个结论:不管怎么分,最多只有一个部门的人数超过了总人数的一半,所以我们只要把他分到次大值就可以。
我们先用桶记录一下每个部门按照没有约束条件的分法进行分配,随后看看哪一个部门的人数超过了这个值,就直接将他标记起来。标记完后,就可以使用优先队列来处理需要转移的人。
我们最好情况下只要转移最大值和次大值的差值最小的,所以我们先将那个部门全部加入进去,接着就移出一部分人,直到剩余的没有超过总人数的一半,这样就可以了。

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,tong[N];
struct node{
	int x,y,z,op1;
}a[N];
priority_queue<int,vector<int>,greater<int>>op; 
void solve(){
	cin>>n; int sum = 0;
	memset(tong,0,sizeof tong);
	for(int i=1;i<=n;++i){
		cin>>a[i].x>>a[i].y>>a[i].z;
		if(a[i].x >= a[i].y && a[i].x >= a[i].z){
			a[i].op1 = 1;
			tong[1]++;
			sum += a[i].x;
		}
		else if(a[i].y >= a[i].x && a[i].y >= a[i].z){
			a[i].op1 = 2;
			tong[2]++;
			sum += a[i].y;
		}
		else if(a[i].z >= a[i].y && a[i].z >= a[i].x){
			a[i].op1 = 3;
			tong[3]++;
			sum += a[i].z;
		}
	}
	int id = -1;
	for(int i=1;i<=3;++i){
		if(tong[i] > n / 2) id = i;
	}
	if(id == -1){
		cout<<sum<<endl;
		return ;
	}
	for(int i=1;i<=n;++i){
		int maxx = max(a[i].x,max(a[i].y,a[i].z));
		int minn = min(a[i].x,min(a[i].y,a[i].z));
		int r = (a[i].x + a[i].y + a[i].z) - maxx - minn;
		if(a[i].op1 == id) op.push(maxx - r);
	} 
	while(op.size() > n/2){
//		cout<<op.top()<<endl;
		sum -= op.top(),op.pop();
	}
	cout<<sum<<endl;
	while(!op.empty()) op.pop();
}
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;
}

P8769 [蓝桥杯 2021 国 C] 巧克力

题目解析

我们可以先优先价格,然后去买比较便宜的种类,使得价格最小化,但是随着时间推移,可能有些巧克力过期了,导致没能达成条件。
但是我们也可以先优先保质期,可能没能达到最小化的价格。
那要怎么办呢?不如我们使用常见贪心处理:“时光倒流”法,从最后一天开始。
因为我们发现时间只有10510^5大,所以我们可以直接枚举时间线,每次过时间线就将到达保质期的巧克力加进这个优先队列。要是这时队列空了,说明无解,否则的话就买下队列中最便宜的巧克力。而要是还有的话,就再次加入队列。这样不仅满足了题目条件,还使得价格最小化。

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 tian,n;
struct node{
	int x,y,z;
	bool operator<(const node &thi)const{
		if(x == thi.x) return y < thi.y;
		return x > thi.x;
	}
}a[N];
bool cmp(node a,node b){
	if(a.x == b.x) return a.x < b.x;
	return a.y > b.y;
}
priority_queue<node>op;
signed main(){
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cin>>tian>>n;
	for(int i=1;i<=n;++i){
		cin>>a[i].x>>a[i].y>>a[i].z;
	}
	sort(a+1,a+1+n,cmp); 
	int idx = 1,ans = 0;
	for(int i=tian;i>=1;--i){
		while(i <= a[idx].y && idx <= n){
			op.push(a[idx]),idx++;
		}
		if(op.empty()){
			cout<<-1<<endl;
			return 0;
		}
		node top = op.top(); op.pop();
		ans += top.x,top.z--;
		if(top.z != 0) op.push(top);
	}
	cout<<ans<<endl;
	return 0;
}

CF1945F Kirill and Mushrooms

题目解析

这道题我们仍然可以像上一题思考,我们任然可以倒退。
要是按排列的顺序采蘑菇,那么前面得蘑菇就没有了魔力值,只有最后采的那个蘑菇,以及所有没出现的蘑菇魔力还是原来的值。
所以我们如果采了xx,那么可用的非零蘑菇就是所有不在该排列里的蘑菇,一共n−pos+1n-pos+1个。
我们先倒着把元素加进去,要是堆里的元素超过了ii,就把最小的扔掉。此时,要是堆的大小刚好等于ii,说明是有至少ii个可用蘑菇,我们就可以进行打擂台了。
就这样重复循环,此时打擂台出的值就是最终答案。
注意因为有多组数据,所以要记得清空。

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 n,v[N],p[N];
priority_queue<int,vector<int>,greater<int>>op;
void solve(){
	cin>>n;
	for(int i=1;i<=n;++i) cin>>v[i];
	for(int i=1;i<=n;++i) cin>>p[i];
	int ans = 0,cnt = 0;
	for(int i=n;i>=1;--i){
		op.push(v[p[i]]);
		while(op.size() > i) op.pop();
		if(op.size() == i && op.top() * i >= ans)
			ans = op.top() * i,cnt = i;
	} 
	while(!op.empty()) op.pop();
	cout<<ans<<' '<<cnt<<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;
}

CF1718B Fibonacci Strings

题目解析

这是一道关于斐波那契字符串性质的组合数学问题。要利用字母以及性质将这个序列拼出来。
我们可以使用小的进行补充,不然大的就没法继续了。随后大的部分假设现在fif_i不填出现次数多的。
fi≥fi−1+fi−3+fi−5+...f_i \ge f_{i-1} + f_{i-3} + f_{i-5} + ...
所以我们先预处理序列和前缀和,接着从大的开始填,向前几题一样从后往前枚举,优先选出现次数多的就可以了。

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,v[N],f[N],sum[N];
int pos = 0,cnt = 0;
struct node{
	int val,id;
	bool operator<(const node &rfs)const{
		return val < rfs.val;
	}
};priority_queue<node>op;
void init(){
	f[1] = f[2] = 1;
	for(int i=3;i<=50;++i) f[i] = f[i-1] + f[i-2];
	for(int i=1;i<=45;++i) sum[i] = sum[i-1] + f[i];
}
string check(){
	while(!op.empty()) op.pop();
	for(int i=1;i<=n;++i) op.push({v[i],i});
	int last = -1;
	while(!op.empty()){
		node top = op.top(); op.pop();
		if(top.id == last){
			if(op.empty()) return "NO\n";
			node tap2 = op.top(); op.pop();
			op.push(top),top = tap2;
		}
		if(top.val >= f[pos]){
			top.val -= f[pos];
			pos -= 1,last = top.id;
		}
		else return "NO\n";
		if(top.val != 0) op.push(top);
	}
	return "YES\n";
}
void solve(){
	cin>>n; cnt = 0;
	for(int i=1;i<=n;++i){
		cin>>v[i]; cnt += v[i];
	}
	bool flag = 0;
	for(int i=1;i<=45;++i){
		if(cnt == sum[i]){
			flag = 1,pos = i;
			break;
		}
	}
	if(!flag) cout<<"NO\n";
	else cout<<check();
}
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; init();
	while(t--) solve();
	return 0;
} 

homework~

CF1493C K-beautiful Strings

AC代码

cmp
#include <bits/stdc++.h>
using namespace std;
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t; cin >> t;
    while(t--){
        int n, k;
        string s;
        cin >> n >> k >> s;
        if (n % k != 0)
        {
            cout << "-1\n";
            continue;
        }
        vector<int> cnt(26, 0);
        for (char ch : s)
            cnt[ch - 'a']++;
        bool ok = true;
        for (int i = 0; i < 26; i++)
        {
            if (cnt[i] % k != 0)
            {
                ok = false;
                break;
            }
        }
        if (ok)
        {
            cout << s << '\n';
            continue;
        }
        string ans;
        bool found = false;
        for (int pos = n - 1; pos >= 0; pos--)
        {
            int cur = s[pos] - 'a';
            cnt[cur]--;
            for (int nxt = cur + 1; nxt < 26; nxt++)
            {
                cnt[nxt]++;
                vector<int> need(26, 0);
                long long sum_need = 0;
                for (int i = 0; i < 26; i++)
                {
                    int rem = cnt[i] % k;
                    if (rem != 0)
                        need[i] = k - rem;
                    else
                        need[i] = 0;
                    sum_need += need[i];
                }
                int rest = n - pos - 1;
                if (sum_need <= rest && (rest - sum_need) % k == 0)
                {
                    ans = s.substr(0, pos);
                    ans += (char)('a' + nxt);
                    for (int i = 0; i < 26; i++)
                    {
                        ans += string(need[i], 'a' + i);
                    }
                    int left = rest - sum_need;
                    ans += string(left, 'a');
                    found = true;
                    break;
                }
                cnt[nxt]--;
            }
            if (found) break;
        }
        if (found) cout << ans << '\n';
        else cout << "-1\n";
    }
    return 0;
}
原文