Submission #34044850
Source Code Expand
#include<bits/stdc++.h>
#ifdef xay5421
#define D(...) fprintf(stderr,__VA_ARGS__)
#define DD(...) D(#__VA_ARGS__ "="),debug_helper::debug(__VA_ARGS__),D("\n")
#include"/home/xay5421/debug.hpp"
#else
#define D(...) ((void)0)
#define DD(...) ((void)0)
#endif
#define pb push_back
#define eb emplace_back
#define SZ(x) ((int)(x).size())
#define each(x,v) for(auto&x:v)
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define per(i,a,b) for(int i=(a);i>=(b);--i)
template<class T>void rd(T&x){int f=0,c;while(!isdigit(c=getchar()))f^=!(c^45);x=(c&15);while(isdigit(c=getchar()))x=x*10+(c&15);if(f)x=-x;}
template<class T>void pt(T x,int c=-1){if(x<0)putchar('-'),x=-x;if(x>9)pt(x/10);putchar(x%10+48);if(c!=-1)putchar(c);}
using namespace std;
using LL=long long;
using ULL=unsigned long long;
template<int P>
class mod_int{
using Z=mod_int;
private:
static int mo(int x){return x<0?x+P:x;}
public:
int x;
int val()const{return x;}
mod_int():x(0){}
template<class T>mod_int(const T&x_):x(x_>=0&&x_<P?static_cast<int>(x_):mo(static_cast<int>(x_%P))){}
bool operator==(const Z&rhs)const{return x==rhs.x;}
bool operator!=(const Z&rhs)const{return x!=rhs.x;}
Z operator-()const{return Z(x?P-x:0);}
Z pow(long long k)const{
Z res=1,t=*this;
while(k){
if(k&1)res*=t;
if(k>>=1)t*=t;
}
return res;
}
Z&operator++(){
x<P-1?++x:x=0;
return *this;
}
Z&operator--(){
x?--x:x=P-1;
return *this;
}
Z operator++(int){
Z ret=x;
x<P-1?++x:x=0;
return ret;
}
Z operator--(int){
Z ret=x;
x?--x:x=P-1;
return ret;
}
Z inv()const{
#ifdef xay5421
assert(x!=0);
#endif
return pow(P-2);
}
Z&operator+=(const Z&rhs){
(x+=rhs.x)>=P&&(x-=P);
return *this;
}
Z&operator-=(const Z&rhs){
(x-=rhs.x)<0&&(x+=P);
return *this;
}
Z&operator*=(const Z&rhs){
x=1ULL*x*rhs.x%P;
return *this;
}
Z&operator/=(const Z&rhs){
return *this*=rhs.inv();
}
#define setO(T,o) friend T operator o(const Z&lhs,const Z&rhs){Z res=lhs;return res o##=rhs;}
setO(Z,+)setO(Z,-)setO(Z,*)setO(Z,/)
#undef setO
};
const int P=998244353;
using Z=mod_int<P>;
const int N=5005;
int n,a[N],aa[N],ok[N];
Z f[2][N],tag[N];
void deal(int i){
fill(ok+1,ok+n+1,0);
int mx=0;
per(j,i,1){
mx=max(mx,a[j]);
ok[mx]=1;
}
mx=0;
rep(j,i,n){
mx=max(mx,a[j]);
ok[mx]=1;
}
rep(j,1,n)if(!ok[j]){
f[i&1][j]=0;
}
}
int main(){
rd(n);
rep(i,1,n)rd(a[i]);
rep(i,1,n)aa[a[i]]=i;
int mx=0;
rep(i,1,n){
mx=max(mx,a[i]);
f[1][mx]=1;
}
rep(i,1,n-1){
rep(j,0,n+1)f[(i+1)&1][j]=0;
rep(j,0,n+1)tag[j]=0;
rep(j,1,n)if(f[i&1][j].val()){
tag[j]+=f[i&1][j];
{
int j_=a[i+1];
if(aa[j_]>=aa[j]){
f[(i+1)&1][j_]+=f[i&1][j];
}
}
if(j>=a[i+1])f[(i+1)&1][j]-=f[i&1][j];
f[(i+1)&1][j]+=f[i&1][j];
}
rep(j,1,n){
if(j)tag[a[j]]+=tag[a[j-1]];
if(a[j]>=a[i+1]+1){
f[(i+1)&1][a[j]]+=tag[a[j]];
}
}
deal(i+1);
}
Z ans=0;
rep(j,1,n)ans+=f[n&1][j];
printf("%d\n",ans.val());
return 0;
}
Submission Info
Submission Time |
|
Task |
B - Adjacent Chmax |
User |
xay5421 |
Language |
C++ (GCC 9.2.1) |
Score |
700 |
Code Size |
3090 Byte |
Status |
AC |
Exec Time |
224 ms |
Memory |
3864 KiB |
Judge Result
Set Name |
Sample |
All |
Score / Max Score |
0 / 0 |
700 / 700 |
Status |
|
|
Set Name |
Test Cases |
Sample |
00-sample-001.txt, 00-sample-002.txt, 00-sample-003.txt |
All |
00-sample-001.txt, 00-sample-002.txt, 00-sample-003.txt, 01-001.txt, 01-002.txt, 01-003.txt, 01-004.txt, 01-005.txt, 01-006.txt, 01-007.txt, 01-008.txt, 01-009.txt, 01-010.txt, 01-011.txt, 01-012.txt, 01-013.txt, 01-014.txt, 01-015.txt, 01-016.txt, 01-017.txt, 01-018.txt, 01-019.txt, 01-020.txt, 01-021.txt, 01-022.txt, 01-023.txt, 01-024.txt, 01-025.txt, 01-026.txt, 01-027.txt, 01-028.txt, 01-029.txt, 01-030.txt, 01-031.txt, 01-032.txt, 01-033.txt, 01-034.txt, 01-035.txt, 01-036.txt, 01-037.txt |
Case Name |
Status |
Exec Time |
Memory |
00-sample-001.txt |
AC |
6 ms |
3568 KiB |
00-sample-002.txt |
AC |
2 ms |
3592 KiB |
00-sample-003.txt |
AC |
2 ms |
3804 KiB |
01-001.txt |
AC |
2 ms |
3736 KiB |
01-002.txt |
AC |
53 ms |
3844 KiB |
01-003.txt |
AC |
5 ms |
3576 KiB |
01-004.txt |
AC |
137 ms |
3796 KiB |
01-005.txt |
AC |
14 ms |
3588 KiB |
01-006.txt |
AC |
24 ms |
3664 KiB |
01-007.txt |
AC |
162 ms |
3640 KiB |
01-008.txt |
AC |
165 ms |
3756 KiB |
01-009.txt |
AC |
157 ms |
3688 KiB |
01-010.txt |
AC |
215 ms |
3792 KiB |
01-011.txt |
AC |
216 ms |
3756 KiB |
01-012.txt |
AC |
217 ms |
3672 KiB |
01-013.txt |
AC |
218 ms |
3796 KiB |
01-014.txt |
AC |
193 ms |
3704 KiB |
01-015.txt |
AC |
217 ms |
3692 KiB |
01-016.txt |
AC |
194 ms |
3864 KiB |
01-017.txt |
AC |
216 ms |
3756 KiB |
01-018.txt |
AC |
158 ms |
3852 KiB |
01-019.txt |
AC |
216 ms |
3688 KiB |
01-020.txt |
AC |
158 ms |
3676 KiB |
01-021.txt |
AC |
217 ms |
3720 KiB |
01-022.txt |
AC |
160 ms |
3760 KiB |
01-023.txt |
AC |
215 ms |
3616 KiB |
01-024.txt |
AC |
158 ms |
3692 KiB |
01-025.txt |
AC |
216 ms |
3848 KiB |
01-026.txt |
AC |
194 ms |
3692 KiB |
01-027.txt |
AC |
218 ms |
3720 KiB |
01-028.txt |
AC |
157 ms |
3796 KiB |
01-029.txt |
AC |
220 ms |
3792 KiB |
01-030.txt |
AC |
158 ms |
3760 KiB |
01-031.txt |
AC |
216 ms |
3688 KiB |
01-032.txt |
AC |
157 ms |
3568 KiB |
01-033.txt |
AC |
217 ms |
3624 KiB |
01-034.txt |
AC |
162 ms |
3784 KiB |
01-035.txt |
AC |
224 ms |
3848 KiB |
01-036.txt |
AC |
154 ms |
3676 KiB |
01-037.txt |
AC |
167 ms |
3860 KiB |