提交记录 3879


用户 题目 状态 得分 用时 内存 语言 代码长度
dwt noi18b. 【NOI2018】冒泡排序 Runtime Error 0 1 s 313680 KB C++ 2.73 KB
提交时间 评测时间
2018-07-18 19:28:34 2020-07-31 21:57:00
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
using namespace std;
#define N 600010
#define M 998244353
int n,ans,vis[N],p[N],a[N];
int dp[19][2][2][2][524290];
inline int read(){
    char ch=getchar(); int x=0,f=1;
    for (;ch>'9'||ch<'0';ch=getchar()) if (ch=='-') f=-1;
    for (;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0';
    return x*f;
}
int dfs(int x,bool flag,bool flag1,bool flag2,int P){
	for (int i=1;i<x;i++) {
		if (p[i]>i) for (int j=i+1;j<=min(p[i],x-1);j++) if (p[j]>p[i]) flag1=0;
		if (p[i]==i) for (int j=i+1;j<x;j++) if (p[j]<p[i]) return 0;
	}
    if (!flag1 && !flag2) return 0;
    if (x==n+1 && flag && (flag1 || flag2)){
	//	ans++;
	//	for (int i=1;i<=n;i++) printf("%d ",p[i]);
	//	puts("");
		return 1;
	}
    if (dp[x][flag][flag1][flag2][P]!=-1) return dp[x][flag][flag1][flag2][P];
    int &res=dp[x][flag][flag1][flag2][P]; res=0;
    if (!flag){
        for (int i=a[x];i<=n;i++){
            if (vis[i]) continue;
            if (i>x){
                vis[i]=1;
                p[x]=i;
                (res+=dfs(x+1,i!=a[x],flag1,flag2,P|(1<<i-1)))%=M;
                vis[i]=0;
            }else if (i<x){
                bool f=0;
                for (int j=i;j<x;j++) if (p[j]<i) {f=1; break;}
            //    if (f) continue;
                vis[i]=1; p[x]=i;
                (res+=dfs(x+1,i!=a[x],flag1,!f,P|(1<<i-1)))%=M;
                vis[i]=0;
            }else if (i==x){
            	bool f=0;
            	for (int j=1;j<x;j++) if (p[j]>i){f=1; break;}
            	if (f) continue;
            	vis[i]=1; p[x]=i;
            	(res+=dfs(x+1,i!=a[x],flag1,flag2,P|(1<<i-1)))%=M;
            	vis[i]=0;
        	}
        }
        return res;
    }
    for (int i=1;i<=n;i++){
        if (vis[i]) continue;
        if (i>=x){
                vis[i]=1;
                p[x]=i;
                (res+=dfs(x+1,1,flag1,flag2,P|(1<<i-1)))%=M;
                vis[i]=0;
            }else if (i<x){
                bool f=0;
                for (int j=i;j<x;j++) if (p[j]<i) {f=1; break;}
            //    if (f) continue;
                vis[i]=1; p[x]=i;
                (res+=dfs(x+1,1,flag1,!f,P|(1<<i-1)))%=M;
                vis[i]=0;
            }else if (i==x){
            	bool f=0;
            	for (int j=1;j<x;j++) if (p[j]>i){f=1; break;}
            	if (f) continue;
            	vis[i]=1; p[x]=i;
            	(res+=dfs(x+1,1,flag1,flag2,P|(1<<i-1)))%=M;
            	vis[i]=0;
        	}
    }
    return res;

}
int main(){
	freopen("inverse.in","r",stdin);
	freopen("inverse.out","w",stdout);
    int T=read();
    while (T--){
        memset(dp,-1,sizeof(dp));
        n=read();
        for (int i=1;i<=n;i++) a[i]=read();
        ans=dfs(1,0,1,1,0);
        printf("%d\n",ans);
    }
    return 0;
}

CompilationN/AN/ACompile OKScore: N/A

Testcase #168.887 ms304 MB + 48 KBRuntime ErrorScore: 0

Testcase #269.136 ms304 MB + 48 KBWrong AnswerScore: 0

Testcase #370.046 ms304 MB + 48 KBWrong AnswerScore: 0

Testcase #478.492 ms304 MB + 48 KBWrong AnswerScore: 0

Testcase #590.024 ms304 MB + 48 KBWrong AnswerScore: 0

Testcase #6115.27 ms304 MB + 48 KBWrong AnswerScore: 0

Testcase #7299.759 ms304 MB + 48 KBWrong AnswerScore: 0

Testcase #8299.925 ms304 MB + 48 KBWrong AnswerScore: 0

Testcase #9558.939 ms304 MB + 48 KBWrong AnswerScore: 0

Testcase #101 s304 MB + 48 KBTime Limit ExceededScore: 0

Testcase #111 s304 MB + 48 KBTime Limit ExceededScore: 0

Testcase #1225.506 ms304 MB + 44 KBRuntime ErrorScore: 0

Testcase #1325.488 ms304 MB + 44 KBRuntime ErrorScore: 0

Testcase #1425.482 ms304 MB + 44 KBRuntime ErrorScore: 0

Testcase #1525.485 ms304 MB + 44 KBRuntime ErrorScore: 0

Testcase #1625.471 ms304 MB + 44 KBRuntime ErrorScore: 0

Testcase #1725.499 ms304 MB + 48 KBRuntime ErrorScore: 0

Testcase #1825.487 ms304 MB + 48 KBRuntime ErrorScore: 0

Testcase #1925.505 ms304 MB + 48 KBRuntime ErrorScore: 0

Testcase #2025.486 ms304 MB + 48 KBRuntime ErrorScore: 0

Testcase #2128.849 ms305 MB + 64 KBRuntime ErrorScore: 0

Testcase #2229.775 ms305 MB + 324 KBRuntime ErrorScore: 0

Testcase #2331.281 ms305 MB + 756 KBRuntime ErrorScore: 0

Testcase #2432.748 ms306 MB + 168 KBRuntime ErrorScore: 0

Testcase #2533.421 ms306 MB + 336 KBRuntime ErrorScore: 0


Judge Duck Online | 评测鸭在线
Server Time: 2026-04-17 20:43:19 | Loaded in 1 ms | Server Status
个人娱乐项目,仅供学习交流使用 | 捐赠