-
Notifications
You must be signed in to change notification settings - Fork 3
/
10611-Theplayboychimp.cpp
executable file
·70 lines (55 loc) · 1.11 KB
/
10611-Theplayboychimp.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
//
// 10611 - The playboy chimp.cpp
// Uva
//
// Created by Alexander Faxå on 2012-02-20.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#include <iostream>
#include <stdio.h>
using namespace std;
int a[50001];
int n, q;
int b;
int lower_than()
{
int mmin = 0;
int mmax = n-1;
while(true)
{
int mid = (mmin + mmax)/2;
if(b > a[mid])
mmin = mid + 1;
else
mmax = mid - 1;
if(a[mid] == b || mmin > mmax)
return mid;
}
}
int main()
{
scanf("%d", &n);
for(int i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
scanf("%d", &q);
for (int i = 0; i < q; i++) {
scanf("%d", &b);
int lower = lower_than();
int upper = lower;
while(lower >= 0 && a[lower] >= b)
lower--;
while(upper < n && a[upper] <= b)
upper++;
if(lower == -1)
printf("X ");
else
printf("%d ", a[lower]);
if(upper >= n)
printf("X\n");
else
printf("%d\n", a[upper]);
}
return 0;
}