-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUVA 11057.cpp
More file actions
50 lines (40 loc) · 774 Bytes
/
UVA 11057.cpp
File metadata and controls
50 lines (40 loc) · 774 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
#include <bits/stdc++.h>
using namespace std;
const int N=100001;
int n;
int arr[N];
int main() {
//freopen("input.txt","r",stdin);
while(scanf("%d",&n)==1){
int p;
int arr[n];
for(int i=1; i<=n; i++){
cin>>arr[i];
}
cin>>p;
sort(arr+1,arr+n+1);
int ans = numeric_limits<int>::max();;
for(int i=1; i<=n; i++){
int sub = p-arr[i];
// int ans1 = -1;
if(sub<arr[i]){
break;
}
else{
int low = i+1;
int high = n;
while(high>=low){
int mid = (high+low)/2;
if(arr[mid]==sub){
ans = min(ans,sub);
break;
}
else if(arr[mid]<sub) low = mid+1;
else high = mid-1;
}
}
}
// cout<<<<" "<<p-ans<<endl;
printf("Peter should buy books whose prices are %d and %d.\n\n",p-ans,ans);
}
}