Advertisement
LucasLima

Exact Sum - UVa 11057

Nov 8th, 2012
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.70 KB | None | 0 0
  1. #include <cstdio>
  2. #include <iostream>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. #define ST first
  8. #define ND second
  9. typedef pair<int,int> PII;
  10. #define REP(i,n) for(int i=0;i<(n);i++)
  11.  
  12. int A[10010];
  13.  
  14. int main () {
  15.     int n;
  16.     while(scanf("%d",&n) == 1) {
  17.         REP(i,n) scanf("%d",&A[i]);
  18.         int m;
  19.         scanf("%d",&m);
  20.         sort(A,A+n);
  21.         int aux = 1<<30;
  22.         PII sol;
  23.         REP(i,n) {
  24.             bool b = binary_search(A+i+1,A+n,m-A[i]);
  25.             if(b) {
  26.                 int pos = lower_bound(A+i+1,A+n,m-A[i])-A;
  27.                 if(abs(A[pos] - A[i]) < aux) {
  28.                     aux = A[pos]-A[i];
  29.                     sol.ST = A[i]; sol.ND = A[pos];
  30.                 }
  31.             }
  32.         }
  33.         printf("Peter should buy books whose prices are %d and %d.\n\n",sol.ST,sol.ND);
  34.     }
  35.     return 0;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement