P14481 星命定轨
代码
#include<bits/stdc++.h>
#define ll long long
using namespace std;
template<typename T> inline void read(T &x){
T w=1;
x=0;
char c=getchar();
while(!isdigit(c)){
if(c=='-') w=-1;
c=getchar();
}
while(isdigit(c)) x=(x<<1)+(x<<3)+(c^48),c=getchar();
x*=w;
}
template<typename T> inline void write(T x){
if(x<0) putchar('-'),x=(~x)+1;
if(x>9) write(x/10);
putchar(x%10^48);
}
const ll N=1e6+5,inf=1e17;
ll st[N],top;
ll a[N],n;
ll ls[N],rs[N];
ll f[N][3][2][2];
void chkmax(ll &x,ll y){
if(y>x) x=y;
}
void dfs(ll u){
if(!ls[u]&&!rs[u]){
for(int i=0;i<3;++i){
for(int j=0;j<2;++j){
for(int k=0;k<2;k++) f[u][i][j][k]=(i==1 ? 0 : -inf);
}
}
f[u][0][1][1]=a[u];
return ;
}
if(ls[u]) dfs(ls[u]);
if(rs[u]) dfs(rs[u]);
for(int lu=0;lu<3;++lu){
for(int ru=0;ru<3;++ru){
for(int lx=0;lx<2;++lx){
for(int ly=0;ly<2;++ly){
for(int rx=0;rx<2;++rx){
for(int ry=0;ry<2;++ry){
if((ly&&rx)||f[ls[u]][lu][lx][ly]<0||f[rs[u]][ru][rx][ry]<0) continue;
ll w=f[ls[u]][lu][lx][ly]+f[rs[u]][ru][rx][ry];
ll pts=lu+ru+1-(ly||rx);
chkmax(f[u][min(pts,2ll)][lx][ry],w);
if(pts>=3) chkmax(f[u][min(pts-3,2ll)][lx][ry],w+a[u]);
if(pts>=2){
if(!lx) chkmax(f[u][min(pts-2,2ll)][1][ry],w+a[u]);
if(!ry) chkmax(f[u][min(pts-2,2ll)][lx][1],w+a[u]);
}
if(pts>=1&&!lx&&!ry) chkmax(f[u][min(pts-1,2ll)][1][1],w+a[u]);
}
}
}
}
}
}
}
int main(){
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
read(n);
for(int i=1;i<=n;++i) read(a[i]);
for(int i=0;i<3;++i){
for(int j=0;j<4;++j){
f[0][i][j&1][(j>>1)&1]=(i ? -inf : 0);
}
}
for(int i=1;i<=n;++i){
while(top&&a[st[top]]>=a[i]) ls[i]=st[top--];
if(top) rs[st[top]]=i;
st[++top]=i;
}
dfs(st[1]);
write(max(f[st[1]][0][0][0],max(f[st[1]][1][0][0],f[st[1]][2][0][0])));
return 0;
}
CF1874F Jellyfish and OEIS
妙妙 trick。
代码
#include<bits/stdc++.h>
#define ll long long
using namespace std;
template<typename T> inline void read(T &x){
T w=1;
x=0;
char c=getchar();
while(!isdigit(c)){
if(c=='-') w=-1;
c=getchar();
}
while(isdigit(c)) x=(x<<1)+(x<<3)+(c^48),c=getchar();
x*=w;
}
template<typename T> inline void write(T x){
if(x<0) putchar('-'),x=(~x)+1;
if(x>9) write(x/10);
putchar(x%10^48);
}
const ll N=205,mod=1e9+7;
ll n,a[N];
ll f[N][N],g[N][N],fac[N];
int main(){
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
read(n);
for(int i=1;i<=n;i++) read(a[i]);
if(a[1]>=n){
write(0);
return 0;
}
fac[0]=1;
for(int i=1;i<=n;i++) fac[i]=fac[i-1]*i%mod;
for(int l=n;l>=1;l--){
memset(g,0,sizeof g);
g[l-1][0]=1;
for(int r=l;r<=n;r++){
for(int c=0;c<=r-l+1;c++){
if(c) g[r][c]=g[r-1][c-1];
for(int i=l+1;i<=r;i++){
if(a[i]>=r) g[r][c]=(g[r][c]-g[i-1][c]*f[i][r]%mod)%mod;
}
f[l][r]=(f[l][r]+g[r][c]*fac[c]%mod)%mod;
}
if(a[l]>=r) g[r][0]=(g[r][0]-f[l][r]%mod);
}
}
write((f[1][n]+mod)%mod);
return 0;
}