Advertisement
STANAANDREY

partition array in2 subarrays with min dif

Dec 15th, 2020
1,069
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.86 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. #define NMAX 100000
  4. int n, v[NMAX], p[NMAX];
  5.  
  6. void read() {
  7.     cin >> n;
  8.     for (int i = 1; i <= n; i++)
  9.         cin >> v[i];
  10. }
  11.  
  12. void sort() {
  13.     for (int i = 1; i < n; i++)
  14.         for (int j = i + 1; j <= n; j++)
  15.             if (v[j] < v[i])
  16.                 swap(v[j], v[i]);
  17. }
  18.  
  19. void greedy() {
  20.     int sum = 0;
  21.     for (int i = 1; i <= n; i++)
  22.         sum += v[i];
  23.  
  24.     sum /= 2;
  25.     for (int i = 1; i <= n; i++)
  26.         if (sum - v[i] >= 0) {
  27.             p[i] = 1;
  28.             sum -= p[i];
  29.         }
  30. }
  31.  
  32. void tipar() {
  33.     for (int i = 1; i <= n; i++)
  34.         if (p[i])
  35.             cout << v[i] << ' ';
  36.     cout << endl;
  37.     for (int i = 1; i <= n; i++)
  38.         if (!p[i])
  39.             cout << v[i] << ' ';
  40. }
  41.  
  42. int main() {
  43.     read();
  44.     sort();
  45.     greedy();
  46.     tipar();
  47.     return 0;
  48. }
  49.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement