-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathscara1.cpp
64 lines (64 loc) · 1.2 KB
/
scara1.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
# include <fstream>
# include <iomanip>
# include <algorithm>
using namespace std;
ifstream f("scara2.in");
ofstream g("scara2.out");
int i,j,n,m,p,hact,H;
int st[100],ap[100],solmin[100];
double sol[100],minn;
double efort ()
{
int i,j;
double var,S;
for (i=1; i<=n; ++i)
{
var=sol[i-1]+st[i];
S=st[i];
for (j=i-1; j>=1 && S+st[j]<=m; --j)
{
S+=st[j];
var=min(var,sol[j-1]+S/(i-j+1)+p);
}
sol[i]=var;
}
return sol[n];
}
void back (int k)
{
if (k==n+1)
{
if (hact==H)
{
double ef;
ef=efort();
if (ef<minn && minn-ef>0.001)
{
minn=ef;
for (int i=1; i<=n; ++i)
solmin[i]=st[i];
}
}
}
else
{
for (int i=1; i<=m && hact+i<=H; ++i)
{
if (ap[i]) continue;
st[k]=i; hact+=i; ap[i]=1;
back(k+1);
st[k]=0; hact-=i; ap[i]=0;
}
}
}
int main ()
{
f>>H>>n>>m>>p;
minn=H*m;
back(1);
g<<fixed<<setprecision(2)<<minn<<"\n";
for (i=1; i<=n; ++i)
g<<solmin[i]<<" ";
g<<"\n";
return 0;
}