-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathzad4_ms.cpp
142 lines (130 loc) · 3.37 KB
/
zad4_ms.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// T(n) = O(nlogn)
// M(n) = O(n)
// https://math.stackexchange.com/questions/3974110/sorting-an-array-to-get-the-maximum-combined-sum-of-the-differences-between-ever
std::vector<float> przekładaniec(const std::vector<float> x)
{
vector<float> sorted(x.size());
copy(x.begin(), x.end(), sorted.begin());
sort(sorted.begin(), sorted.end());
vector<float> results(x.size());
if (x.size() % 2 == 0)
{
bool ascending = true;
int smallestI = (x.size() / 2) - 1, biggestI = x.size() / 2;
for (int i = 0; i < x.size() / 2; i++)
{
if (ascending)
{
results[i] = sorted[smallestI--];
results[x.size() - 1 - i] = sorted[biggestI++];
}
else
{
results[i] = sorted[biggestI++];
results[x.size() - 1 - i] = sorted[smallestI--];
}
ascending = !ascending;
}
}
else{
bool shouldPutSmallest = true;
int smallestI = 0, biggestI = x.size() - 1 - 1;
int pivot = x.size()/2;
results[pivot] = sorted[x.size()-1];
for (int i = 1; i <= pivot; i++)
{
if (shouldPutSmallest)
{
results[pivot-i] = sorted[smallestI++];
results[pivot+i] = sorted[smallestI++];
}
else
{
results[pivot-i] = sorted[biggestI--];
results[pivot+i] = sorted[biggestI--];
}
shouldPutSmallest = !shouldPutSmallest;
}
}
return results;
}
float sum(const std::vector<float> x)
{
float sum = 0;
for (int i = 1; i < x.size(); i++)
{
sum += abs(x[i - 1] - x[i]);
}
return sum;
}
// T(n) = O(n!)
// M(n) = O(n)
std::vector<float> przekładaniec_brut(const std::vector<float> x)
{
vector<float> perm(x.size());
copy(x.begin(), x.end(), perm.begin());
sort(perm.begin(), perm.end());
float max = 0;
vector<float> results(x.size());
while (next_permutation(perm.begin(), perm.end()))
{
float currentSum = sum(perm);
if (currentSum > max)
{
max = currentSum;
copy(perm.begin(), perm.end(), results.begin());
}
}
return results;
}
vector<float> randomVector()
{
srand((unsigned)time(NULL));
int length = 0;
while (length <= 3)
{
length = rand() % 10;
}
vector<float> ret;
for (int i = 0; i < length; i++)
{
float nextNum = (rand() % 10000) / 100.0;
ret.push_back(nextNum);
}
return ret;
}
int main()
{
int n;
cin >> n;
vector<float> x;
for (int i = 0; i < n; i++)
{
float input;
cin >> input;
x.push_back(input);
}
// vector<float> x = randomVector();
vector<float> bestPerm = przekładaniec(x);
//int sumBest = sum(bestPerm);
for (float a : bestPerm)
{
cout << a << endl;
}
// vector<float> brutPerm = przekładaniec_brut(x);
// int sumBrut = sum(brutPerm);
// if (sumBest != sumBrut)
// {
// cout << "bad :(" << endl;
// for (float a : brutPerm)
// {
// cout << a << endl;
// }
// cout << endl
// << sumBest << " != " << sumBrut << endl;
// }
}