Advertisement
Pridexs

PECAS - METODO 1

Jun 13th, 2014
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.50 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string>
  5. #include <utility>
  6. #include <algorithm>
  7. #include <vector>
  8. #include <map>
  9. #include <math.h>
  10.  
  11. using namespace std;
  12.  
  13. int compare (const void *a, const void *b) {
  14.     return ( *(int*)a - *(int*)b );
  15. }
  16.  
  17. int main()
  18. {
  19.     int x, n;
  20.     int cent[1000001];
  21.     int cont;
  22.     int cont2;
  23.     int menor, maior;
  24.     int size;
  25.  
  26.     int diferenca;
  27.     bool achou;
  28.  
  29.     while (scanf ("%d %d", &x, &n) != EOF) {
  30.         x *= 10000000;
  31.         size = 0;
  32.         cont2 = 0;
  33.         achou = false;
  34.         menor = 0;
  35.         maior = 0;
  36.         for (int i = 0; i < n; i++) {
  37.             scanf("%d", &cont);
  38.  
  39.             if (cont < x) {
  40.                 cent[i-cont2] = cont;
  41.             } else {
  42.                 cont++;
  43.             }
  44.         }
  45.  
  46.         qsort(cent, n-cont2, sizeof(int), compare);
  47.         size = (n-cont2)/2;
  48.         for (int i = 0; i < size; i++) {
  49.             for (int j = (n-cont2)-1; j > i+1; j--) {
  50.                 if (cent[i] + cent[j] < x) {
  51.                     break;
  52.                 } else if (cent[i] + cent[j] == x) {
  53.                     menor = cent[i];
  54.                     maior = cent[j];
  55.                     achou = true;
  56.                     break;
  57.                 }
  58.             }
  59.             if (achou) break;
  60.         }
  61.  
  62.         if (!menor && !maior) {
  63.             printf("PERIGO\n");
  64.         } else {
  65.             printf("SIM %d %d\n", menor, maior);
  66.         }
  67.     }
  68.     return 0;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement