跳到正文

个人记录

slope trick

gala_xk约 39 分钟
原文
文章信息与版本

文章信息

发布
2026年1月15日
收录
2026年10月20日
更新
2026年11月10日
篇幅
约 39 分钟

版本

  1. v1当前

理论

理论

一道初中题

设 F(x)=∣x+5∣+∣x−3∣+∣x−8∣F(x)=|x+5|+|x-3|+|x-8|,求 F(x)F(x) 的最值。

这个简单,我们采用零点分段法,在 x=−5,3,8x=-5,3,8 时分别分段,写作四段分段函数即可。

注意到,k=0k=0(即使是一个点)时答案最优。

我们考虑一个集合 S={−5,−5,3,3,8,8}S=\{-5,-5,3,3,8,8\} 和一个函数 y=−3x+6y=-3x+6 分别表示函数拐点集合和在第一个拐点前的函数。

从前往后扫,对于每个拐点,每出现一次函数的斜率 +1+1。

然后考虑最开始的斜率 kk,在 xx 为第 kk 个拐点集合中的数时函数值最小,所以根据这个集合和函数就能解决最值问题。

性质

记函数 ff 的拐点集合为 SfS_f,最开始的函数为 FfF_f。

若 h(x)=f(x)+g(x)h(x)=f(x)+g(x),

则有:

Sh=Sf+Sg,Fh=Ff+FgS_h=S_f+S_g,F_h=F_f+F_g

其中可重集的加法为可重集并。

证明其实也很简单,首先两个第一段相加,因为一次函数加法就是这样,其次可重集的并是由于斜率的可加性。

解决问题类型

  • 转移形如 fi(x)=min{fj(x)}+g(x)f_i(x)=min\{f_j(x)\}+g(x)。
  • fi,gf_i,g 是凸的、连续的分段函数。
  • fi,gf_i,g 的斜率为整数。

例题

CF13C Sequence

题解

考虑转移,设 f(i,j)f(i,j) 表示考虑前 ii 个数,第 ii 个数权值为 jj 的最小代价,则有转移:

f(i,j)=min⁡k≤j{f(i−1,k)}+∣j−ai∣f(i,j)=\min_{k\le j}\{f(i-1,k)\}+|j-a_i|

我们将状态改写成关于 jj 的函数 fi(j)f_i(j),注意到 f1(j)=∣j−a1∣f_1(j)=|j-a_1| 是一个一次分段函数,一次分段函数的 min⁡\min 也是一次分段函数,并且加上的绝对值函数是凸函数,所以 fif_i 也是凸函数。我们可以把 ff 给转成拐点集合表示。

如何处理 min⁡\min 呢?注意到函数是下凸的,当函数斜率小于 00 后函数值显然大于斜率等于 00,所以取 min⁡k≤j\min_{k\le j} 相当于砍掉所有斜率 <0<0 的数,也就是把斜率 <0<0 的数从集合里丢掉。

然后是插入绝对值函数,我们用一个优先队列维护,插入两个(因为绝对值函数转折是 11 到 −1-1,斜率 −2-2)转折点(aia_i),然后由于总斜率增加了 22,而我们只期望增加 11,所以 pop 掉最大转折点(已经使斜率为负,没用了)即可。

最后是统计答案,每一个没有 pop 掉插入转折的的操作会产生贡献(不然 ∣x−ai∣=0|x-a_i|=0),于是我们只需将答案加上新的转折点 pop 掉其他值的贡献即可(也就是 ∣ai−vpop∣|a_i-v_{pop}|)。

std
C++
/* Use the slash-star style comments or the system won't see your
   identification information */
/*
ID: zhuheng1
TASK:
LANG: C++
*/
/* LANG can be C++11 or C++14 for those more recent releases */
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=998244353;
const int N=100005;
#undef _GLIBCXX_DEBUG
template <typename A, typename B>string to_string(pair<A, B> p);
template <typename A, typename B, typename C>string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D>string to_string(tuple<A, B, C, D> p);
string to_string(const string& s) {  return '"' + s + '"';}
string to_string(const char* s) {  return to_string((string) s);}
string to_string(bool b) {  return (b ? "true" : "false");}
string to_string(vector<bool> v) {  bool first = true;  string res = "{";  for (int i = 0; i < static_cast<int>(v.size()); i++) {  if (!first) {  res += ", ";  }  first = false;  res += to_string(v[i]);  }  res += "}";  return res;}
template <size_t N> string to_string(bitset<N> v) {  string res = ""; for (size_t i = 0; i < N; i++) { res += static_cast<char>('0' + v[i]); } return res; }
template <typename A> string to_string(A v) {bool first = true; string res = "{"; for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res;}
template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; }
template <typename A, typename B, typename C>string to_string(tuple<A, B, C> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")"; }
template <typename A, typename B, typename C, typename D>string to_string(tuple<A, B, C, D> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")"; }
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); }
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)

namespace Rewrite{
	priority_queue<int>q; 
	ll ans=0;
	void Sizuru_Nakatsu(){
		/*fill your code here*/
		int n;
		cin>>n;
		for(int i=1;i<=n;i++){
			int a;
			cin>>a;
			if(q.empty()){
				q.push(a);
			}
			else{
				int c=q.top();
				if(c<=a){
					q.push(a);
				}
				else{
					q.pop();
					q.push(a);q.push(a);
					ans+=c-a;
				}
			}
		}
		cout<<ans;
		return;
	}
}

#ifdef int
signed main()
#else
int main()
#endif
{
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
//	ios::sync_with_stdio(0);
//	cin.tie(0);cout.tie(0);
	int T=1;
//	cin>>T;  /*If there's only one test case, comment this line.*/
	while(T--){
		Rewrite::Sizuru_Nakatsu();
	}
    return 0;
}



/* Code written by
		gala_xk(Shizuru_Nakatsu)*/



P4331 [BalticOI 2004] Sequence (Day1)

题解

首先,序列要求单调,我们可以给其加上一个偏移量 bi=−ib_i=-i 来消除不等于的条件。

为什么这个正确?我们考虑这个偏移量就相当于将 min⁡i≤j\min_{i\le j} 改为 min⁡i−(p−1)≤j−(p)\min_{i-(p-1)\le j-(p)},也就是 min⁡i+1≤j\min_{i+1\le j}。

然后是输出方案,我们只需要记录每一次 pop 的值,和后面一个取 min⁡\min 即可。

std
C++
/* Use the slash-star style comments or the system won't see your
   identification information */
/*
ID: zhuheng1
TASK:
LANG: C++
*/
/* LANG can be C++11 or C++14 for those more recent releases */
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=998244353;
const int N=100005;
#undef _GLIBCXX_DEBUG
template <typename A, typename B>string to_string(pair<A, B> p);
template <typename A, typename B, typename C>string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D>string to_string(tuple<A, B, C, D> p);
string to_string(const string& s) {  return '"' + s + '"';}
string to_string(const char* s) {  return to_string((string) s);}
string to_string(bool b) {  return (b ? "true" : "false");}
string to_string(vector<bool> v) {  bool first = true;  string res = "{";  for (int i = 0; i < static_cast<int>(v.size()); i++) {  if (!first) {  res += ", ";  }  first = false;  res += to_string(v[i]);  }  res += "}";  return res;}
template <size_t N> string to_string(bitset<N> v) {  string res = ""; for (size_t i = 0; i < N; i++) { res += static_cast<char>('0' + v[i]); } return res; }
template <typename A> string to_string(A v) {bool first = true; string res = "{"; for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res;}
template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; }
template <typename A, typename B, typename C>string to_string(tuple<A, B, C> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")"; }
template <typename A, typename B, typename C, typename D>string to_string(tuple<A, B, C, D> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")"; }
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); }
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)

namespace Rewrite{
	priority_queue<int>q; 
	ll ans=0;
	int res[1000005];
	void Sizuru_Nakatsu(){
		/*fill your code here*/
		int n;
		cin>>n;
		for(int i=1;i<=n;i++){
			int a;
			cin>>a;a-=i;
			if(q.empty()){
				q.push(a);
			}
			else{
				int c=q.top();
				if(c<=a){
					q.push(a);
				}
				else{
					q.pop();
					q.push(a);q.push(a);
					ans+=c-a;
				}
			}
					res[i]=q.top()+i;
		}
		for(int i=n-1;i>=1;i--){
			res[i]=min(res[i+1]-1,res[i]);
		}
		cout<<ans<<endl;
		for(int i=1;i<=n;i++){
			cout<<res[i]<<endl;
		}
		return;
	}
}

#ifdef int
signed main()
#else
int main()
#endif
{
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
//	ios::sync_with_stdio(0);
//	cin.tie(0);cout.tie(0);
	int T=1;
//	cin>>T;  /*If there's only one test case, comment this line.*/
	while(T--){
		Rewrite::Sizuru_Nakatsu();
	}
    return 0;
}



/* Code written by
		gala_xk(Shizuru_Nakatsu)*/

P11598 [NOISG 2018 Finals] Safety

题解

转移变为了:

f(i,j)=min⁡j−k≤c≤j+k{f(i−1,c)}+∣j−ai∣f(i,j)=\min_{j-k\le c\le j+k}\{f(i-1,c)\}+|j-a_i|

发现 min⁡\min 有两个限制,我们考虑这个 min⁡\min 相当于将原来的集合分成两段,一段斜率为正,一段非负,中间插入一个长为 2k2k 的斜率为 00 的段。维护一个偏移量即可。

std
C++
/* Use the slash-star style comments or the system won't see your
   identification information */
/*
ID: zhuheng1
TASK:
LANG: C++
*/
/* LANG can be C++11 or C++14 for those more recent releases */
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=998244353;
const int N=100005;
#undef _GLIBCXX_DEBUG
template <typename A, typename B>string to_string(pair<A, B> p);
template <typename A, typename B, typename C>string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D>string to_string(tuple<A, B, C, D> p);
string to_string(const string& s) {  return '"' + s + '"';}
string to_string(const char* s) {  return to_string((string) s);}
string to_string(bool b) {  return (b ? "true" : "false");}
string to_string(vector<bool> v) {  bool first = true;  string res = "{";  for (int i = 0; i < static_cast<int>(v.size()); i++) {  if (!first) {  res += ", ";  }  first = false;  res += to_string(v[i]);  }  res += "}";  return res;}
template <size_t N> string to_string(bitset<N> v) {  string res = ""; for (size_t i = 0; i < N; i++) { res += static_cast<char>('0' + v[i]); } return res; }
template <typename A> string to_string(A v) {bool first = true; string res = "{"; for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res;}
template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; }
template <typename A, typename B, typename C>string to_string(tuple<A, B, C> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")"; }
template <typename A, typename B, typename C, typename D>string to_string(tuple<A, B, C, D> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")"; }
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); }
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)

namespace Rewrite{
	int n;
	ll aa[1000005],ans,k;
	ll bb[1000005];
	priority_queue<ll>q; 
	priority_queue<ll,vector<ll>,greater<ll> >q2; 
	void Sizuru_Nakatsu(){
		/*fill your code here*/
		cin>>n>>k;//k++;
		for(int i=1;i<=n;i++){
			cin>>aa[i];
//			cout<<aa[i]<<" ";
		}
//		cout<<endl;
//		for(int i=1;i<=n;i++){
//			cout<<bb[i]<<" ";
//		}cout<<endl;
//		for(int i=1;i<=n;i++){
//			ll a=aa[i];
//				ll a1=a-i*k,a2=a+i*k;
//			cout<<a1<<" "<<a2<<endl;
//		}
		
		for(int i=1;i<=n;i++){
//			printf("\nCase: %d\n",i);
			ll a=aa[i];
			if(i==1){
				q.push(a-(n-1)*k);
				q2.push(a+(n-1)*k);
			}
			else{
				ll a1=a-(n-i)*k,a2=a+(n-i)*k;
				ll c=q.top()+(n-i)*k,d=q2.top()-(n-i)*k;
				if(a<c){
					q2.push(c+(n-i)*k);
					q.pop();
					q.push(a1);
					q.push(a1);
					ans+=c-a;
//				cout<<i<<" "<<c<<" "<<a<<" ";
//					cout<<"case1\n";
				}
				else if(a>d){
					q.push(d-(n-i)*k);
					q2.pop();
					q2.push(a2);
					q2.push(a2);
					ans+=a-d;
//				cout<<i<<" "<<a<<" "<<d<<" ";
//					cout<<"case2\n";
					
				}
				else{
					q.push(a1);
					q2.push(a2);
				}
			}
//			priority_queue<ll>qq=q; 
//			priority_queue<ll,vector<ll>,greater<ll> >qq2=q2; 
//			while(!qq.empty()){cout<<qq.top()<<" ";qq.pop();}puts("");
//			while(!qq2.empty()){cout<<qq2.top()<<" ";qq2.pop();}puts("");
			
		}
		cout<<ans;
		return;
	}
}

#ifdef int
signed main()
#else
int main()
#endif
{
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	int T=1;
//	cin>>T;  /*If there's only one test case, comment this line.*/
	while(T--){
		Rewrite::Sizuru_Nakatsu();
	}
    return 0;
}



/* Code written by
		gala_xk(Shizuru_Nakatsu)*/



AT_arc070_c [ARC070E] NarrowRectangles

题解

和上一题一样,只不过 kk 变成了前一个和这一个的区间长度。

std
C++
/* Use the slash-star style comments or the system won't see your
   identification information */
/*
ID: zhuheng1
TASK:
LANG: C++
*/
/* LANG can be C++11 or C++14 for those more recent releases */
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=998244353;
const int N=100005;
#undef _GLIBCXX_DEBUG
template <typename A, typename B>string to_string(pair<A, B> p);
template <typename A, typename B, typename C>string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D>string to_string(tuple<A, B, C, D> p);
string to_string(const string& s) {  return '"' + s + '"';}
string to_string(const char* s) {  return to_string((string) s);}
string to_string(bool b) {  return (b ? "true" : "false");}
string to_string(vector<bool> v) {  bool first = true;  string res = "{";  for (int i = 0; i < static_cast<int>(v.size()); i++) {  if (!first) {  res += ", ";  }  first = false;  res += to_string(v[i]);  }  res += "}";  return res;}
template <size_t N> string to_string(bitset<N> v) {  string res = ""; for (size_t i = 0; i < N; i++) { res += static_cast<char>('0' + v[i]); } return res; }
template <typename A> string to_string(A v) {bool first = true; string res = "{"; for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res;}
template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; }
template <typename A, typename B, typename C>string to_string(tuple<A, B, C> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")"; }
template <typename A, typename B, typename C, typename D>string to_string(tuple<A, B, C, D> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")"; }
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); }
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)

namespace Rewrite{
	int n;
	ll aa[1000005],rr[1000005],ans,k;
	ll bb[1000005],cc[1000005];
	priority_queue<ll>q; 
	priority_queue<ll,vector<ll>,greater<ll> >q2; 
	void Sizuru_Nakatsu(){
		/*fill your code here*/
		cin>>n;//k++;
		for(int i=1;i<=n;i++){
			cin>>aa[i]>>rr[i];
//			cout<<aa[i]<<" ";
		}
		for(int i=n-1;i>=1;i--){
			bb[i]=bb[i+1]+rr[i+1]-aa[i+1];
			cc[i]=cc[i+1]+rr[i]-aa[i];
		}
//		cout<<endl;
//		for(int i=1;i<=n;i++){
//			cout<<bb[i]<<" ";
//		}cout<<endl;
//		for(int i=1;i<=n;i++){
//			ll a=aa[i];
//				ll a1=a-i*k,a2=a+i*k;
//			cout<<a1<<" "<<a2<<endl;
//		}
		
		for(int i=1;i<=n;i++){
//			printf("\nCase: %d\n",i);
			ll a=aa[i];
			if(i==1){
				q.push(a-bb[i]);
				q2.push(a+cc[i]);
			}
			else{
				ll a1=a-bb[i],a2=a+cc[i];
				ll c=q.top()+bb[i],d=q2.top()-cc[i];
				if(a<c){
					q2.push(c+cc[i]);
					q.pop();
					q.push(a1);
					q.push(a1);
					ans+=c-a;
//				cout<<i<<" "<<c<<" "<<a<<" ";
//					cout<<"case1\n";
				}
				else if(a>d){
					q.push(d-bb[i]);
					q2.pop();
					q2.push(a2);
					q2.push(a2);
					ans+=a-d;
//				cout<<i<<" "<<a<<" "<<d<<" ";
//					cout<<"case2\n";
					
				}
				else{
					q.push(a1);
					q2.push(a2);
				}
			}
//			priority_queue<ll>qq=q; 
//			priority_queue<ll,vector<ll>,greater<ll> >qq2=q2; 
//			while(!qq.empty()){cout<<qq.top()<<" ";qq.pop();}puts("");
//			while(!qq2.empty()){cout<<qq2.top()<<" ";qq2.pop();}puts("");
			
		}
		cout<<ans;
		return;
	}
}

#ifdef int
signed main()
#else
int main()
#endif
{
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	int T=1;
//	cin>>T;  /*If there's only one test case, comment this line.*/
	while(T--){
		Rewrite::Sizuru_Nakatsu();
	}
    return 0;
}



/* Code written by
		gala_xk(Shizuru_Nakatsu)*/



AT_abc217_h [ABC217H] Snuketoon

题解

和 P11598 几乎一样。

std
C++
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=1e9+7;
const int N=100005;
#undef _GLIBCXX_DEBUG
template <typename A, typename B>string to_string(pair<A, B> p);
template <typename A, typename B, typename C>string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D>string to_string(tuple<A, B, C, D> p);
string to_string(const string& s) {  return '"' + s + '"';}
string to_string(const char* s) {  return to_string((string) s);}
string to_string(bool b) {  return (b ? "true" : "false");}
string to_string(vector<bool> v) {  bool first = true;  string res = "{";  for (int i = 0; i < static_cast<int>(v.size()); i++) {  if (!first) {  res += ", ";  }  first = false;  res += to_string(v[i]);  }  res += "}";  return res;}
template <size_t N> string to_string(bitset<N> v) {  string res = ""; for (size_t i = 0; i < N; i++) { res += static_cast<char>('0' + v[i]); } return res; }
template <typename A> string to_string(A v) {bool first = true; string res = "{"; for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res;}
template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; }
template <typename A, typename B, typename C>string to_string(tuple<A, B, C> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")"; }
template <typename A, typename B, typename C, typename D>string to_string(tuple<A, B, C, D> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")"; }
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); }
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)

namespace Rewrite{
	int n,a[2005];
	struct BIT{
		int a[2005];
		int lowbit(int x){
			return x&(-x);
		}
		void insert(int pos,int val){pos++;
			while(pos<=n+1){
				a[pos]+=val;a[pos]%=mod;
				pos+=lowbit(pos);
			}
		}
		int que(int pos){
			ll res=0;pos++;
			while(pos){
				res+=a[pos];
				pos-=lowbit(pos);
			}
			return res%mod;
		}
		int que(int l,int r){
			return (que(r)-que(l-1)+mod)%mod;
		}
	}tr[2005],tr2[2005],tr3[2005];
	ll dp[2005][2005];
	int qpow(int x,int y){
		if(y==0)return 1;
		ll z=qpow(x,y/2);
		z=z*z%mod;
		if(y&1)z=z*x%mod;
		return z;
	}
	ll inv(int x){
		return qpow(x,mod-2);
	}
	ll Inv[2005];
	void Sizuru_Nakatsu(){
		/*fill your code here*/
		cin>>n;Inv[0]=0;a[n+1]=n+1;
		for(int i=1;i<=n;i++){
			cin>>a[i];
		}
		for(int i=0;i<=n+1;i++){Inv[i]=inv(i);
			dp[i][i]=1;
			tr[i].insert(a[i],dp[i][i]);
			tr2[i].insert(a[i],dp[i][i]);
			tr3[i].insert(a[i],1);
		}
		for(int len=2;len<=n+2;len++){
			for(int l=0;l<=n+1;l++){
				int r=l+len-1;
				if(r>n+1)break;
				if(a[l]>a[r])continue;
//				if(len==2){
//					dp[l][r]=2;
//					continue;
//				}
				tr3[l].insert(a[r],1);
				int cc=tr3[l].que(a[l]+1,a[r]-1);
				if(cc==0)continue;
				dp[l][r]=tr[l].que(a[l]+1,a[r]-1)+tr2[r].que(a[l]+1,a[r]-1);
				dp[l][r]*=Inv[cc];;
				dp[l][r]++;
				dp[l][r]%=mod;
//				debug(l,r,dp[l][r]);
				tr[l].insert(a[r],dp[l][r]);
				tr2[r].insert(a[l],dp[l][r]);
			}
		}
		cout<<dp[0][n+1];
		return;
	}
}

#ifdef int
signed main()
#else
int main()
#endif
{
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
//	ios::sync_with_stdio(0);
//	cin.tie(0);cout.tie(0);
	int T=1;
//	cin>>T;  /*If there's only one test case, comment this line.*/
	while(T--){
		Rewrite::Sizuru_Nakatsu();
	}
    return 0;
}



/* Code written by
		gala_xk(Shizuru_Nakatsu)*/



原文