SHARE
TWEET

Untitled

a guest Dec 11th, 2019 76 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <fstream>
  4. using namespace std;
  5.  
  6. struct suma
  7. {
  8.     int x, y, z;
  9.     int sum;
  10. };
  11.  
  12. ifstream fin("loto.in");
  13. ofstream fout("loto.out");
  14.  
  15. //true daca a < b
  16. //false altfel
  17. bool cmp(suma a, suma b)
  18. {
  19.     return a.sum < b.sum;
  20. }
  21.  
  22. int main()
  23. {
  24.     int n,S,s,ki=0,val;
  25.     fin>>n>>S;
  26.     int v[101];
  27.  
  28.     suma sume[1000001];
  29.  
  30.     for(int i=1;i<=n;i++)
  31.         fin>>v[i];
  32.     for(int i=1;i<=n;i++)
  33.         for(int j=i;j<=n;j++)
  34.             for(int k=j;k<=n;k++)
  35.             {
  36.                 sume[ki].sum = v[i]+v[j]+v[k];
  37.                 sume[ki].x = v[i];
  38.                 sume[ki].y = v[j];
  39.                 sume[ki].z = v[k];
  40.                 ki++;
  41.             }
  42.  
  43.     sort(sume,sume+ki,cmp);
  44.  
  45.     bool ok = 0;
  46.     for(int i=0;i<ki;i++)
  47.     {
  48.         val=S-sume[i].sum;
  49.         int l=0,r=ki-1,m,sol=-1;
  50.         while(l<=r)
  51.         {
  52.             m=(l+r)/2;
  53.             if(sume[m].sum==val){sol=m;break;}
  54.             else if(sume[m].sum<val)l=m+1;
  55.             else if(sume[m].sum>val)r=m-1;
  56.         }
  57.         if(sol>=0)
  58.         {
  59.             ok = 1;
  60.             fout << sume[i].x << " " << sume[i].y << " " << sume[i].z << " ";
  61.             fout << sume[sol].x << " " << sume[sol].y << " " << sume[sol].z;
  62.             break;
  63.         }
  64.     }
  65.     if(!ok)
  66.         fout << -1;
  67.     return 0;
  68. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top