-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path561.cpp
136 lines (119 loc) · 3.28 KB
/
561.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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define TEST_CASE_561 2, 4, 3, 2, 6, 7
//#define SortMethod Quicksort
#define SortMethod bubblesort
class Solution
{
public:
int arrayPairSum(vector<int> &nums)
{
sort(begin(nums), end(nums));
int sum = 0;
for (int i = 0; i < nums.size(); ++i)
sum += nums[i++];
return sum;
}
};
template <int... Ints>
using int_sequence = integer_sequence<int, Ints...>;
template <int I, int... N>
constexpr auto catint(int_sequence<N...>)
{
return int_sequence<I, N...>();
}
template <int Head, int... I, int... N>
constexpr auto catsequence(int_sequence<I...>, int_sequence<N...>)
{
return int_sequence<I..., Head, N...>();
}
template <int Head, int Itop, int... I>
constexpr auto Smallerthan(int_sequence<Itop, I...>)
{
if constexpr (sizeof...(I) == 0)
{
if constexpr (Itop < Head)
return int_sequence<Itop>();
else
return int_sequence<>();
}
else
{
if constexpr (Itop < Head)
return catint<Itop>(Smallerthan<Head>(int_sequence<I...>()));
else
return Smallerthan<Head>(int_sequence<I...>());
}
}
template <int Head, int Itop, int... I>
constexpr auto Biggerthan(int_sequence<Itop, I...>)
{
if constexpr (sizeof...(I) == 0)
{
if constexpr (Itop >= Head)
return int_sequence<Itop>();
else
return int_sequence<>();
}
else
{
if constexpr (Itop >= Head)
return catint<Itop>(Biggerthan<Head>(int_sequence<I...>()));
else
return Biggerthan<Head>(int_sequence<I...>());
}
}
constexpr auto Quicksort(int_sequence<>) { return int_sequence<>(); }
template <int Itop, int... I>
constexpr auto Quicksort(int_sequence<Itop, I...>)
{
if constexpr (sizeof...(I) == 0)
return int_sequence<Itop>();
else
return catsequence<Itop>(Quicksort(Smallerthan<Itop>(int_sequence<I...>())), Quicksort(Biggerthan<Itop>(int_sequence<I...>())));
}
template <int Insert>
constexpr auto bubbleInsert(int_sequence<>)
{
return int_sequence<Insert>();
}
template <int Insert, int Itop, int... I>
constexpr auto bubbleInsert(int_sequence<Itop, I...>)
{
if constexpr (Itop > Insert)
return int_sequence<Insert, Itop, I...>();
else
return catint<Itop>(bubbleInsert<Insert>(int_sequence<I...>()));
}
constexpr auto bubblesort(int_sequence<>){return int_sequence<>();}
template <int Itop, int... I>
constexpr auto bubblesort(int_sequence<Itop, I...>)
{
return bubbleInsert<Itop>(bubblesort(int_sequence<I...>()));
}
template <int Ifirst, int Isecond, int... I>
constexpr int PairSum(int_sequence<Ifirst, Isecond, I...>)
{
if constexpr (sizeof...(I) == 0)
return Ifirst;
else
return Ifirst + PairSum(int_sequence<I...>());
}
template <int... I>
constexpr int arrayPairSum(int_sequence<I...> _)
{
return PairSum(SortMethod(_));
}
template <>
constexpr int arrayPairSum<>(int_sequence<>) { return 0; }
int main()
{
Solution s;
vector<int> test = {TEST_CASE_561};
cout << s.arrayPairSum(test) << endl;
auto template_test = int_sequence<TEST_CASE_561>();
auto template_result = arrayPairSum(template_test);
cout << template_result << endl;
}