slope trick
理论
:::info[理论]
一道初中题
设
这个简单,我们采用零点分段法,在
注意到,
我们考虑一个集合
从前往后扫,对于每个拐点,每出现一次函数的斜率
然后考虑最开始的斜率
性质
记函数
若
则有:
其中可重集的加法为可重集并。
证明其实也很简单,首先两个第一段相加,因为一次函数加法就是这样,其次可重集的并是由于斜率的可加性。
解决问题类型
- 转移形如
f_i(x)=min\{f_j(x)\}+g(x) 。 -
-
:::
例题
CF13C Sequence
::::info[题解]
考虑转移,设
我们将状态改写成关于
如何处理
然后是插入绝对值函数,我们用一个优先队列维护,插入两个(因为绝对值函数转折是
最后是统计答案,每一个没有 pop 掉插入转折的的操作会产生贡献(不然
/* 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)
::::info[题解]
首先,序列要求单调,我们可以给其加上一个偏移量
为什么这个正确?我们考虑这个偏移量就相当于将
| 然后是输出方案,我们只需要记录每一次 pop 的值,和后面一个取 |
|---|
| :::info[std] |
| ```cpp |
| /* 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](https://www.luogu.com.cn/problem/P11598)
::::info[题解]
转移变为了:
$$f(i,j)=\min_{j-k\le c\le j+k}\{f(i-1,c)\}+|j-a_i|$$
发现 $\min$ 有两个限制,我们考虑这个 $\min$ 相当于将原来的集合分成两段,一段斜率为正,一段非负,中间插入一个长为 $2k$ 的斜率为 $0$ 的段。维护一个偏移量即可。
::::
:::info[std]
```cpp
/* 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
::::info[题解]
和上一题一样,只不过
/* 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
::::info[题解] 和 P11598 几乎一样。 :::: :::info[std]
#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)*/
:::