Advertisement
PerryTheWizz

PbMatRara

Apr 6th, 2020
601
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.37 KB | None | 0 0
  1. #include <iostream>
  2. struct trip{
  3.     int i,j,v;
  4. };
  5. bool cmp(trip i1, trip i2)
  6. {
  7.     if(i1.i<i2.i)
  8.     {
  9.         return 1;
  10.     }
  11.     else
  12.     {
  13.         if(i1.i==i2.i&&i1.j<i2.j)
  14.         {
  15.             return 1;
  16.         }
  17.     }
  18.     return 0;
  19. }
  20. void ins(trip X[], int &n, trip toIns)
  21. {
  22.     int ok=0;
  23.     X[n].i=-20;///Rezolva cazul cand ar fi cel mai mic element din sir
  24.     n++;
  25.     int it=0;
  26.     while(X[it].i>toIns.i)
  27.     it++;
  28.     while(X[it].i==toIns.i&&X[it].j>toIns.j)
  29.     it++;
  30.     for(;it<n;it++)
  31.     {
  32.         std::swap(X[it],toIns);
  33.     }
  34. }
  35. int partition (trip arr[], int low, int high)  
  36. {  
  37.     trip pivot = arr[high]; // pivot  
  38.     int i = (low - 1); // Index of smaller element  
  39.  
  40.     for (int j = low; j <= high - 1; j++)  
  41.     {  
  42.         // If current element is smaller than the pivot  
  43.         if (cmp(arr[j],pivot))  
  44.         {  
  45.             i++; // increment index of smaller element  
  46.             std::swap(arr[i], arr[j]);  
  47.         }  
  48.     }  
  49.     std::swap(arr[i + 1], arr[high]);  
  50.     return (i + 1);  
  51. }  
  52.  
  53. /* The main function that implements QuickSort  
  54. arr[] --> Array to be sorted,  
  55. low --> Starting index,  
  56. high --> Ending index */
  57. void quickSort(trip arr[], int low, int high)  
  58. {  
  59.     if (low < high)  
  60.     {  
  61.         /* pi is partitioning index, arr[p] is now  
  62.         at right place */
  63.         int pi = partition(arr, low, high);  
  64.  
  65.         // Separately sort elements before  
  66.         // partition and after partition  
  67.         quickSort(arr, low, pi - 1);  
  68.         quickSort(arr, pi + 1, high);  
  69.     }  
  70. }  
  71. int getVal(trip X[],int i, int j)
  72. {
  73.     int it=0;
  74.     while(X[it].i>i)
  75.     it++;
  76.     while(X[it].i==i&&X[it].j>j)
  77.     it++;
  78.     if(X[it].i!=i||X[it].j!=j)
  79.     return 0;
  80.     else
  81.     {
  82.         return X[it].v;
  83.     }
  84. }
  85. void readRare(trip X[],int &n)
  86. {
  87.     n=0;
  88.     while(1)
  89.     {
  90.         int x,y,v;
  91.         std::cin>>x>>y>>v;
  92.         if(x==-1 && y==-1 && v==-1)
  93.         {
  94.         quickSort(X,0,n-1);
  95.         return;
  96.         }
  97.         else
  98.         {
  99.         X[n].i=x;
  100.         X[n].j=y;
  101.         X[n].v=v;
  102.         n++;
  103.         }
  104.     }
  105. }
  106. void cpy(trip &a, trip b)
  107. {
  108.     a.i=b.i;
  109.     a.j=b.j;
  110.     a.v=b.v;
  111. }
  112. void sumRare(trip A[],int a, trip B[],int b, trip C[], int &c)
  113. {
  114.     c=0;
  115.     int ia=0,ib=0;
  116.     while(ia<a&&ib<b)
  117.     {
  118.     if(cmp(A[ia],B[ib]))//A[i]<B[ib]
  119.     {
  120.         cpy(C[c++],A[ia++]);
  121.     }
  122.     else if(cmp(B[ib],A[ia]))//B[ib]<A[ia]
  123.     {
  124.         cpy(C[c++],B[ib++]);
  125.     }
  126.     else//A[ia]==B[ib]
  127.     {
  128.         cpy(C[c],A[ia]);
  129.         C[c].v+=B[ib].v;
  130.         ia++,ib++,c++;
  131.     }
  132.     }
  133.     while(ia<a)
  134.     {
  135.         cpy(C[c++],A[ia++]);
  136.     }
  137.     while(ib<b)
  138.     {
  139.         cpy(C[c++],B[ib++]);
  140.     }
  141. }
  142. void prtRare(trip A[],int a,int n, int m )
  143. {
  144.     int ai=0;
  145.     for(int i=1;i<=n;i++)
  146.     {
  147.         for(int j=1;j<=m;j++)
  148.         {
  149.             if((A[ai].i==i&&A[ai].j==j)&&ai<a)
  150.             {
  151.                 std::cout<<A[ai].v;
  152.                 ai++;
  153.             }
  154.             else
  155.             {
  156.                 std::cout<<"0";
  157.             }
  158.             std::cout<<" ";
  159.         }
  160.         std::cout<<"\n";
  161.     }
  162. }
  163.  
  164. int main() {
  165.     trip A[200],B[200];
  166.     int a,m,n,b;
  167.     std::cin>>n>>m;
  168.     readRare(A,a);
  169.     readRare(B,b);
  170.     trip C[200];
  171.     int c;
  172.     sumRare(A,a,B,b,C,c);
  173.     prtRare(C,c,n,m);
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement