-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstress_test.cpp
More file actions
96 lines (86 loc) · 2.07 KB
/
stress_test.cpp
File metadata and controls
96 lines (86 loc) · 2.07 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
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
#include <cstdlib>
#include <iostream>
#include <vector>
using std::cin;
using std::cout;
using std::vector;
long long MaxPairwiseProduct(const std::vector<int>& numbers)
{
long long result = 0;
int n = numbers.size();
for (int i = 0; i < n; ++i)
{
for (int j = i + 1; j < n; ++j)
{
long long tmp = ((long long)numbers[i]) * numbers[j];
if (tmp > result)
{
result = tmp;
}
}
}
return result;
}
long long MaxPairwiseProductFast(const std::vector<int>& numbers)
{
int max_index1 = -1;
for (int i = 0; i < numbers.size(); ++i)
{
if (max_index1 == -1 || numbers[i] > numbers[max_index1])
{
max_index1 = i;
}
}
int max_index2 = -1;
for (int j = 0; j < numbers.size(); ++j)
{
if ((j != max_index1) && ((max_index2 == -1) || numbers[j] > numbers[max_index2]))
{
max_index2 = j;
}
}
return ((long long) (numbers[max_index1])) * numbers[max_index2];
}
int main() {
/*
while (true)
{
int n = rand() % 100 + 2;
cout << n << "\n";
vector<int> a;
for (int i = 0; i < n; ++i)
{
a.push_back(rand() % 100000);
}
for (int i = 0; i < n; ++i)
{
cout << a[i] << ' ';
}
cout << "\n";
long long res1 = MaxPairwiseProduct(a);
long long res2 = MaxPairwiseProductFast(a);
if (res1 != res2)
{
cout << "Wrong answer: " << res1 << ' ' << res2 << "\n";
break;
}
else
{
cout << "OK\n";
}
}*/
int len;
cin >> len;
if (len < 2)
return 1;
std::vector<int> numbers(len);
for (int n=0; n < len; ++n)
{
cin >> numbers[n];
}
//long long result = MaxPairwiseProduct(numbers);
long long result = MaxPairwiseProductFast(numbers);
//long long result = MaxPairwiseProductFast(std::vector<int>(100000, 0 ));
cout << result << "\n";
return 0;
}