#include "bartender.h"
using namespace std;
typedef long long lld;
static int seq[22], scn;
static lld fac=1, su, s4, s3;
// 4 1 3 2 5 => 0 0 1 1 4 => 0*5*4*3*2 + 0*5*4*3 + 1*5*4 + 1*5 + 4 => a * 3^12 + b
vector<int> BlendWines(int K, vector<int> R){
int N=R.size();
vector<int> A;
if(N <= 12){
for(int i=0; i<N; i++) A.push_back(1);
return A;
}
for(int i=0; i<N; i++){
if(R[i] > 12) seq[scn++] = R[i];
}
for(int i=1; i<=scn; i++) fac*=i;
for(int i=0; i<scn; i++){
int c=0;
for(int j=0; j<i; j++){
if(seq[i]>seq[j]) c++;
}
su += fac*c;
fac /= i+2;
}
s4 = su/531441, s3 = su%531441;
for(int i=0; i<N; i++){
if(R[i] <= 12) A.push_back(s3%3+1), s3/=3;
else A.push_back(s4%4+4), s4/=4;
}
return A;
}
#include "taster.h"
#include <algorithm>
#include <string>
using namespace std;
int N, ba[22], bcn;
int comp(int a, int b){ // if a<b, 1.
if(a>N || b>N) return a<b;
return Compare(a-1,b-1) < 0;
}
vector<string> ss;
void dfs(int ix, int chk, string s){
if(ix==10){
ss.push_back(s);
return;
}
int mi=0;
if(ix>=5) mi=s[ix-5]-'0'+1;
else if(ix) mi=s[ix-1]-'0'+1;
for(int i=mi; i<=9; i++){
if(chk&(1<<i)) continue;
dfs(ix+1, chk|(1<<i), s+char('0'+i));
}
}
int cmd[9999][2]; string res[9999];
int getdepth(vector<string> v, int ix, int req){
if(v.size() <= 1){
if(v.size() == 1) res[ix] = v[0];
return 1;
}
if((int)v.size() > (1<<req)) return 0;
vector<string> v1, v2;
for(int i=0; i<10; i++){
for(int j=i+1; j<10; j++){
v1.clear(), v2.clear();
for(auto &s: v){
if(s[i]<s[j]) v1.push_back(s);
else v2.push_back(s);
}
if(getdepth(v1, ix*2, req-1) && getdepth(v2, ix*2+1, req-1)){
cmd[ix][0]=i, cmd[ix][1]=j;
return 1;
}
}
}
return 0;
}
void ins(int *v, int sz){
int mi=0, mx=sz-1, md, cu=sz;
while(mi<=mx){
md=(mi+mx)/2;
if(comp(v[sz],v[md])) cu=md, mx=md-1;
else mi=md+1;
}
reverse(v+cu, v+sz+1);
reverse(v+cu+1, v+sz+1);
}
void swap2(int *v, int ix1, int ix2){
swap(v[ix1], v[ix2]);
swap(v[ix1+5], v[ix2+5]);
}
void smsort(int *v, int sz){
if(sz == 5){
if(!comp(v[0], v[1])) swap2(v, 0, 1);
if(!comp(v[2], v[3])) swap2(v, 2, 3);
if(!comp(v[0], v[2])) swap2(v, 0, 2), swap2(v, 1, 3);
if(comp(v[2], v[4])){
if(!comp(v[3], v[4])) swap2(v, 3, 4);
// a b cde
if(comp(v[1], v[3])){
if(!comp(v[1], v[2])) swap2(v, 1, 2);
}
else{
swap2(v, 1, 2); swap2(v, 2, 3); // acd b e
if(!comp(v[3], v[4])) swap2(v, 3, 4);
}
}
else{
swap2(v, 3, 4); swap2(v, 2, 3);
if(comp(v[0], v[2])){
// a b ecd
if(comp(v[1], v[3])){
if(!comp(v[1], v[2])) swap2(v, 1, 2);
}
else{
swap2(v, 1, 2); swap2(v, 2, 3); // aec b d
if(!comp(v[3], v[4])) swap2(v, 3, 4);
}
}
else{
swap2(v, 1, 2), swap2(v, 0, 1); // ea b cd
if(!comp(v[2], v[3])){
swap2(v, 2, 3);
if(!comp(v[3], v[4])) swap2(v, 3, 4);
}
}
}
}
if(sz == 10){
for(int i=0; i<5; i++){
if(!comp(v[i],v[i+5])) swap(v[i],v[i+5]);
}
smsort(v,5);
int st=1;
while(cmd[st][0]!=cmd[st][1]){
if(comp(v[cmd[st][0]], v[cmd[st][1]])) st=st*2;
else st=st*2+1;
}
string x = res[st];
for(int i=0; i<9; i++){
int j;
for(j=i; j<10; j++){
if(x[j]==i+'0') break;
}
swap(v[i],v[j]), swap(x[i],x[j]);
}
}
if(sz == 12) smsort(v,10), ins(v,10), ins(v,11);
}
typedef long long lld;
static lld su, s4, s3, p4=1, p3=1;
static int seq[22], scn, fs[22];
vector<int> SortWines(int K, vector<int> A) {
dfs(0,0,"");
getdepth(ss,1,10);
vector<int> R;
N = A.size();
if(N <= 12){
for(int i=1; i<=12; i++) ba[i-1]=i;
smsort(ba, 12);
R.resize(N);
for(int i=0; i<N; i++) R[ba[i]-1]=i+1;
return R;
}
for(int i=0; i<N; i++){
if(A[i]<=3) s3 += (A[i]-1)*p3, p3*=3, ba[bcn++]=i+1;
else s4 += (A[i]-4)*p4, p4*=4, scn++;
}
su = s4*531441 + s3;
smsort(ba, 12);
R.resize(N);
for(int i=0; i<12; i++) R[ba[i]-1]=i+1;
for(int i=scn-1; i>=0; i--) fs[i]=su%(i+1), su/=i+1;
for(int i=0; i<scn; i++){
int a;
for(a=scn-1; a>=0; a--){
if(!fs[a]) break;
}
seq[a]=13+i;
for(int j=a; j<scn; j++) fs[j]--;
}
scn=0;
for(int i=0; i<N; i++){
if(A[i]>3) R[i]=seq[scn++];
}
return R;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
253 ms |
1292 KB |
Correct |
2 |
Correct |
223 ms |
1312 KB |
Correct |
3 |
Correct |
247 ms |
1296 KB |
Correct |
4 |
Correct |
231 ms |
1280 KB |
Correct |
5 |
Correct |
224 ms |
1520 KB |
Correct |
6 |
Correct |
244 ms |
1404 KB |
Correct |
7 |
Correct |
228 ms |
1480 KB |
Correct |
8 |
Correct |
209 ms |
1284 KB |
Correct |
9 |
Correct |
235 ms |
1432 KB |
Correct |
10 |
Correct |
216 ms |
1296 KB |
Correct |
11 |
Correct |
209 ms |
1436 KB |
Correct |
12 |
Correct |
217 ms |
1288 KB |
Correct |
13 |
Correct |
221 ms |
1424 KB |
Correct |
14 |
Correct |
218 ms |
1296 KB |
Correct |
15 |
Correct |
234 ms |
1284 KB |
Correct |
16 |
Correct |
225 ms |
1476 KB |
Correct |
17 |
Correct |
221 ms |
1352 KB |
Correct |
18 |
Correct |
208 ms |
1452 KB |
Correct |
19 |
Correct |
233 ms |
1292 KB |
Correct |
20 |
Correct |
225 ms |
1416 KB |
Correct |
21 |
Correct |
218 ms |
1200 KB |
Correct |
22 |
Correct |
213 ms |
1404 KB |
Correct |
23 |
Correct |
213 ms |
1404 KB |
Correct |
24 |
Correct |
220 ms |
1380 KB |
Correct |
25 |
Correct |
216 ms |
1452 KB |
Correct |
26 |
Correct |
217 ms |
1292 KB |
Correct |
27 |
Correct |
218 ms |
1428 KB |
Correct |
28 |
Correct |
221 ms |
1368 KB |
Correct |
29 |
Correct |
219 ms |
1448 KB |
Correct |
30 |
Correct |
226 ms |
1420 KB |
Correct |
31 |
Correct |
210 ms |
1292 KB |
Correct |
32 |
Correct |
216 ms |
1428 KB |
Correct |
33 |
Correct |
224 ms |
1448 KB |
Correct |
34 |
Correct |
212 ms |
1300 KB |
Correct |
35 |
Correct |
216 ms |
1268 KB |
Correct |
36 |
Correct |
215 ms |
1372 KB |
Correct |
37 |
Correct |
214 ms |
1468 KB |
Correct |
38 |
Correct |
233 ms |
1328 KB |
Correct |
39 |
Correct |
236 ms |
1420 KB |
Correct |
40 |
Correct |
210 ms |
1208 KB |
Correct |
41 |
Correct |
259 ms |
1304 KB |
Correct |
42 |
Correct |
214 ms |
1364 KB |
Correct |
43 |
Correct |
206 ms |
1284 KB |
Correct |
44 |
Correct |
227 ms |
1404 KB |
Correct |
45 |
Correct |
210 ms |
1284 KB |
Correct |
46 |
Correct |
210 ms |
1156 KB |
Correct |
47 |
Correct |
218 ms |
1424 KB |
Correct |
48 |
Correct |
211 ms |
1460 KB |
Correct |
49 |
Correct |
208 ms |
1284 KB |
Correct |
50 |
Correct |
230 ms |
1448 KB |
Correct |
51 |
Correct |
220 ms |
1156 KB |
Correct |
52 |
Correct |
207 ms |
1300 KB |
Correct |
53 |
Correct |
215 ms |
1436 KB |
Correct |
54 |
Correct |
218 ms |
1404 KB |
Correct |
55 |
Correct |
204 ms |
1156 KB |
Correct |
56 |
Correct |
227 ms |
1204 KB |
Correct |
57 |
Correct |
247 ms |
1268 KB |
Correct |
58 |
Correct |
233 ms |
1420 KB |
Correct |
59 |
Correct |
221 ms |
1508 KB |
Correct |
60 |
Correct |
223 ms |
1292 KB |
Correct |
61 |
Correct |
207 ms |
1420 KB |
Correct |
62 |
Correct |
212 ms |
1308 KB |
Correct |
63 |
Correct |
212 ms |
1284 KB |
Correct |
64 |
Correct |
206 ms |
1436 KB |
Correct |
65 |
Correct |
216 ms |
1440 KB |
Correct |
66 |
Correct |
211 ms |
1244 KB |
Correct |
67 |
Correct |
211 ms |
1504 KB |
Correct |
68 |
Correct |
208 ms |
1544 KB |
Correct |
69 |
Correct |
202 ms |
1292 KB |
Correct |
70 |
Correct |
207 ms |
1412 KB |
Correct |
71 |
Correct |
218 ms |
1420 KB |
Correct |
72 |
Correct |
212 ms |
1420 KB |
Correct |
73 |
Correct |
216 ms |
1308 KB |
Correct |
74 |
Correct |
214 ms |
1212 KB |
Correct |
75 |
Correct |
212 ms |
1284 KB |
Correct |
76 |
Correct |
206 ms |
1348 KB |
Correct |
77 |
Correct |
211 ms |
1428 KB |
Correct |
78 |
Correct |
210 ms |
1284 KB |
Correct |
79 |
Correct |
214 ms |
1284 KB |
Correct |
80 |
Correct |
210 ms |
1284 KB |
Correct |
81 |
Correct |
208 ms |
1420 KB |
Correct |
82 |
Correct |
209 ms |
1420 KB |
Correct |
83 |
Correct |
210 ms |
1412 KB |
Correct |
84 |
Correct |
214 ms |
1344 KB |
Correct |
85 |
Correct |
215 ms |
1424 KB |
Correct |
86 |
Correct |
212 ms |
1284 KB |
Correct |
87 |
Correct |
208 ms |
1404 KB |
Correct |
88 |
Correct |
207 ms |
1428 KB |
Correct |
89 |
Correct |
212 ms |
1304 KB |
Correct |
90 |
Correct |
212 ms |
1284 KB |
Correct |
91 |
Correct |
208 ms |
1292 KB |
Correct |
92 |
Correct |
208 ms |
1288 KB |
Correct |
93 |
Correct |
213 ms |
1316 KB |
Correct |
94 |
Correct |
213 ms |
1292 KB |
Correct |
95 |
Correct |
207 ms |
1156 KB |
Correct |
96 |
Correct |
212 ms |
1428 KB |
Correct |
97 |
Correct |
212 ms |
1216 KB |
Correct |
98 |
Correct |
209 ms |
1208 KB |
Correct |
99 |
Correct |
235 ms |
1240 KB |
Correct |
100 |
Correct |
204 ms |
1284 KB |
Correct |
101 |
Correct |
204 ms |
1204 KB |
Correct |
102 |
Correct |
207 ms |
1592 KB |
Correct |
103 |
Correct |
205 ms |
1156 KB |
Correct |
104 |
Correct |
204 ms |
1300 KB |
Correct |
105 |
Correct |
206 ms |
1540 KB |
Correct |
106 |
Correct |
211 ms |
1532 KB |
Correct |
107 |
Correct |
199 ms |
1420 KB |
Correct |
108 |
Correct |
210 ms |
1440 KB |
Correct |
109 |
Correct |
204 ms |
1284 KB |
Correct |
110 |
Correct |
212 ms |
1156 KB |
Correct |
111 |
Correct |
212 ms |
1560 KB |
Correct |
112 |
Correct |
208 ms |
1420 KB |
Correct |
113 |
Correct |
204 ms |
1284 KB |
Correct |
114 |
Correct |
211 ms |
1440 KB |
Correct |
115 |
Correct |
213 ms |
1288 KB |
Correct |
116 |
Correct |
206 ms |
1292 KB |
Correct |
117 |
Correct |
216 ms |
1344 KB |
Correct |
118 |
Correct |
211 ms |
1420 KB |
Correct |
119 |
Correct |
214 ms |
1324 KB |
Correct |
120 |
Correct |
217 ms |
1156 KB |
Correct |