提交记录 3881


用户 题目 状态 得分 用时 内存 语言 代码长度
dwt noi18b. 【NOI2018】冒泡排序 Runtime Error 0 1 s 313656 KB C++ 2.66 KB
提交时间 评测时间
2018-07-18 19:29:43 2020-07-31 21:57:12
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
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(){
    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.588 ms304 MB + 24 KBRuntime ErrorScore: 0

Testcase #269.118 ms304 MB + 24 KBWrong AnswerScore: 0

Testcase #370.025 ms304 MB + 24 KBWrong AnswerScore: 0

Testcase #477.881 ms304 MB + 24 KBWrong AnswerScore: 0

Testcase #589.175 ms304 MB + 24 KBWrong AnswerScore: 0

Testcase #6113.567 ms304 MB + 24 KBWrong AnswerScore: 0

Testcase #7292.431 ms304 MB + 24 KBWrong AnswerScore: 0

Testcase #8292.227 ms304 MB + 24 KBWrong AnswerScore: 0

Testcase #9539.656 ms304 MB + 24 KBWrong AnswerScore: 0

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

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

Testcase #1225.419 ms304 MB + 20 KBRuntime ErrorScore: 0

Testcase #1325.401 ms304 MB + 20 KBRuntime ErrorScore: 0

Testcase #1425.429 ms304 MB + 20 KBRuntime ErrorScore: 0

Testcase #1525.412 ms304 MB + 20 KBRuntime ErrorScore: 0

Testcase #1625.428 ms304 MB + 20 KBRuntime ErrorScore: 0

Testcase #1725.407 ms304 MB + 24 KBRuntime ErrorScore: 0

Testcase #1825.427 ms304 MB + 24 KBRuntime ErrorScore: 0

Testcase #1925.406 ms304 MB + 24 KBRuntime ErrorScore: 0

Testcase #2025.417 ms304 MB + 24 KBRuntime ErrorScore: 0

Testcase #2129.171 ms305 MB + 36 KBRuntime ErrorScore: 0

Testcase #2230.166 ms305 MB + 296 KBRuntime ErrorScore: 0

Testcase #2331.846 ms305 MB + 732 KBRuntime ErrorScore: 0

Testcase #2433.497 ms306 MB + 140 KBRuntime ErrorScore: 0

Testcase #2534.178 ms306 MB + 312 KBRuntime ErrorScore: 0


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