-
Notifications
You must be signed in to change notification settings - Fork 0
/
stackingnumbers.cpp
114 lines (93 loc) · 1.9 KB
/
stackingnumbers.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/**
* Australian Informatics Olympiad Senior 2003, Q2
* Ananya Kumar, 2011
* NUS High School
*/
#include <cstdio>
#include <algorithm>
using namespace std;
#define MAXN 32
#define MAXB 35000
int best[MAXB];
bool done[MAXN] = {false};
int cur[6];
int choice[MAXN];
int bestChoice[MAXN];
int s, b;
int highs = 0;
void recur ( int num )
{
if ( num == 5 )
{
int t1 = (4*choice[cur[1]]+6*choice[cur[2]]+4*choice[cur[3]]+choice[cur[4]])%b;
int t2 = (choice[cur[1]]+4*choice[cur[2]]+6*choice[cur[3]]+4*choice[cur[4]])%b;
int maxt = max(t1,t2);
int mint = min(t1,t2);
//printf("%d %d %d %d\n",choice[cur[1]],choice[cur[2]],choice[cur[3]],choice[cur[4]]);
int i, j;
i = best[mint];
while ( done[i] ) i = (i+s-1)%s;
done[i] = true;
j = best[maxt];
while ( done[j] ) j = (j+s-1)%s;
int curh = ((choice[i]+mint)%b) * ((choice[j]+maxt)%b);
if ( curh >= highs )
{
highs = curh;
copy(cur+1,cur+5,bestChoice+1);
if ( t1 == mint )
{
bestChoice[0] = i;
bestChoice[5] = j;
}
else
{
bestChoice[0] = j;
bestChoice[5] = i;
}
}
done[i] = false;
}
else
{
int maxs = s;
if ( num == 3 ) maxs = cur[2];
for ( int i = 0; i < maxs; i++ )
{
if ( done[i] ) continue;
done[i] = true;
cur[num] = i;
recur(num+1);
done[i] = false;
}
}
}
int main ()
{
FILE *in = fopen("stackin.txt","r");
FILE *out = fopen("stackout.txt","w");
fscanf(in,"%d",&b);
fscanf(in,"%d",&s);
int i, j, k;
for ( i = 0; i < s; i++ )
fscanf(in,"%d",&choice[i]);
sort(choice,choice+s);
//Precompute best array
for ( i = 0; i < b; i++ )
{
k = 0;
for ( j = 0; j < s; j++ )
{
if ( (i+choice[j])%b >= k )
{
k = (i+choice[j])%b;
best[i] = j;
}
}
//printf("%d %d\n",i,choice[best[i]]);
}
recur(1);
fprintf(out,"%d\n",highs);
for ( i = 0; i < 6; i++ ) fprintf(out,"%d ",choice[bestChoice[i]]);
fprintf(out,"\n");
}