fork(1) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. // Speed
  5. #define fast_io ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
  6. // Typedefs
  7. #define int long long
  8. #define pb push_back
  9. #define ff first
  10. #define ss second
  11. #define all(x) (x).begin(), (x).end()
  12. #define sz(x) ((int)(x).size())
  13. #define endl '\n'
  14.  
  15. void solve() {
  16. int n, m;
  17. cin >> n >> m;
  18. vector<pair<int, int>> a(n);
  19. int sum_others = 0;
  20.  
  21. for(int i = 0; i < n; i++) {
  22. cin >> a[i].ff;
  23. a[i].ss = i + 1; // Store original 1-based index
  24. }
  25.  
  26. // Sort elves by value (smallest to largest)
  27. sort(all(a));
  28.  
  29. // --- CASE 1: We want 0 survivors ---
  30. if (m == 0) {
  31. int king_idx = n - 1;
  32. int king_hp = a[king_idx].ff;
  33.  
  34. // Calculate damage potential from all others
  35. int total_dmg = 0;
  36. for(int i = 0; i < n - 1; i++) total_dmg += a[i].ff;
  37.  
  38. if (total_dmg < king_hp) {
  39. cout << -1 << endl;
  40. return;
  41. }
  42.  
  43. vector<pair<int, int>> moves;
  44. // Smallest elves attack the King until King dies
  45. for(int i = 0; i < n - 1; i++) {
  46. moves.pb({a[i].ss, a[king_idx].ss});
  47. king_hp -= a[i].ff; // King takes damage
  48. if (king_hp <= 0) break; // Stop if King is dead
  49. }
  50.  
  51. cout << sz(moves) << endl;
  52. for(auto p : moves) cout << p.ff << " " << p.ss << endl;
  53. return;
  54. }
  55.  
  56. // --- CASE 2: We want m survivors ---
  57.  
  58. // We need m victims for m survivors to safely "attack" and lock themselves.
  59. // Total needed: m (Survivors) + m (Victims) = 2m.
  60. if (n < 2 * m) {
  61. cout << -1 << endl;
  62. return;
  63. }
  64.  
  65. // Groups logic (based on sorted array):
  66. // Trash: Indices 0 to (n - 2m - 1)
  67. // Targets: Indices (n - 2m) to (n - m - 1)
  68. // Survivors: Indices (n - m) to (n - 1)
  69.  
  70. vector<pair<int, int>> moves;
  71.  
  72. // 1. Process Trash (Chain them to get rid of them)
  73. // Trash 0 -> Trash 1 -> ... -> Trash Last -> Target 0
  74. int trash_count = n - 2 * m;
  75. int first_target_idx = n - 2 * m;
  76.  
  77. if (trash_count > 0) {
  78. for (int i = 0; i < trash_count; i++) {
  79. if (i == trash_count - 1) {
  80. // Last trash attacks the first target
  81. moves.pb({a[i].ss, a[first_target_idx].ss});
  82. } else {
  83. // Trash attacks next trash
  84. moves.pb({a[i].ss, a[i+1].ss});
  85. }
  86. }
  87. }
  88.  
  89. // 2. Survivors attack Targets
  90. // We pair Survivor[i] with Target[i]
  91. for (int i = 0; i < m; i++) {
  92. int target_idx = n - 2 * m + i;
  93. int survivor_idx = n - m + i;
  94.  
  95. // Survivor attacks Target
  96. moves.pb({a[survivor_idx].ss, a[target_idx].ss});
  97. }
  98.  
  99. // Output
  100. cout << sz(moves) << endl;
  101. for(auto p : moves) {
  102. cout << p.ff << " " << p.ss << endl;
  103. }
  104. }
  105.  
  106. int32_t main() {
  107. fast_io;
  108. int t;
  109. cin >> t;
  110. while(t--) {
  111. solve();
  112. }
  113. return 0;
  114. }
Success #stdin #stdout 0s 5320KB
stdin
7
4 2
1 4 2 3
2 2
6 7
3 0
1 2 3
3 1
1 2 3
3 2
1 2 3
4 1
2 3 4 5
6 0
998244353 1000000000 314159265 676767677 999999999 987654321
stdout
2
4 1
2 3
-1
2
1 3
2 3
2
1 2
3 2
-1
3
1 2
2 3
4 3
3
3 2
4 2
6 2