-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
66 lines (57 loc) · 1.4 KB
/
main.cpp
File metadata and controls
66 lines (57 loc) · 1.4 KB
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
#include <iostream>
#include <vector>
using std::cin;
using std::cout;
long long MaxPairwiseProduct(const std::vector<int>& input)
{
long long result = 0;
int n = input.size();
for (int i = 0; i < n; ++i)
{
for (int j = i + 1; j < n; ++j)
{
auto tmp = ((long long)input[i]) * input[j];
if (tmp > result)
{
result = tmp;
}
}
}
return result;
}
long long MaxPairwiseProductFast(const std::vector<int>& input)
{
int max_index1 = -1;
for (int i = 0; i < input.size(); ++i)
{
if (max_index1 == -1 || input[i] > input[max_index1])
{
max_index1 = i;
}
}
int max_index2 = -1;
for (int j = 0; j < input.size(); ++j)
{
if ((max_index2 == -1 || input[j] > input[max_index2]) && (input[j] != input[max_index1]))
{
max_index2 = j;
}
}
return ((long long) (input[max_index1])) * input[max_index2];
}
int main() {
int len;
cin >> len;
if (len < 2)
return 1;
std::vector<int> input(len);
for (int n=0; n < len; ++n)
{
cin >> input[n];
}
//long long result = MaxPairwiseProduct(input);
long long result = MaxPairwiseProductFast(input);
//long long result = MaxPairwiseProductFast(std::vector<int>(100000, 0 ));
cout << "result: " << result << "\n";
return 0;
}