-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet_Peak.cpp
More file actions
63 lines (56 loc) · 849 Bytes
/
Get_Peak.cpp
File metadata and controls
63 lines (56 loc) · 849 Bytes
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
#include "stdafx.h"
#include "iostream"
#include "vector"
#include "string"
#include "map"
#include <algorithm>
#include <stdlib.h>
#include <time.h>
#include <stack>
#include <sstream>
#include <iomanip>
#include <random>
#include <assert.h>
using namespace std;
int getPeak(int A[], int n)
{
if(!n)return -1;
if(n==1)return A[0];
int l=0,h=n-1;
while(l<=h)
{
if(l==h)
{
return A[l];
}
else if(l+1==h)
{
return max(A[l], A[h]);
}
else
{
int m=(l+h)/2;
if(A[l]<=A[m])l=m;
else h=m;
}
}
return -1;
}
void testGetPeak()
{
int A[100];
int k=34, n=57;
for(int i=0;i<n;i++)
{
if(i<k)A[i]=i;
else A[i]=n-i;
}
A[k]=k;
A[k+1]=34;
cout<<getPeak(A, 57)<<endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
testGetPeak();
return 0;
}