Guest User

Untitled

a guest
Jul 19th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.74 KB | None | 0 0
  1. #include<iostream>
  2. #include<algorithm>
  3. #include<stdio.h>
  4. #include<math.h>
  5. #include<set>
  6. #include<vector>
  7. #include<string.h>
  8. #include<string>
  9. using namespace std;
  10.  
  11. int arrstart[14326];
  12. int arrend[14326];
  13.  
  14. int a[14326];
  15.  
  16. bool assert(bool t)
  17. {
  18.     if (!t)
  19.         throw 42;
  20.     return true;
  21. }
  22.  
  23. int cntOst(int a, int b)
  24. {
  25.     int res = a%b;
  26.     if (res < 0)
  27.         res += b;
  28.     return res;
  29. }
  30.  
  31. int main()
  32. {
  33. #ifdef _DEBUG
  34.     freopen("in.txt","r",stdin);
  35. #endif
  36.  
  37.     int m;
  38.     cin >> m;
  39.  
  40.     int zeroCnt = 0;
  41.     int ost = -1;
  42.     int sum1=0,sum2=0;
  43.  
  44.     for (int i=0; i<m; i++)
  45.     {
  46.         cin >> arrstart[i];
  47.         sum1 += arrstart[i];
  48.         if (arrstart[i] == 0)
  49.             zeroCnt++;
  50.     }
  51.  
  52.     if (zeroCnt > 1)
  53.     {
  54.         cout << "-1";
  55.         return 0;
  56.     }
  57.  
  58.     for (int j=0; j<m; j++)
  59.     {
  60.         cin >> arrend[j];
  61.         sum2 += arrend[j];
  62.     }
  63.  
  64.     if (sum1 != sum2)
  65.     {
  66.         cout << "-1";
  67.         return 0;
  68.     }
  69.  
  70.     int maxdiff = 0;
  71.  
  72.     for (int i=0; i<m; i++)
  73.     {
  74.         int curr = cntOst(arrend[i]-arrstart[i],m);
  75.         if (arrend[i]-arrstart[i] < maxdiff)
  76.             maxdiff = arrend[i]-arrstart[i];
  77.         if (ost == -1)
  78.             ost = curr;
  79.         else
  80.         {
  81.             if (ost != curr)
  82.             {
  83.                 cout << "-1";
  84.                 return 0;
  85.             }
  86.         }
  87.     }
  88.  
  89.     maxdiff *= -1;
  90.  
  91.     int N = m - ost;
  92.     while(N == 0 || N < maxdiff)
  93.         N += m;
  94.  
  95.     for (int i=0; i<m; i++)
  96.     {
  97.         a[i] = (arrend[i] - arrstart[i] + N) / m;
  98.     }
  99.  
  100.     cout << N << endl;
  101.    
  102.     for (int i=0; i<N; i++)
  103.     {
  104.         bool minus = true;
  105.  
  106.         for (int j=0; j<m; j++)
  107.         {
  108.             if (arrstart[j] == 0)
  109.             {
  110.                 arrstart[j] += (m-1);
  111.                 a[j]--;
  112.                 minus = false;
  113.                 cout << (j+1) << " ";
  114.             }
  115.             else
  116.             {
  117.                 arrstart[j]--;
  118.             }
  119.         }
  120.  
  121.         if (minus)
  122.         {
  123.             int w=0;
  124.             while (a[w] == 0 && assert(a[w] >= 0))
  125.                 w++;
  126.             a[w]--;
  127.             arrstart[w] += m;
  128.             cout << w+1 << " ";
  129.         }
  130.  
  131.     }
  132.  
  133.     return 0;
  134. }
Add Comment
Please, Sign In to add comment