#include <iostream> #include <string> #include <algorithm> using namespace std; bool desc(int i,int j) { return (i>j); } void buildTree(int a[], int i, int j, int n) { if (a[i] > 0) { cout << i+1 << " " << j+1 << endl; a[i]--; a[j]--; buildTree(a, i, j+1, n); } if (i<n) { buildTree(a, i+1, j, n); } } int a[1000000]; static void getPodSolution() { long long ch = 0; long long n = 2; cin >> n; long long sum = 0; for (long long i = 0; i < n ; i++) { cin >> a[i]; sum += a[i]; } long long k = sum / 2; long long msum = (sum % 2); long long diff = (n - k - 1); sort(a, a+n, desc); int i = n-1; while ((sum % 2 != 0) && (diff != 0) && i >= 0) { if (diff > 0) { a[i]++; sum++; } else if (diff < 0) { if ((diff + a[i]/2) <= 0) { sum-=a[i]; n--; } else { ch++; a[i]--; sum--; } } i--; } if (sum % 2 != 0) { a[n-1]--; ch++; } cout << ch << endl; buildTree(a, 0, 1, n); } int main(int argc, char** argv) { getPodSolution(); return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | #include <iostream> #include <string> #include <algorithm> using namespace std; bool desc(int i,int j) { return (i>j); } void buildTree(int a[], int i, int j, int n) { if (a[i] > 0) { cout << i+1 << " " << j+1 << endl; a[i]--; a[j]--; buildTree(a, i, j+1, n); } if (i<n) { buildTree(a, i+1, j, n); } } int a[1000000]; static void getPodSolution() { long long ch = 0; long long n = 2; cin >> n; long long sum = 0; for (long long i = 0; i < n ; i++) { cin >> a[i]; sum += a[i]; } long long k = sum / 2; long long msum = (sum % 2); long long diff = (n - k - 1); sort(a, a+n, desc); int i = n-1; while ((sum % 2 != 0) && (diff != 0) && i >= 0) { if (diff > 0) { a[i]++; sum++; } else if (diff < 0) { if ((diff + a[i]/2) <= 0) { sum-=a[i]; n--; } else { ch++; a[i]--; sum--; } } i--; } if (sum % 2 != 0) { a[n-1]--; ch++; } cout << ch << endl; buildTree(a, 0, 1, n); } int main(int argc, char** argv) { getPodSolution(); return 0; } |