QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#593632#7875. Queue SortingJZYZAC ✓162ms14056kbC++142.4kb2024-09-27 15:09:382024-09-27 15:09:39

Judging History

你现在查看的是最新测评结果

  • [2024-09-27 15:09:39]
  • 评测
  • 测评结果:AC
  • 用时:162ms
  • 内存:14056kb
  • [2024-09-27 15:09:38]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std ;

typedef long long LL ;
const int N = 510 , mod = 998244353 ;

int n , a[N] , S[N] ;
LL dp[2][N][N] , tmp[N][N] , sum[2][N][N] ;
 
int main()
{
	scanf("%d" , &n ) ;
//	if( n == 300 ) {
//		cout << 507010274 ;
//		return 0 ;
//	}
	for(int i = 1 ; i <= n ; i ++ ) {
		scanf("%d" , &a[i] ) ;
		if( a[i] == 0 ) {
			i -- ; n -- ;
			continue ;
		}
		S[i] = S[i-1] + a[i] ;
	}
	dp[S[1]&1][S[1]][0] = 1 ;
//	sum[S[1]&1][S[1]][0] = 1 ;
	for(int k = 0 ; k < S[1] ; k ++ ) {
		sum[S[1]&1][S[1]][k] = ( (k?sum[S[1]&1][S[1]][k-1]:0) + dp[S[1]&1][S[1]][k] ) % mod ;
	}
	int now = 2 ;//种类 
	tmp[S[2]][0] = 1 ;
//	cout << tmp[3][0] << " lk" << endl ;
	for(int i = S[1]+1 ; i <= S[n] ; i ++ ) {
		int num = S[now]-i ;
		// 全部放到结尾
		for(int j = 1 ; j <= S[now] ; j ++ ) {
			dp[i&1][j][0] = 0 ; sum[i&1][j][0] = 0 ;
//			printf("dp[%d][%d][%d] = %lld\n" , i , j , 0 , dp[i&1][j][0] ) ;
			for(int k = 1 ; k < j ; k ++ ) {
				dp[i&1][j][k] = 0 ;
				dp[i&1][j][k] = sum[(i-1)&1][j-1][k-1] ;
//				for(int p = 0 ; p <= k-1 ; p ++ ) {
//					dp[i&1][j][k] = ( dp[i&1][j][k] + dp[(i-1)&1][j-1][p] ) % mod ;
//				}
//				printf("dp[%d][%d][%d] = %lld\n" , i , j , k , dp[i&1][j][k] ) ;
				// 剩num个放到结尾 
				tmp[j+num-k][0] = ( tmp[j+num-k][0] + dp[i&1][j][k] ) % mod ;
//				printf("Contribute to [%d][%d] , val=%lld\n" , j+num-k , 0 , tmp[j+num-k][0] ) ;
				sum[i&1][j][k] = ( sum[i&1][j][k-1] + dp[i&1][j][k] ) % mod ;
			}
		}
		if( i == S[now] ) {
			for(int j = 0 ; j <= S[now] ; j ++ ) {
				for(int k = 0 ; k < j ; k ++ ) {
					dp[i&1][j][k] = tmp[j][k] ;
					sum[i&1][j][k] = ( (k?sum[i&1][j][k-1]:0) + dp[i&1][j][k] ) % mod ;
//					printf("New dp[%d][%d][%d] = %lld\n" , i , j , k , dp[i&1][j][k] ) ;
				}
			}
			memset( tmp , 0 , sizeof tmp ) ;
			now ++ ;
			if( a[now] ) { // 直接考虑 a[now] 个全放到结尾 
				for(int j = 1 ; j <= S[now-1] ; j ++ ) {
					tmp[j+a[now]][0] = ( tmp[j+a[now]][0] + dp[i&1][j][0] ) % mod ;
//					printf("contri to %d %d , val=%lld\n" , j+a[now] , 0 , tmp[j+a[now]][0] ) ;
				}
			}
		}
	}
	LL ans = 0 ;
	for(int j = 1 ; j <= S[n] ; j ++ ) {
//		printf("Ans: dp[%d][%d][%d] = %lld\n" , S[n] , j , 0 , dp[S[n]][j][0] ) ;
		ans = ( ans + dp[S[n]&1][j][0] ) % mod ;
	} 
	printf("%lld" , ans ) ;
	return 0 ;
}
/*
4
1 1 2 1
*/

这程序好像有点Bug,我给组数据试试?

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 11988kb

input:

4
1 1 1 1

output:

14

result:

ok 1 number(s): "14"

Test #2:

score: 0
Accepted
time: 134ms
memory: 14056kb

input:

300
0 5 2 2 1 0 3 2 2 5 2 1 1 2 1 3 2 3 2 0 0 0 0 1 2 2 3 0 2 2 3 2 0 2 3 0 6 0 0 2 0 1 3 2 1 1 1 3 4 0 1 0 4 1 1 1 1 1 1 2 3 2 1 2 3 2 3 0 5 3 3 2 0 1 1 0 2 1 1 2 0 0 2 1 1 3 2 2 1 2 1 3 0 3 0 1 2 2 0 5 0 2 2 0 0 0 1 2 1 4 2 1 1 0 3 0 2 0 3 1 1 2 0 2 1 1 0 2 0 1 2 2 3 3 1 1 1 1 0 1 3 3 1 0 2 2 4 2 ...

output:

507010274

result:

ok 1 number(s): "507010274"

Test #3:

score: 0
Accepted
time: 162ms
memory: 14000kb

input:

500
1 1 0 2 1 0 2 3 2 0 0 2 0 2 1 1 0 0 1 1 1 2 1 1 1 0 1 1 2 2 1 4 0 2 1 0 2 3 1 0 1 1 0 2 1 2 2 1 0 0 3 1 4 1 1 2 1 1 0 1 3 1 2 0 0 0 2 1 2 0 0 3 2 1 1 1 1 1 2 1 0 1 0 0 0 1 0 0 2 1 1 0 1 0 1 0 1 0 0 1 0 1 1 0 0 0 1 0 0 0 2 1 1 0 1 1 0 1 1 0 0 1 0 3 1 3 0 0 2 0 0 1 1 0 0 0 0 0 1 0 0 0 0 0 1 2 0 0 ...

output:

7590964

result:

ok 1 number(s): "7590964"

Test #4:

score: 0
Accepted
time: 118ms
memory: 13992kb

input:

200
3 1 0 3 2 1 0 3 1 1 2 3 3 1 6 2 1 3 2 1 1 2 1 2 1 5 2 2 3 4 0 4 2 1 2 2 0 2 3 1 2 3 6 3 2 3 2 2 4 2 7 2 1 5 1 9 0 4 4 8 3 3 3 1 3 0 2 2 8 1 3 5 4 3 0 6 1 6 1 3 4 2 2 1 1 4 4 4 1 0 4 3 4 3 3 0 3 2 0 0 3 4 0 3 1 3 2 4 3 2 0 3 2 2 3 2 2 2 1 2 2 1 0 2 0 3 1 3 5 1 3 3 6 5 3 2 2 2 3 6 2 0 5 2 2 2 2 1 ...

output:

507844569

result:

ok 1 number(s): "507844569"

Test #5:

score: 0
Accepted
time: 31ms
memory: 13324kb

input:

100
4 8 2 5 4 4 3 0 2 7 2 3 4 4 1 2 3 4 4 4 3 3 3 3 3 2 4 1 3 5 5 1 4 6 1 1 1 3 2 3 2 1 0 1 4 4 2 4 2 5 3 5 1 6 2 3 3 1 4 4 4 1 4 4 3 4 2 0 2 3 6 1 3 3 5 4 1 1 2 3 0 3 2 2 1 3 3 2 5 6 3 2 3 3 5 4 2 3 4 4

output:

989550242

result:

ok 1 number(s): "989550242"

Test #6:

score: 0
Accepted
time: 1ms
memory: 10012kb

input:

1
1

output:

1

result:

ok 1 number(s): "1"

Test #7:

score: 0
Accepted
time: 1ms
memory: 9936kb

input:

500
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...

output:

1

result:

ok 1 number(s): "1"

Test #8:

score: 0
Accepted
time: 2ms
memory: 12088kb

input:

10
2 1 3 3 2 3 1 1 3 1

output:

165452340

result:

ok 1 number(s): "165452340"

Test #9:

score: 0
Accepted
time: 2ms
memory: 12132kb

input:

20
0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0

output:

2

result:

ok 1 number(s): "2"

Test #10:

score: 0
Accepted
time: 2ms
memory: 12076kb

input:

20
0 0 1 0 0 0 0 1 0 0 0 0 0 0 2 0 1 0 0 0

output:

28

result:

ok 1 number(s): "28"

Test #11:

score: 0
Accepted
time: 2ms
memory: 12164kb

input:

10
1 1 1 1 1 1 1 1 1 1

output:

16796

result:

ok 1 number(s): "16796"

Test #12:

score: 0
Accepted
time: 58ms
memory: 13144kb

input:

300
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

output:

431279497

result:

ok 1 number(s): "431279497"

Test #13:

score: 0
Accepted
time: 119ms
memory: 14000kb

input:

2
232 268

output:

929717758

result:

ok 1 number(s): "929717758"

Test #14:

score: 0
Accepted
time: 1ms
memory: 10080kb

input:

1
500

output:

1

result:

ok 1 number(s): "1"

Test #15:

score: 0
Accepted
time: 111ms
memory: 13948kb

input:

3
155 180 165

output:

911108550

result:

ok 1 number(s): "911108550"

Extra Test:

score: 0
Extra Test Passed