QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#844408#8228. 排序juan_123100 ✓4167ms49144kbC++142.8kb2025-01-05 21:14:502025-01-05 21:14:52

Judging History

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

  • [2025-01-05 21:14:52]
  • 评测
  • 测评结果:100
  • 用时:4167ms
  • 内存:49144kb
  • [2025-01-05 21:14:50]
  • 提交

answer

/*
请问如何想到从小到大插入。
*/
#include<bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
unordered_map<int,pair<int,int> >mp;
//用 map 维护出现情况,优先队列维护转移顺序
priority_queue<int,vector<int>,greater<int> >q;
int n,m;
int d[35];
int a[35];
int calc(int j){
	//计算 j 点被交换的次数
//	for(int i = 0;i<n;i++)cout << a[i] << " ";cout << endl;
	int pos = j,ans = 0;
	for(int i = 1;i<=m;i++){
		int tt = 0,c = 0,st = pos%d[i];
		/*
		for(int j =0;j<d[i];j++){
			for(int k =j;k<n;k+=d[i]){
				for(int l = k;l!=j and a[l]>a[l-d[i]];l-=d[i]){
					swap(a[l],a[l-d[i]]);
					if(a[l]==0 or a[l-d[i]]==0)++ans;
				}
			}
		}
		*/
		for(int j =0;j<d[i];j++){
			if(j == st)continue;
		//	cout << "  " << j << endl;
			int tt =0,c = 0;
			int now = j;
			while(now<n){tt += (a[now]==1);now+=d[i];}
			now = j;
			while(now<n){
				++c;
				if(c<=tt)a[now] = 1;
				else a[now] = -1;
				now += d[i];
			}
		}
		for(int j = st;j<n;j+=d[i]){
		//	cout << j << " " << a[j] << " " << pos << endl;
			if(a[j]==1){
				++tt;
				ans+=(j>pos);
			}
			if(a[j]==-1){
				ans+=(j<pos);
			}
		}
		//cout << ans << endl;
		while(st<n){
			++c;
			if(c <= tt)a[st] = 1;
			else{
				if(c == tt+1)a[st] = 0,pos = st;
				else a[st] = -1;
			}	
			st += d[i];
		}
	//	for(int i =0;i<n;i++)cout << " " << a[i];cout << endl;
		
	}
//	for(int i =0;i<n;i++)cout << a[i] << " ";cout << endl;
	for(int i =1;i<n;i++)assert(a[i]<=a[i-1]);
//	cout << ans << endl;
	return ans;
}
signed main(){
	cin >> n >> m;
	for(int i = 1;i<=m;i++)cin >> d[i];
	for(int i = 0;i<d[1];i++){
		int cc = 0;
		for(int j =0;j<n;j++)a[j] = 1;a[i] = 0;
		mp[(1<<i)] = {calc(i),1};
		q.push(1<<i);
	}
	while(!q.empty()){
		auto e = q.top();q.pop();
		int S = e;auto ee = mp[S];int mx = ee.first,cnt = ee.second;
//		cout << " " << S << " " << mx << " " << cnt << endl;
		for(int i =0;i<d[1];i++){
			for(int j = i;j<n;j+=d[1]){
				if(!(S>>j&1)){
	//				cout << j << endl;
					//往 j 里塞 i
					for(int i = 0;i<n;i++){
						if(S>>i&1)a[i] = -1;
						else a[i] = 1;
					}
					a[j] = 0;
					int to = S|(1<<j),mmx = mx+calc(j);
					if(!mp.count(to)){
						q.push(to);
						mp[to] = {mmx,cnt};
					}else{
						auto e = mp[to];
						if(e.first == mmx){
							e.second = (e.second+cnt);
							if(e.second>=mod)e.second-=mod;
						}else if(e.first < mmx){
							e.first = mmx,e.second = cnt;
						}
						mp[to] = e;
					}
					break;
				}
			}
		}
	}
	int tt =0 ;
	for(int i =0;i<d[1];i++){
		int cc =0 ;
		for(int j = i;j<n;j+=d[1])cc++;
		tt = tt+cc*(cc-1)/2;
	}
	auto e = mp[(1<<n)-1];
	int mx = e.first,cnt = e.second;
	cout << mx/2 << " " << cnt << endl;
}/*
30 9
10 9 8 7 6 5 4 3 1
*/

详细

Subtask #1:

score: 18
Accepted

Test #1:

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

input:

2 2
2 1

output:

1 1

result:

ok 2 number(s): "1 1"

Test #2:

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

input:

5 4
5 4 2 1

output:

6 4

result:

ok 2 number(s): "6 4"

Test #3:

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

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: 3620kb

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: 0ms
memory: 3552kb

input:

8 3
8 7 1

output:

22 7

result:

ok 2 number(s): "22 7"

Test #6:

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

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: 2ms
memory: 3684kb

input:

16 2
6 1

output:

77 15

result:

ok 2 number(s): "77 15"

Test #8:

score: 27
Accepted
time: 20ms
memory: 3948kb

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: 22ms
memory: 4004kb

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: 3944kb

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: 4ms
memory: 3976kb

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: 2ms
memory: 3996kb

input:

22 3
5 3 1

output:

100 1

result:

ok 2 number(s): "100 1"

Test #13:

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

input:

22 1
1

output:

231 1

result:

ok 2 number(s): "231 1"

Test #14:

score: 21
Accepted
time: 160ms
memory: 8124kb

input:

22 4
10 8 3 1

output:

97 4

result:

ok 2 number(s): "97 4"

Test #15:

score: 21
Accepted
time: 180ms
memory: 8052kb

input:

22 5
10 7 6 3 1

output:

92 70

result:

ok 2 number(s): "92 70"

Test #16:

score: 21
Accepted
time: 197ms
memory: 7988kb

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: 282ms
memory: 8064kb

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: 5ms
memory: 3828kb

input:

14 2
10 1

output:

61 210

result:

ok 2 number(s): "61 210"

Test #19:

score: 10
Accepted
time: 0ms
memory: 3520kb

input:

18 2
2 1

output:

117 1

result:

ok 2 number(s): "117 1"

Test #20:

score: 10
Accepted
time: 782ms
memory: 25704kb

input:

30 2
9 1

output:

264 84

result:

ok 2 number(s): "264 84"

Test #21:

score: 10
Accepted
time: 618ms
memory: 23096kb

input:

29 2
9 1

output:

253 36

result:

ok 2 number(s): "253 36"

Test #22:

score: 10
Accepted
time: 99ms
memory: 6332kb

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: 2333ms
memory: 49084kb

input:

30 4
10 9 5 1

output:

188 40

result:

ok 2 number(s): "188 40"

Test #24:

score: 24
Accepted
time: 3806ms
memory: 48980kb

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: 3418ms
memory: 49144kb

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: 3551ms
memory: 49128kb

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: 2919ms
memory: 49068kb

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: 4167ms
memory: 49144kb

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: 2102ms
memory: 43472kb

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