forked from Autoratch/Thailand-Olympiad-in-Informatics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoi12_peak.cpp
48 lines (37 loc) · 962 Bytes
/
toi12_peak.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
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define MOD 1e9 + 7
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
int n,k;
cin >> n >> k;
vector<int> a(n);
for(int i = 0;i < n;i++) cin >> a[i];
priority_queue<int> q;
for(int i = 0;i < n;i++)
{
if(i==0)
{
if(a[i]>a[i+1]) q.push(a[i]);
continue;
}
if(i==n-1)
{
if(a[i]>a[i-1]) q.push(a[i]);
continue;
}
if(a[i]>a[i-1] and a[i]>a[i+1]) q.push(a[i]);
}
if(q.empty()){ cout << "-1"; return 0; }
vector<int> ans;
int m = 0,p = 0;
while(!q.empty())
{
if(q.top()!=p){ ans.push_back(q.top()); m++; if(m==k) break; p = q.top(); }
q.pop();
}
if(m<k) for(int i = ans.size()-1;i >= 0;i--) cout << ans[i] << endl;
else for(int i = 0;i < ans.size();i++){ if(i==k) return 0; cout << ans[i] << endl; }
}