QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#855539#8228. 排序Meatherm100 ✓2716ms47748kbC++142.1kb2025-01-12 23:00:192025-01-12 23:00:26

Judging History

This is the latest submission verdict.

  • [2025-01-12 23:00:26]
  • Judged
  • Verdict: 100
  • Time: 2716ms
  • Memory: 47748kb
  • [2025-01-12 23:00:19]
  • Submitted

answer

# include <bits/stdc++.h>

const int N=35,INF=0x3f3f3f3f,mod=1e9+7;

inline int read(void){
	int res,f=1;
	char c;
	while((c=getchar())<'0'||c>'9')
		if(c=='-') f=-1;
	res=c-48;
	while((c=getchar())>='0'&&c<='9')
		res=res*10+c-48;
	return res*f;
}

int n,m,d[N];

inline int adc(int a,int b){
	return (a+b>=mod)?(a+b-mod):(a+b);
}

struct Node{
	int mx,sum;
	Node(int _a=0,int _b=0): mx(_a),sum(_b) {}
	inline Node operator + (const Node &rhs) const{
		if(mx<rhs.mx) return rhs;
		else if(mx>rhs.mx) return *this;
		return Node(mx,adc(sum,rhs.sum));
	}
};

std::unordered_map <int,Node> f;

// f[S]: 固定了 S 中元素的情况下, 产生逆序对的最大值, 以及方案数 
// 从大到小加入元素 

std::queue <int> q;

int t[N][N];
int suf[N][N][N];
int pre[N][N][N];

inline void calc(int s,int pos){
	assert(((s>>pos)&1)==0);
	
	int nw=0,t=s|(1<<pos);
	
	int cur=s;
	
	for(int i=1;i<=m;++i){
		int d=::d[i],npos=0,nex=0;
		for(int p=0;p<d;++p){
			if(pos%d==p) nw+=__builtin_popcount(pre[d][p][pos/d]&cur);
			int cnt=__builtin_popcount(::t[d][p]&cur); nex|=suf[d][p][cnt];
			if(pos%d==p) npos=p+(n-1-p)/d*d-cnt*d;
		}
		pos=npos,cur=nex;
//		printf("nw = %d pos = %d\n",nw,pos);
//		for(int i=0;i<n;++i) printf("%d ",v[i]); puts("");
	}

//	printf("%d -> %d = %d\n",s,t,f[s].mx+nw);
	
	if(f.count(t)) f[t]=Node(f[s].mx+nw,f[s].sum)+f[t];
	else f[t]=Node(f[s].mx+nw,f[s].sum),q.push(t);
	return;
}

int main(void){
	n=read(),m=read();
	for(int i=1;i<=m;++i) d[i]=read();
	
	for(int d=1;d<=n;++d){
		for(int x=0,y,c;x<d;++x){
			for(y=x,c=1;y<n;y+=d,++c) t[d][x]|=(1<<y),pre[d][x][c]=pre[d][x][c-1]|(1<<y);
			for(y-=d,c=1;y>=0;y-=d,++c) suf[d][x][c]=suf[d][x][c-1]|(1<<y);
		}
	}
	
	q.push(0); f[0]=Node(0,1);
	while(!q.empty()){
		int s=q.front();
		
//		printf("s = %d (mx = %d val = %d)\n",s,f[s].mx,f[s].sum);
//		for(int i=0;i<n;++i) printf("%d",(s>>i)&1);
//		puts("\n");
		
		q.pop();
		for(int p=0;p<d[1];++p){
			for(int x=p;x<n;x+=d[1]){
				if(((s>>x)&1)==0){
					calc(s,x);
					break;
				}
			}
		}
	}
	printf("%d %d",f[(1<<n)-1].mx,f[(1<<n)-1].sum);

	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 18
Accepted

Test #1:

score: 18
Accepted
time: 0ms
memory: 3836kb

input:

2 2
2 1

output:

1 1

result:

ok 2 number(s): "1 1"

Test #2:

score: 18
Accepted
time: 0ms
memory: 3752kb

input:

5 4
5 4 2 1

output:

6 4

result:

ok 2 number(s): "6 4"

Test #3:

score: 18
Accepted
time: 1ms
memory: 4132kb

input:

8 4
6 3 2 1

output:

15 4

result:

ok 2 number(s): "15 4"

Test #4:

score: 18
Accepted
time: 1ms
memory: 3880kb

input:

8 6
8 7 5 4 2 1

output:

14 2

result:

ok 2 number(s): "14 2"

Test #5:

score: 18
Accepted
time: 1ms
memory: 3936kb

input:

8 3
8 7 1

output:

22 7

result:

ok 2 number(s): "22 7"

Test #6:

score: 18
Accepted
time: 1ms
memory: 3844kb

input:

8 1
1

output:

28 1

result:

ok 2 number(s): "28 1"

Subtask #2:

score: 27
Accepted

Dependency #1:

100%
Accepted

Test #7:

score: 27
Accepted
time: 0ms
memory: 4300kb

input:

16 2
6 1

output:

77 15

result:

ok 2 number(s): "77 15"

Test #8:

score: 27
Accepted
time: 19ms
memory: 4420kb

input:

16 8
10 9 8 7 6 5 4 1

output:

57 5

result:

ok 2 number(s): "57 5"

Test #9:

score: 27
Accepted
time: 21ms
memory: 4312kb

input:

16 10
10 9 8 7 6 5 4 3 2 1

output:

57 3

result:

ok 2 number(s): "57 3"

Test #10:

score: 27
Accepted
time: 17ms
memory: 4384kb

input:

16 7
10 9 8 6 5 4 1

output:

49 1

result:

ok 2 number(s): "49 1"

Test #11:

score: 27
Accepted
time: 3ms
memory: 4020kb

input:

16 4
7 6 2 1

output:

52 9

result:

ok 2 number(s): "52 9"

Subtask #3:

score: 21
Accepted

Dependency #2:

100%
Accepted

Test #12:

score: 21
Accepted
time: 0ms
memory: 4100kb

input:

22 3
5 3 1

output:

100 1

result:

ok 2 number(s): "100 1"

Test #13:

score: 21
Accepted
time: 1ms
memory: 4032kb

input:

22 1
1

output:

231 1

result:

ok 2 number(s): "231 1"

Test #14:

score: 21
Accepted
time: 104ms
memory: 8184kb

input:

22 4
10 8 3 1

output:

97 4

result:

ok 2 number(s): "97 4"

Test #15:

score: 21
Accepted
time: 109ms
memory: 8056kb

input:

22 5
10 7 6 3 1

output:

92 70

result:

ok 2 number(s): "92 70"

Test #16:

score: 21
Accepted
time: 153ms
memory: 8132kb

input:

22 6
10 9 8 7 3 1

output:

97 1

result:

ok 2 number(s): "97 1"

Test #17:

score: 21
Accepted
time: 218ms
memory: 8104kb

input:

22 10
10 9 8 7 6 5 4 3 2 1

output:

109 1

result:

ok 2 number(s): "109 1"

Subtask #4:

score: 10
Accepted

Test #18:

score: 10
Accepted
time: 3ms
memory: 4068kb

input:

14 2
10 1

output:

61 210

result:

ok 2 number(s): "61 210"

Test #19:

score: 10
Accepted
time: 1ms
memory: 4036kb

input:

18 2
2 1

output:

117 1

result:

ok 2 number(s): "117 1"

Test #20:

score: 10
Accepted
time: 376ms
memory: 25028kb

input:

30 2
9 1

output:

264 84

result:

ok 2 number(s): "264 84"

Test #21:

score: 10
Accepted
time: 289ms
memory: 22796kb

input:

29 2
9 1

output:

253 36

result:

ok 2 number(s): "253 36"

Test #22:

score: 10
Accepted
time: 40ms
memory: 6628kb

input:

25 2
8 1

output:

195 8

result:

ok 2 number(s): "195 8"

Subtask #5:

score: 24
Accepted

Dependency #1:

100%
Accepted

Dependency #2:

100%
Accepted

Dependency #3:

100%
Accepted

Dependency #4:

100%
Accepted

Test #23:

score: 24
Accepted
time: 1569ms
memory: 47748kb

input:

30 4
10 9 5 1

output:

188 40

result:

ok 2 number(s): "188 40"

Test #24:

score: 24
Accepted
time: 2622ms
memory: 47544kb

input:

30 9
10 9 8 7 6 5 4 3 1

output:

184 6

result:

ok 2 number(s): "184 6"

Test #25:

score: 24
Accepted
time: 2349ms
memory: 47704kb

input:

30 8
10 9 8 7 4 3 2 1

output:

154 1

result:

ok 2 number(s): "154 1"

Test #26:

score: 24
Accepted
time: 2348ms
memory: 47576kb

input:

30 8
10 8 7 6 5 4 3 1

output:

155 1

result:

ok 2 number(s): "155 1"

Test #27:

score: 24
Accepted
time: 1881ms
memory: 47572kb

input:

30 6
10 8 6 4 3 1

output:

150 4

result:

ok 2 number(s): "150 4"

Test #28:

score: 24
Accepted
time: 2716ms
memory: 47632kb

input:

30 10
10 9 8 7 6 5 4 3 2 1

output:

184 6

result:

ok 2 number(s): "184 6"

Test #29:

score: 24
Accepted
time: 1411ms
memory: 42880kb

input:

29 6
10 9 7 5 3 1

output:

129 200

result:

ok 2 number(s): "129 200"

Extra Test:

score: 0
Extra Test Passed