跳到正文

题解

P1001 A+B Problem 题解

PigeonTree约 6 分钟
原文

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

文章信息与版本

文章信息

发布
2026年7月4日
收录
2026年7月4日
更新
2026年11月11日
篇幅
约 6 分钟
题目
P1001

版本

  1. v4当前
  2. v3
  3. v2
  4. v1

这道题目这么长,肯定很困难,故写下此记。

题目思路

观察题目,注意到 A,BA,B 在 [−109,109][-10^9,10^9] 之间,但若我们将 A,BA,B 看成一个序列 SS,则发现 ∣S∣|S| 已经固定为 22,十分的小。则整个题目被转换为:

给你一个长度固定为 22 的序列,我们需要在序列中选出 22 个数,使得所选之数的和最大。

解法 1

易发现可以直接使用 sort 函数对序列 SS 从大到小排序,并输出 ∑i=12Si\sum_{i=1}^2S_i 即可。

易证得时间复杂度为 O(Nlog⁡N)O(N \log N).

参考程序 1
C++
#include<bits/stdc++.h>
#define ft first
#define sd second
#define pb push_back
#define int long long
using namespace std;
const int N=2+5,INF=0x3f3f3f3f;
int n=2,S[N];
void init(){}
void solution(){
	for(int i=1;i<=n;i++)cin>>S[i];
	stable_sort(S+1,S+1+n,[](int x,int y){
		return x>y;
	});
	cout<<S[1]+S[2];
}
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	int T=1;
	// cin>>T;
	while(T--){
		init();
		solution();
	}
	return 0;
}

解法 2

易发现 NN 十分地小,则可以考虑使用 DP。

分析一下,设 fi,jf_{i,j} 表示,前 ii 个数中,选择出jj 个数字的和最大值,且此时 SiS_i 已经被选中。则易推出动态转移方程:

fi,j=Si+max⁡1≤k≤i−1{fk,j−1}f_{i,j}=S_i+\max_{1\le k\le i-1}\{f_{k,j-1}\}

接下来考虑边界情况和答案统计。

易发现,∀1≤i≤N, fi,1=Si\forall 1\le i\le N,\ f_{i,1}=S_i。答案显然就是 fN,2f_{N,2}。

综上时间复杂度易得为 O(N3)O(N^3)。

另外提一嘴的是,在我们转移的时候,因为 SiS_i 可能为负,所以要设这个临时变量 _max 为 −∞-\infin,而且可能没有判断过,所以还要在循环过后判断 _max 是否为 −∞-\infin。

参考程序 2
C++
#include<bits/stdc++.h>
#define ft first
#define sd second
#define pb push_back
#define int long long
using namespace std;
const int N=2+5,INF=0x3f3f3f3f;
int n=2,S[N],f[N][N];
void init(){}
void solution(){
	for(int i=1;i<=n;i++)cin>>S[i];
	for(int i=1;i<=n;i++){
		f[i][1]=S[i];
		for(int j=1;j<=n;j++){
			int _max=-INF;
			for(int k=1;k<=i-1;k++){
				_max=max(_max,f[k][j-1]);
			}
			if(_max!=-INF)f[i][j]=S[i]+_max;
		}
	}
	cout<<f[n][2];
}
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	int T=1;
	// cin>>T;
	while(T--){
		init();
		solution();
	}
	return 0;
}

解法 3

接解法 2。

易发现我们需要求的是前面某个状态的最小值,发现可以使用数据结构或者优先队列优化。

这里以数据结构为例。

我们需要一个数据结构,可以实现单点更改和区间最值,很显然是——线段树。 此外,注意到我们只需要查前缀最大值,则也可以使用码量较小的树状数组,这里以树状数组为例。

很轻松的就来到了 O(N2logN)O(N^2logN)。

参考程序 3
C++
#include<bits/stdc++.h>
#define ft first
#define sd second
#define pb push_back
#define int long long
using namespace std;
const int N=2+5,INF=0x3f3f3f3f;
int n=2,S[N],f[N][N];
int c[N][N];//树状数组
int lowbit(int x){
	return x&(-x);
}
void update(int x,int value,int j){
	for(;x<=n;x+=lowbit(x))c[x][j]=max(c[x][j],value);
}
int ask(int x,int j){
	if(x==0)return 0;//特殊判断
	int rtn=-INF;
	for(;x>=1;x-=lowbit(x))rtn=max(c[x][j],rtn);
	return rtn;
}
void init(){
	memset(c,-INF,sizeof c);
}
void solution(){
	for(int i=1;i<=n;i++)cin>>S[i];
	for(int i=1;i<=n;i++){
		f[i][1]=S[i];
		for(int j=1;j<=n;j++){
			f[i][j]=S[i]+ask(i-1,j);
			update(i,f[i][j],j);
		}
	}
	cout<<f[n][2];
}
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	int T=1;
	// cin>>T;
	while(T--){
		init();
		solution();
	}
	return 0;
}

后记

这是一道有难度的数据结构/优先队列优化 dp 的,虽然并未我想象中那么难,但是 CCF 居然似乎没考过这道题!!!