QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#358709 | #8285. Shell Sort | PPP# | TL | 4163ms | 11220kb | C++17 | 3.4kb | 2024-03-19 22:57:25 | 2024-03-19 22:57:26 |
Judging History
answer
//#ifdef DEBUG
//#define _GLIBCXX_DEBUG
//#endif
#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
typedef long long ll;
typedef long double ld;
const int maxN = 35;
const int maxM = 15;
int n, m;
int d[maxM];
const int mod = (int)1e9 + 7;
int sum(int a, int b) {
int s = a + b;
if (s >= mod) s -= mod;
return s;
}
int sub(int a, int b) {
int s = a - b;
if (s < 0) s += mod;
return s;
}
int mult(int a, int b) {
return (1LL * a * b) % mod;
}
int ini_array[maxN];
int cnt_chain[15];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#ifdef DEBUG
freopen("input.txt", "r", stdin);
#endif
cin >> n >> m;
for (int i = 0; i < m; i++) {
cin >> d[i];
}
vector<vector<int>> chains(d[0]);
for (int z = 0; z < d[0]; z++) {
for (int x = z; x < n; x += d[0]) {
chains[z].emplace_back(x);
}
}
int add = 0;
for (int x = 0; x < d[0]; x++) {
add += (chains[x].size() * (chains[x].size() - 1)) / 2;
}
// cout << add << " ????? " << endl;
int states = 1;
for (int x = 0; x < d[0]; x++) {
states *= (chains[x].size() + 1);
}
// cout << states << " ??? " << endl;
vector<pair<int,int>> P(states, make_pair(-1, 0));
vector<int> suf_prod(d[0]);
suf_prod[0] = 1;
for (int i = 1; i < d[0]; i++) {
suf_prod[i] = suf_prod[i - 1] * (chains[i - 1].size() + 1);
}
P[0] = {add, 1};
// cout << " gg " << endl;
for (int i = 0; i < states; i++) {
int cur = i;
for (int x = 0; x < d[0]; x++) {
cnt_chain[x] = (cur % (chains[x].size() + 1));
cur /= (chains[x].size() + 1);
}
for (int x = 0; x < d[0]; x++) {
if (cnt_chain[x] < (int)chains[x].size()) {
for (int u = 0; u < d[0]; u++) {
for (int g = 0; g < cnt_chain[u]; g++) {
ini_array[chains[u][g]] = 0;
}
for (int g = cnt_chain[u]; g < (int)chains[u].size(); g++) {
ini_array[chains[u][g]] = 2;
}
}
ini_array[chains[x][cnt_chain[x]]] = 1;
int new_state = suf_prod[x] + i;
int T = 0;
for (int u = 1; u < m; u++) {
int f = d[u];
for (int p = 0; p < f; p++) {
for (int X = p; X < n; X += f) {
for (int Y = X + f; Y < n; Y += f) {
if (ini_array[X] > ini_array[Y]) {
if (ini_array[X] == 1 && ini_array[Y] == 0) {
T += 1;
}
swap(ini_array[X], ini_array[Y]);
}
}
}
}
}
if (P[new_state].first < P[i].first + T) {
P[new_state] = {P[i].first + T, P[i].second};
}
else if (P[new_state].first == P[i].first + T) {
P[new_state].second = sum(P[new_state].second, P[i].second);
}
}
}
}
cout << P.back().first << " " << P.back().second << '\n';
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3468kb
input:
2 2 2 1
output:
1 1
result:
ok 2 number(s): "1 1"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3580kb
input:
5 4 5 4 2 1
output:
6 4
result:
ok 2 number(s): "6 4"
Test #3:
score: 0
Accepted
time: 1ms
memory: 3584kb
input:
8 4 6 3 2 1
output:
15 4
result:
ok 2 number(s): "15 4"
Test #4:
score: 0
Accepted
time: 1ms
memory: 3540kb
input:
8 6 8 7 5 4 2 1
output:
14 2
result:
ok 2 number(s): "14 2"
Test #5:
score: 0
Accepted
time: 1ms
memory: 3784kb
input:
8 3 8 7 1
output:
22 7
result:
ok 2 number(s): "22 7"
Test #6:
score: 0
Accepted
time: 0ms
memory: 3544kb
input:
8 1 1
output:
28 1
result:
ok 2 number(s): "28 1"
Test #7:
score: 0
Accepted
time: 0ms
memory: 3548kb
input:
16 2 6 1
output:
77 15
result:
ok 2 number(s): "77 15"
Test #8:
score: 0
Accepted
time: 23ms
memory: 3624kb
input:
16 8 10 9 8 7 6 5 4 1
output:
57 5
result:
ok 2 number(s): "57 5"
Test #9:
score: 0
Accepted
time: 31ms
memory: 3700kb
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: 0
Accepted
time: 22ms
memory: 3584kb
input:
16 7 10 9 8 6 5 4 1
output:
49 1
result:
ok 2 number(s): "49 1"
Test #11:
score: 0
Accepted
time: 5ms
memory: 3788kb
input:
16 4 7 6 2 1
output:
52 9
result:
ok 2 number(s): "52 9"
Test #12:
score: 0
Accepted
time: 6ms
memory: 3812kb
input:
22 3 5 3 1
output:
100 1
result:
ok 2 number(s): "100 1"
Test #13:
score: 0
Accepted
time: 1ms
memory: 3832kb
input:
22 1 1
output:
231 1
result:
ok 2 number(s): "231 1"
Test #14:
score: 0
Accepted
time: 271ms
memory: 3776kb
input:
22 4 10 8 3 1
output:
97 4
result:
ok 2 number(s): "97 4"
Test #15:
score: 0
Accepted
time: 308ms
memory: 3960kb
input:
22 5 10 7 6 3 1
output:
92 70
result:
ok 2 number(s): "92 70"
Test #16:
score: 0
Accepted
time: 324ms
memory: 3868kb
input:
22 6 10 9 8 7 3 1
output:
97 1
result:
ok 2 number(s): "97 1"
Test #17:
score: 0
Accepted
time: 506ms
memory: 3844kb
input:
22 10 10 9 8 7 6 5 4 3 2 1
output:
109 1
result:
ok 2 number(s): "109 1"
Test #18:
score: 0
Accepted
time: 5ms
memory: 3620kb
input:
14 2 10 1
output:
61 210
result:
ok 2 number(s): "61 210"
Test #19:
score: 0
Accepted
time: 0ms
memory: 3528kb
input:
18 2 2 1
output:
117 1
result:
ok 2 number(s): "117 1"
Test #20:
score: 0
Accepted
time: 1349ms
memory: 6964kb
input:
30 2 9 1
output:
264 84
result:
ok 2 number(s): "264 84"
Test #21:
score: 0
Accepted
time: 1001ms
memory: 6184kb
input:
29 2 9 1
output:
253 36
result:
ok 2 number(s): "253 36"
Test #22:
score: 0
Accepted
time: 147ms
memory: 3832kb
input:
25 2 8 1
output:
195 8
result:
ok 2 number(s): "195 8"
Test #23:
score: 0
Accepted
time: 4163ms
memory: 11220kb
input:
30 4 10 9 5 1
output:
188 40
result:
ok 2 number(s): "188 40"
Test #24:
score: -100
Time Limit Exceeded
input:
30 9 10 9 8 7 6 5 4 3 1