QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#515259 | #2271. High-Tech Detective | urayaha_yahaura# | WA | 0ms | 3928kb | C++14 | 2.7kb | 2024-08-11 16:32:19 | 2024-08-11 16:32:20 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N=1e4+7;
const int mod=1e9+7;
int read(){
int x=0;
char c;
while(!isdigit(c=getchar()));
while(isdigit(c)){
x=x*10+(c^48);
c=getchar();
}
return x;
}
struct mint{
int x;
mint(int x=0):x(x%mod){}
friend mint operator +(mint a,mint b){
a.x+=b.x;
if(a.x>=mod)
a.x-=mod;
return a;
}
friend mint operator *(mint a,mint b){
return mint(1ll*a.x*b.x%mod);
}
};
int qpow(int a,int x){
mint A=a,res=1;
while(x){
if(x&1)
res=res*A;
A=A*A;
x>>=1;
}
return res.x;
}
int n,m,x,cnt,I[N],O[N];
char op[N],a[N];
int pre[N],sufI[N],sufO[N],isufO[N];
mint f[N],g[N];
int main(){
n=read();
for(int i=1;i<=n*2;++i){
char c;
while(!isalpha(c=getchar()));
x=read();
if(x){
if(c=='I'){
if(O[x]){
puts("0");
return 0;
}
I[x]=i;
}
else{
if(!I[x])
++cnt;
O[x]=i;
}
}
op[i]=c;
a[i]=x;
}
for(int i=1;i<=n*2;++i){
if(I[a[i]]&&O[a[i]])
continue;
++m;
op[m]=op[i];
a[m]=a[i];
}
for(int i=1;i<=m;++i){
if(op[i]=='O')
pre[i]=pre[i-1]-1;
else
pre[i]=pre[i-1]+1;
if(pre[i]<0){
puts("0");
return 0;
}
}
for(int i=m;i;--i){
sufI[i]=sufI[i+1];
sufO[i]=sufO[i+1];
if(op[i]=='O'&&a[i])
++sufO[i];
else if(op[i]=='I'&&!a[i])
++sufI[i];
isufO[i]=qpow(sufO[i],mod-2);
}
f[cnt]=1;
for(int i=1;i<=m;++i){
for(int j=0;j<=cnt;++j){
if(sufI[i]<j){
g[j]=0;
continue;
}
if(op[i]=='I'){
if(a[i])
g[j]=f[j];
else
g[j]=f[j+1]*(j+1)+f[j]*(sufI[i]-j);
}
else{
if(a[i]){
if(sufO[i]>j)
g[j]=f[j]*(sufO[i]-j)*isufO[i];
else
g[j]=0;
}
else{
if(pre[i-1]>j)
g[j]=f[j]*(pre[i-1]-j);
else
g[j]=0;
}
}
}
for(int j=0;j<=cnt;++j)
f[j]=g[j];
}
printf("%d\n",f[0].x);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3904kb
input:
4 I 1 I 0 O 0 I 0 O 2 I 4 O 0 O 4
output:
3
result:
ok single line: '3'
Test #2:
score: 0
Accepted
time: 0ms
memory: 3924kb
input:
3 I 0 I 0 I 0 O 0 O 0 O 0
output:
36
result:
ok single line: '36'
Test #3:
score: 0
Accepted
time: 0ms
memory: 3928kb
input:
1 I 0 O 1
output:
1
result:
ok single line: '1'
Test #4:
score: 0
Accepted
time: 0ms
memory: 3908kb
input:
2 I 0 I 1 O 1 O 0
output:
1
result:
ok single line: '1'
Test #5:
score: 0
Accepted
time: 0ms
memory: 3900kb
input:
4 I 0 I 0 O 0 O 0 I 0 I 3 O 0 O 0
output:
24
result:
ok single line: '24'
Test #6:
score: -100
Wrong Answer
time: 0ms
memory: 3868kb
input:
8 I 0 I 0 I 0 I 8 I 4 I 0 O 1 I 0 O 8 O 4 I 5 O 0 O 0 O 5 O 0 O 7
output:
2304
result:
wrong answer 1st lines differ - expected: '576', found: '2304'