-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathminimum_rotated.cpp
More file actions
59 lines (54 loc) · 1019 Bytes
/
minimum_rotated.cpp
File metadata and controls
59 lines (54 loc) · 1019 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
/*
http://www.practice.geeksforgeeks.org/problem-page.php?pid=819
Minimum element in a sorted and rotated array
Divide and Conquer
Rodrigo Paes
*/
#include <bits/stdc++.h>
using namespace std;
#define MAX_N 500
int a[MAX_N];
int n;
int dq(int b, int e)
{
// printf("(%d, %d)\n", b, e);
if (e - b == 2)
{
return min(a[b], a[e-1]);
}
else if (e - b == 1)
{
return a[b];
}
int mid = (b + e) / 2;
// printf("a[%d]=%d\n", mid, a[mid]);
/*
Se o elemento do final do intervalo é maior que o atual,
significa que todos os elementos entre eles são maiores que o mid.
Logo a resposta só pode ser o próprio mid ou então alguém do lado esquerdo.
*/
if (a[mid] < a[e-1])
{
return dq(b, mid+1); // lado esquerdo. Perceba que incluí o próprio mid
}
else
{
return dq(mid + 1, e);
}
}
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
scanf("%d", &n);
// printf("\n\n####\n");
for (int i = 0; i < n; ++i)
{
scanf("%d", &a[i]);
}
printf("%d\n", dq(0,n));
}
return 0;
}