fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define int long long
  4. #define mod 1000000007
  5.  
  6.  
  7. void solve(){
  8. int n;cin>>n;
  9. int v[n];
  10. set<pair<int,int>>s;
  11. int k=1;
  12. for(int i=0;i<n;i++){
  13. cin>>v[i];
  14. if(v[i]!=0)s.insert({v[i],k});
  15. k++;
  16. }
  17. vector<pair<int,int>>ans;
  18. while(s.size()>1){
  19. auto st=s.begin();
  20. auto end=s.end();
  21. end--;
  22. s.erase(st);
  23. s.erase(end);
  24. s.insert({(*end).first-1,(*end).second});
  25. if((*st).first>1)s.insert({(*st).first-1,(*st).second});
  26. ans.push_back({(*st).second,(*end).second});
  27.  
  28. }
  29. cout<<ans.size()<<endl;
  30. for(int i=0;i<ans.size();i++){
  31. cout<<ans[i].first<<" "<<ans[i].second<<endl;
  32. }
  33. }
  34. int32_t main(){
  35. ios_base::sync_with_stdio(false);
  36. cin.tie(NULL);
  37.  
  38. int t;cin>>t;
  39. while(t--){
  40. solve();
  41. }
  42. }
Success #stdin #stdout 0.01s 5348KB
stdin
8
2
2 3
3
1 2 3
4
1 2 3 4
3
0 0 2
2
6 2
3
0 0 2
5
8 2 0 1 1
5
0 1 0 0 6
stdout
2
1 2
1 2
3
1 3
2 3
2 3
5
1 4
2 4
2 3
3 4
3 4
0
2
2 1
2 1
0
4
4 1
5 1
2 1
2 1
1
2 5