#include <iostream> #include <algorithm> #include <vector> std::vector< std::pair<int, int> > edges; int free_pos = 1; void v_to_tree(int v, std::vector<int> & degrees) { while(degrees[v]) { degrees[v]--; degrees[free_pos]--; edges.push_back(std::make_pair(v, free_pos)); v_to_tree(free_pos++, degrees); } } int main() { std::ios_base::sync_with_stdio(false); int best_change = 2, n; std::cin >> n; std::vector<int> best{1, 1}, A(n + 1), pref_A(n + 1, 0); for(int i = 0; i < n; i++) std::cin >> A[i + 1]; std::sort(A.begin() + 1, A.end()); for(int i = 1; i < n + 1; i++) pref_A[i] = A[i] + pref_A[i - 1]; for(int v_count = 2; v_count <= n; v_count++) { if(v_count > 2) break; int needed_degree = 2 * v_count - 2; int a = 1, b = n + 1 - v_count, c; while(a < b) { c = (a + b) / 2; if(pref_A[c + v_count - 1] - pref_A[c - 1] < needed_degree) a = c + 1; else b = c; } /* std::cout << a << " here \n"; for(int k = 0; k < 10; k++) std::cout << pref_A[k] << " "; std::cout << "\n"; */ int diff = pref_A[a + v_count - 1] - pref_A[a - 1]; if(diff == needed_degree) { best_change = 0; best = std::vector<int>(A.begin() + a, A.begin() + a + v_count); } else if(diff > needed_degree) { if(best_change == 2 && A[a + v_count - 1] > (diff - needed_degree)) { best_change = 1; best = std::vector<int>(A.begin() + a, A.begin() + a + v_count); best[best.size() - 1] -= (diff - needed_degree); } } else { if(best_change == 2) { best_change = 1; best = std::vector<int>(A.begin() + a, A.begin() + a + v_count); best[0] -= (diff - needed_degree); } } if(a > 1) { diff = pref_A[a + v_count - 2] - pref_A[a - 2]; if(best_change == 2) { best_change = 1; best = std::vector<int>(A.begin() + a - 1, A.begin() + a + v_count - 1); best[0] -= (diff - needed_degree); } } } std::sort(best.begin(), best.end()); std::reverse(best.begin(), best.end()); /* std::cout << best_change << "\n"; std::cout << best.size() << "\n"; for(int i = 0; i < best.size(); i++) std::cout << best[i] << " "; std::cout << std::endl; */ v_to_tree(0, best); std::cout << best_change << "\n"; std::cout << best.size() << "\n"; for(int i = 0; i < edges.size(); i++) std::cout << edges[i].first + 1 << " " << edges[i].second + 1 << "\n"; 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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | #include <iostream> #include <algorithm> #include <vector> std::vector< std::pair<int, int> > edges; int free_pos = 1; void v_to_tree(int v, std::vector<int> & degrees) { while(degrees[v]) { degrees[v]--; degrees[free_pos]--; edges.push_back(std::make_pair(v, free_pos)); v_to_tree(free_pos++, degrees); } } int main() { std::ios_base::sync_with_stdio(false); int best_change = 2, n; std::cin >> n; std::vector<int> best{1, 1}, A(n + 1), pref_A(n + 1, 0); for(int i = 0; i < n; i++) std::cin >> A[i + 1]; std::sort(A.begin() + 1, A.end()); for(int i = 1; i < n + 1; i++) pref_A[i] = A[i] + pref_A[i - 1]; for(int v_count = 2; v_count <= n; v_count++) { if(v_count > 2) break; int needed_degree = 2 * v_count - 2; int a = 1, b = n + 1 - v_count, c; while(a < b) { c = (a + b) / 2; if(pref_A[c + v_count - 1] - pref_A[c - 1] < needed_degree) a = c + 1; else b = c; } /* std::cout << a << " here \n"; for(int k = 0; k < 10; k++) std::cout << pref_A[k] << " "; std::cout << "\n"; */ int diff = pref_A[a + v_count - 1] - pref_A[a - 1]; if(diff == needed_degree) { best_change = 0; best = std::vector<int>(A.begin() + a, A.begin() + a + v_count); } else if(diff > needed_degree) { if(best_change == 2 && A[a + v_count - 1] > (diff - needed_degree)) { best_change = 1; best = std::vector<int>(A.begin() + a, A.begin() + a + v_count); best[best.size() - 1] -= (diff - needed_degree); } } else { if(best_change == 2) { best_change = 1; best = std::vector<int>(A.begin() + a, A.begin() + a + v_count); best[0] -= (diff - needed_degree); } } if(a > 1) { diff = pref_A[a + v_count - 2] - pref_A[a - 2]; if(best_change == 2) { best_change = 1; best = std::vector<int>(A.begin() + a - 1, A.begin() + a + v_count - 1); best[0] -= (diff - needed_degree); } } } std::sort(best.begin(), best.end()); std::reverse(best.begin(), best.end()); /* std::cout << best_change << "\n"; std::cout << best.size() << "\n"; for(int i = 0; i < best.size(); i++) std::cout << best[i] << " "; std::cout << std::endl; */ v_to_tree(0, best); std::cout << best_change << "\n"; std::cout << best.size() << "\n"; for(int i = 0; i < edges.size(); i++) std::cout << edges[i].first + 1 << " " << edges[i].second + 1 << "\n"; return 0; } |