跳到正文

个人记录

MXOI题解记录

Yuantianyou约 23 分钟
原文

这是 2026年5月23日 存档的第 2 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2026年5月10日
收录
2026年5月16日
更新
2026年11月11日
篇幅
约 23 分钟
获赞
1

版本

  1. v3当前
  2. v2
  3. v1

CF1878A How Much Does Daytona Cost?题解

题目描述

有 t 组数据。每组数据给出一个长度为 n 的序列 a 和一个数字 k。

请你判断 a 中是否存在一个非空子段使得 k 在这个子段中出现的次数严格大于其它数字的出现次数。存在输出 YES,否则输出 NO。

分析&解法

题目中说是否存在一个非空子段,那就说明这个子段长度可以为 1 (就他自己),所以我们只需要遍历数组判断他是否存在就可以了。

Code:Code:

Code:
C++
#include<iostream>
using namespace std;
int t,n,k;
int a[150];
int main(){
	cin >> t;
	while(t--){
		cin >> n >> k;
		bool f = 0;
		for(int i = 1;i <= n;i++){
			cin >> a[i];
			if(a[i] == k)
				f = 1;
		}
		if(f) cout << "YES\n";
		else cout << "NO\n";
	}
	return 0;
} 

CF1474A Puzzle From the Future

题目描述

意思就是给你一个 bb 数组,让你构造一个 aa 数组,使的按题目求得的 dd 数组尽可能大。

分析&解法

根据题目可知,构造的 a 数组中只会包含0,1。
我们如果要让数组 d 尽可能大,就要尽可能让数组 a 数位更多且没有前导0,还要让 d 有尽可能多的2或1且前后不重复。
所以我们可以挨个遍历前一个数和当前的数,如果当前的数加一不等于前面的数。我们就给它赋值当前的数加一,否则就等于零。 这样就可以求出 a 数组了。

Code:Code:

Code:
C++
#include<iostream>
#include<string>
using namespace std;
int t,n;
char s1[100005],s2[100005];
int main(){
	cin >> t;
	while(t--){
		cin >> n;
		cin >> s1;
		s2[0] = '1';
		for(int i = 1;i < n;i++){
			int a = s1[i] - '0',b = s2[i-1] - '0',c = s1[i-1] - '0';
			if(a + 1 != b + c) s2[i] = '1';
			else s2[i] = '0';
		}
		cout << s2 << endl;
	}
	return 0;
}

CF1932A Thorns and Coins

这道题看上去是DP,实则是假DP,只需要模拟就行了

题目描述

给你一个行走路线,路上有金币和障碍,你一次可以走一步或两步,问最多能获得多少金币。

分析&解法

只需要模拟即可。

因为我们可以走一格,就相当于在没有障碍的情况下每个格子都能走到,所以只要这一格为金币,我们就可以吃到。

如果这一个为障碍,那么我们就可以判断是否能越到这个障碍,如果这一格为障碍且上一格也为障碍,就一定不能跨越(因为无论走一格还是两格都无法穿过这两个障碍)然后就 breakbreak 就行了。

Code:Code:

Code:
C++
#include<iostream>
using namespace std;
int t,n;
char s[55];
int ans;
int main(){
	cin >> t;
	while(t--){
		cin >> n;
		cin >> s;
		ans = 0;
		for(int i = 0;i < n;i++){
			if(s[i] == '@') ans++;
			if(s[i] == '*' && s[i - 1] == '*') break;
		}
		cout << ans << endl;
	}
	return 0;
}

CF1931C Make Equal Again

题目描述

进行至多一次区间赋值操作,使数组中的所有元素都相等的最小代价。

分析&解法

既然题目要求了至多一次,那么最终整个数组的元素要么是a1a_1,要么就是ana_n。

我们进行两次统计,一次统计以a1a_1为最终元素的代价;一次统计以ana_n为最终元素的代价。

最后取他俩的最小值就可以了。

CodeCode

Code:
C++
#include<iostream>
using namespace std;
int t,n,a[200005];
int main(){
	cin.tie(0),cout.tie(0);
	ios::sync_with_stdio(false);
	cin >> t;
	while(t--){
		cin >> n;
		for(int i = 1;i <= n;i++)
			cin >> a[i];
		int ans1 = 0,ans2 = 0;
		int i = 1,j = n;
		while(a[i] == a[1]) i++;
		while(a[j] == a[1]) j--;
		ans1 = max(0,j - i + 1);
		i = 1,j = n;
		while(a[i] == a[n]) i++;
		while(a[j] == a[n]) j--;
		ans2 = max(0,j - i + 1);
		cout << min(ans1,ans2) << endl;
	}
	return 0;
}

CF1714C Minimum Varied Number

题目描述

找出数码和为 ss 的最小数字,使得其中的所有数字都是不同的。

分析&解法

如果想让一个数尽可能小,那么就要让他数位越少且高位上的数字越小。

那么我们可以让他的个位最大,越往前数越小(9,8,7,6...9,8,7,6...依次递减),这样就可以保证“数位越少且高位上的数字越小”的条件了。

Code:Code:

Code:
C++
#include<iostream>
using namespace std;
int t,s;
int ans[50];
int main(){
	cin >> t;
	while(t--){
		cin >> s;
		int sy = s,i = 1,j = 9;
		while(1){
			if(sy > j) ans[i] = j;
			else{
				ans[i] = sy;
				break;
			}
			sy -= j,i++,j--;
		}
		for(int k = i;k >= 1;k--) cout << ans[k];
		cout << endl;
	}
	return 0;
}

CF1409B Minimum Product

题目描述

选择 aa 或 bb 中的一个,将其减少 11。但操作后 aa 不能小于 xx,bb 不能小于 yy。

你的任务是,通过不超过 nn 次操作,使 aa 和 bb 的乘积尽可能小,并输出这个最小值。

分析&解法

核心:两个数相乘,差越大,他们的积越小;越接近,他们的积越大。

所以我们可以分别让 aa 最小和 bb 最小,再分别取出他们的乘积,取最小值就行了。

Code:Code:

Code:
C++
#include<bits/stdc++.h>
using namespace std;
int t;
int main() {
	cin >> t;
	while (t--) {
		long long a, b, x, y, n;
		cin >> a >> b >> x >> y >> n;
		long long ans = 1e18;
		int i = 2;
		while (i--) {
			int da = min(n, a - x);
			int db = min(n - da, b - y);
			ans = min(ans, (a - da) * 1ll * (b - db));
			swap(a, b);
			swap(x, y);
		}
		cout << ans << endl;
	}
}

CF1515A Phoenix and Gold

题目描述

有 nn 个互不相同的数,请构造一个序列使不存在前缀和为 xx,如果没有请输出NO.

分析&解法

(个人见解)

如何构造这个序列呢?我们可以先给他排个序,在输出过程中如果发现有前缀和为 xx 时,就先输出后一个数,再输出这一个数。

个人证明: (aa 为 wiw_i 之前的前缀和,bb 为 wiw_i,cc 为 wi+1w_{i+1}) 如果 a+b=xa + b = x 因为 b≠cb \ne c 所以 a+c≠xa + c \ne x

Code:Code:

Code:
C++
#include <bits/stdc++.h>
using namespace std;
int t,n,x;
int w[105];
int main(){
    cin >> t;
    while(t--){
        cin >> n >> x;
        for(int i = 1;i <= n;i++) cin >> w[i];
        sort(w + 1,w + n + 1);
        int sum = 0;
        bool f = 0;
        for(int i = 1;i <= n;i++)
            sum += w[i];
        if(sum == x){
            cout << "NO\n";
            continue;
        }  
        sum = 0;
        cout << "YES\n";
        for(int i = 1;i <= n;i++){
            if(sum + w[i] == x){
                printf("%d %d ",w[i+1],w[i]);
                sum += w[i+1]+w[i],i++;
            }else{
                printf("%d ",w[i]);
                sum += w[i];
            }
        }
        cout << endl;
    }
    return 0;
}

CF1501B Napoleon Cake

题目描述

对一个堆进行 nn 次操作,每一次操作前堆都会增加一层。 第 ii 次操作为标记堆顶 aia_i 层。最后输出整个堆的标记情况。

分析&解法

模拟 + 差分 即可

维护一个差分数组,每次将 chai+1−1,chai−ai+1+1cha_{i+1} - 1,cha_{i-a_i+1} + 1,即将 [i−ai+1,i][i-a_i+1,i] 的区间加一。

最后输出这个差分数组,如果 chai>1cha_i > 1,就输出 11 ,否则输出 00 。

Code:Code:

Code:
C++
#include <bits/stdc++.h>
using namespace std;
int t, n;
int a[200005];
int main()
{
    cin >> t;
    while (t--)
    {
        cin >> n;
        for (int i = 1; i <= n; i++)
            cin >> a[i];
        int b[200005] = {0};
        for (int i = 1; i <= n; i++)
        {
            if (a[i] == 0)
                continue;
            b[i + 1] -= 1;
            b[i - a[i] + 1 <= 0 ? 1 : i - a[i] + 1] += 1;
        }
        for (int i = 1; i <= n; i++)
            b[i] = b[i - 1] + b[i];
        for (int i = 1; i <= n; i++)
        {
            if (b[i] >= 1)
                cout << 1 << ' ';
            else
                cout << 0 << ' ';
        }
        cout << endl;
    }
    return 0;
}

CF1554A Cherry

题目描述

给定 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n。请你在所有满足 1≤l<r≤n1 \le l < r \le n 的整数对 (l,r)(l, r) 中,找到 max(al,al+1,…,ar)⋅min(al,al+1,…,ar)max(a_l, a_{l+1}, \ldots, a_r) \cdot min(a_l, a_{l+1}, \ldots, a_r) 的最大值。

分析&解法

仔细分析就会发现,

假设我们有两个相邻的数 a,b (a<b)a,b\,(a\lt b),那么这个序列的解是 a⋅ba\cdot b。现在我们向 bb 后面添加一个数 cc,对于这个序列的解改变有且仅有两种情况:

  • 第一种情况,当 b<cb\lt c 时,解为 a⋅ca\cdot c。
  • 第二种情况,当 a>ca\gt c 时,解为 b⋅cb\cdot c。

对于第一种情况,显然 b⋅c>a⋅cb\cdot c\gt a\cdot c,所以我们将 aa 移出序列可以得到更优解;对于第二种情况,显然解与 aa 无关。

综上,我们只需要考虑序列长度为 22 的情况,对此我们只需要对所有数依次判断更新答案即可。

Code:Code:

Code:
C++
#include <iostream>
using namespace std;
using ll = long long;
int t;
int n, a[1000006];
int main()
{
    cin >> t;
    while (t--)
    {
        cin >> n;
        for (int i = 1; i <= n; ++i)
            cin >> a[i];
        ll ans = -1e18;
        for (int i = 1; i < n; ++i)
            ans = max(ans, 1ll * a[i] * a[i + 1]);
        cout << ans << '\n';
    }
}

P3029 [USACO11NOV] Cow Lineup S

难度: 普及/提高−

分析&解法

先按照坐标从小到大排序,接着维护一个包含所有品种奶牛的单调队列,每次求一下成本取最小值就行了。 具体维护方法如下:

  1. 开始前先求出来一个包含所有品种奶牛的队列
  2. 每次把队尾多余的奶牛删掉
  3. 求成本
  4. 加入新奶牛

Code:Code:

Code:Code:
C++
#include <algorithm>
#include <iostream>
#include <map>
using namespace std;
int n;
struct node{
    int x, id;
} a[500005];
bool cmp(node x1, node x2){
    return x1.x < x2.x;
}
map<int, int> mp;
int cnt = 0, ans = 2e9 + 10;
int main(){
    cin >> n;
    for (int i = 1; i <= n; i++){
        cin >> a[i].x >> a[i].id;
        mp[a[i].id]++;
        if (mp[a[i].id] == 1)
            cnt++;
    }
    sort(a + 1, a + n + 1, cmp);
    mp.clear();
    int dft = 0, l = 1, r;
    for (r = 1; r <= n; ++r){
        mp[a[r].id]++;
        if (mp[a[r].id] == 1)
            dft++;
        if (dft == cnt)
            break;
    }
    ans = min(ans, a[r].x - a[l].x);
    while (r <= n){
        while (mp[a[l].id] - 1 >= 1)
            mp[a[l].id]--, l++;
        ans = min(ans, a[r].x - a[l].x);
        mp[a[++r].id]++;
    }
    cout << ans;
    return 0;
}

AT_abc141_d [ABC141D] Powerful Discount Tickets

难度: 普及−(中位)

分析&解法

首先我们要知道,对一个价格为 XX 的商品使用一张优惠券后的价格是 ⌊X2⌋\left\lfloor \frac{X}{2} \right\rfloor元。

所以我们的贪心策略就是每次对最大的数使用一张优惠卷,使其价格折半。最后把剩下的价格加起来就行了。

因为每次要取最大的数,所以用一个堆(优先队列)来维护。

Code:Code:

Code:Code:
C++
#include<iostream>
#include<queue>
using namespace std;
int m;
int n;
int s[200005];
long long ans;
int main(){
	cin >> n;
	cin >> m;
	priority_queue<int> q;
	for(int i = 1;i <= n;i++){
		cin >> s[i];
		q.push(s[i]);
	}
	for(int i = 1;i <= m;i++){
		int now = q.top();
		q.pop();
		q.push(now / 2);
	}
	for(int i = 1;i <= n;i++){
		ans += q.top();
		q.pop();
	}
	cout << ans;
	return 0;
}

CF1800C2 Powering the Hero (hard version)

难度: 普及−(中位)

分析&解法

可以发现,每个英雄牌所有的力量值都为其前未被使用的最大奖励牌的力量值。

所以我们可以维护一个大根堆(优先队列),存放奖励牌,这样每次就可以取到奖励牌的最大值了。

Code:Code:

Code:Code:
C++
#include<bits/stdc++.h>
using namespace std;
long long T;
void solve() {
	long long ans=0;
	priority_queue<long long> s;
	long long n;
	cin >> n ;
	for(long long i = 1; i <= n; i++) {
		long long x;
		cin>>x;
		if(x == 0 && s.size() > 0) {
			ans += s.top();
			s.pop();
		} else {
			s.push(x);
		}
	}
	cout<<ans<<'\n';
}
int main() {
	cin>>T;
	while(T--)
		solve();
	return 0;
}

CF1353D Constructing the Array

难度: 普及−(上位)

分析&解法

因为每次都会取最长的区间,所以我们可以用一个大根堆(优先队列)维护一个最大的区间。

其中大根堆里要存储区间 [l,r][l,r] 的左右端点,要用到结构体。所以我们又要手写一个优先队列的比较函数(或代码中的cmp结构体)。

重点知识

结构体的优先队列: 实现按 ll 降序排列,当 ll 相同时按 rr 升序排列。

  1. 结构体内部重构,例如
C++
struct node {
	int l, r;
	bool operator < (const node& x) const{
		if(l == x.l) return r > x.r;
		return l < x.l;
	}//这里的‘>’‘<’是优先级
};
priority_queue<node> q;
  1. 结构体外比较函数,例如
C++
struct cmp{
	bool operator () (const node& x,const node& y){
		if(x.l == y.l) return x.r > y.r;
		return x.l < y.l; 
	}//这里的‘>’‘<’是优先级
};
priority_queue<node,vector<node>,cmp> q;

注意两者的使用方式!!!

Code:Code:

Code:Code:
C++
#include <queue>
#include <iostream>
using namespace std;
int t, n;
struct node {
	int l, r;
};
struct cmp{
	bool operator () (const node& x,const node& y){
		if(x.r - x.l + 1 == y.r - y.l + 1) return x.l > y.l;
		return x.r - x.l + 1 < y.r - y.l + 1;
	}
};
int main() {
	cin >> t;
	while (t--) {
		cin >> n;
		priority_queue<node,vector<node>,cmp> q;
		q.push({1, n});
		int a[200005] = {}, i = 1;
		while (!q.empty()) {
			node u = q.top();
			q.pop();
			int mid;
			if ((u.r - u.l + 1) % 2 == 0)
				mid = (u.l + u.r - 1) / 2;
			else
				mid = (u.l + u.r) / 2;
			a[mid] = i++;
			if (u.l <= mid - 1)
				q.push({u.l, mid - 1});
			if (mid + 1 <= u.r)
				q.push({mid + 1, u.r});
		}
		for (int i = 1; i <= n; i++)
			cout << a[i] << ' ';
		cout << '\n';
	}
}

CF1579D Productive Meeting

难度: 普及/提高-(下位)

分析&解析

贪心策略是:每次社交次数最大的人与第二大的人社交。 所以需要一个大根堆(优先队列)来维护社交次数最大的人。

Code:Code:

Code:Code:
C++
#include <iostream>
#include <queue>
#include <map>
#include <algorithm>
using namespace std;
int t;
int n;
struct node {
	int sj, id;
};
struct cmp{
	bool operator () (const node& x,const node& y){
		return x.sj < y.sj;
	}
};
int main() {
	cin >> t;
	while (t--) {
		cin >> n;
		priority_queue<node,vector<node>,cmp> q;
		for (int i = 1; i <= n; i++) {
			int sj;
			cin >> sj;
			q.push({sj,i});
		}
		int ans = 0;
		node b[200010] = {};
		while(1) {
			node u = q.top();
			q.pop();
			node v = q.top();
			q.pop();
			if(u.sj == 0 || v.sj == 0) break;
			b[++ans] = {u.id,v.id};
			u.sj--,v.sj--;
			q.push(u),q.push(v);
		}
		cout << ans << '\n';
		for (int i = 1; i <= ans; i++)
			cout << b[i].sj << ' ' << b[i].id << '\n';
	}
	return 0;
}