This repository has been archived by the owner on Jun 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
2nd_min.cpp
76 lines (73 loc) · 1.64 KB
/
2nd_min.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
#include <algorithm>
#include <iostream>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <list>
using namespace std;
int get_second_min(stack<int> &s)
{
// write only in this function, do not declare static
priority_queue<int, vector<int>, greater<int>> pq;
stack<int> tmp = s;
while (tmp.size())
{
pq.push(tmp.top());
tmp.pop();
}
int m = pq.top();
while (pq.top() == m)
{
pq.pop();
}
return pq.top();
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<int> v(n);
for (int i = 0; i < n; i++)
{
cin >> v[i];
}
// repeat n-1 times
for (int last = 1; last < n; last += 1)
{
stack<int> s;
// build s;
bool distinct = false;
for (int i = last; i >= 0; i--)
{
s.push(v[i]);
if (v[i] != v[0])
distinct = true;
}
cout << "--use v[" << last << "] to v[0] --"
<< "\n";
if (distinct)
{
// call get_second_min if we have at least 2 distinct value
int answer = get_second_min(s);
// print result and s
cout << "result is " << answer << "\n";
cout << "size of s is " << s.size() << "\n"
<< "member of s are ";
while (s.size() > 0)
{
cout << s.top() << " ";
s.pop();
}
cout << "\n";
}
else
{
cout << "skip because s has only one value\n\n\n";
}
}
return 0;
}